How to fetch social facebook feeds ?

install gem

gem 'koala'

create rake tasks :

namespace :social do
  desc "Facebook  Cloning"
  task facebook: :environment do
    fb_token =                                                                                                                          'token'
    client = Koala::Facebook::API.new(fb_token)
    begin
      counts = 10 # change count to modify number of tweets fetched
      @posts = client.get_connection('errakeshpd',
                          'posts',{fields: ['message', 'id', 'from', 'type','picture', 'link', 'created_time', 'updated_time']}
                          )
      @posts = @posts.try(:first, counts)
      @posts.each do |post|
        fb_post = NewsFeed.where(title: "Social-Facebook", category: 0, description: post['message'], url: post['link'], created_at: post['created_time'], updated_at: post['created_time']).first_or_initialize

        if fb_post.new_record?
          fb_post.save
        end
      end
    rescue
      next
    end
  end
end

How to create facebook long lasting token ?

Use this in browser url and replace the variable with actual value then press enter , that is it..

https://graph.facebook.com/oauth/access_token?
    client_id=APP_ID&
    client_secret=APP_SECRET&
    grant_type=fb_exchange_token&
    fb_exchange_token=EXISTING_ACCESS_TOKEN

Mina and puma automated deployment rails application

config/puma.rb

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum, this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
#
port        ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
# preload_app!

# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted this block will be run, if you are using `preload_app!`
# option you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, Ruby
# cannot share connections between processes.
#
# on_worker_boot do
#   ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

deploy.rb

require 'mina/multistage'
require 'mina/whenever'
require 'mina/bundler'
require 'mina/rails'
require 'mina/tail'
require 'mina/puma'
require 'mina/git'
# require 'mina/rbenv'  # for rbenv support. (http://rbenv.org)
require 'mina/rvm'    # for rvm support. (http://rvm.io)

# Basic settings:
#   domain       - The hostname to SSH to.
#   deploy_to    - Path to deploy into.
#   repository   - Git repo to clone from. (needed by mina/git)
#   branch       - Branch name to deploy. (needed by mina/git)

# For system-wide RVM install.
#   set :rvm_path, '/usr/local/rvm/bin/rvm'

# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log', 'config/application.yml', 'pids', 'tmp/pids', 'tmp/sockets']
set :rvm_path, '/usr/local/rvm/scripts/rvm'

# Optional settings:
  set :user, 'ubuntu'    # Username in the server to SSH to.
  set :domain, 'ipaddress'
  set :deploy_to, '/home/rails/site'
  set :repository, 'git@gitlab.com:user/webapp.git'
  set :branch, 'sub_api'
  det :env, 'production'
#   set :port, '30000'     # SSH port number.
#   set :forward_agent, true     # SSH forward_agent.

# set :sidekiq_processes, 1
# set :sidekiq_pid, "#{deploy_to}/#{shared_path}/pids/sidekiq.pid"

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .ruby-version or .rbenv-version to your repository.
  # invoke :'rbenv:load'

  # For those using RVM, use this to load an RVM version@gemset.
  invoke :'rvm:use[ruby-2.3.0]'
end

# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task setup: :environment do
  # Puma needs a place to store its pid file and socket file.
  queue! %(mkdir -p "#{deploy_to}/#{shared_path}/tmp/sockets")
  queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/sockets")
  queue! %(mkdir -p "#{deploy_to}/#{shared_path}/tmp/pids")
  queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/pids")

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/pids"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/pids"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]

  queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
  queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
  queue! %[touch "#{deploy_to}/#{shared_path}/config/application.yml"]
  queue  %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/database.yml', 'secrets.yml and 'application.yml'."]

  if repository
    repo_host = repository.split(%r{@|://}).last.split(%r{:|\/}).first
    repo_port = /:([0-9]+)/.match(repository) && /:([0-9]+)/.match(repository)[1] || '22'

    queue %[
      if ! ssh-keygen -H  -F #{repo_host} &>/dev/null; then
        ssh-keyscan -t rsa -p #{repo_port} -H #{repo_host} >> ~/.ssh/known_hosts
      fi
    ]
  end
end

desc "Deploys the current version to the server."
task deploy: :environment do
  to :before_hook do
    # Put things to run locally before ssh
  end
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    # invoke :'sidekiq:quiet'
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_create'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile' if rails_env.eql? 'production'
    invoke :'deploy:cleanup'

    to :launch do
      invoke :'puma:stop'
      invoke :'puma:start'
      # invoke :'puma:phased_restart'
      # invoke :'sidekiq:restart'
    end
  end
end

desc "Seed data to the database"
task seed: :environment do
  queue "cd #{deploy_to}/current"
  queue "bundle exec rake db:seed RAILS_ENV=#{rails_env}"
end

desc "Restart nginx server"
task :restart do
  queue 'sudo service nginx restart'
end

# For help in making your deploy script, see the Mina documentation:
#
#  - http://nadarei.co/mina
#  - http://nadarei.co/mina/tasks
#  - http://nadarei.co/mina/settings
#  - http://nadarei.co/mina/helpers

How to use helper methodes in active admin custom view ?

How to use helper methodes in active admin custom view


create a file

app/helpers/active_admin_helpers.rb

Add content

module ActiveAdminHelpers
  def methode_name(param1)
  end
end

open your admin model file ‘include ActiveAdminHelpers’ like below

include ActiveAdminHelpers
ActiveAdmin.register_page "users" do
end

Then use the methode is custom view like :

<$= methode_name(param1) %>

How to run rails app using nginx puma

upstream mysite_app {
server unix:///home/ubuntu/mysite/shared/sockets/puma.sock;
}

server {
listen 80;
server_name mydomain.com;

root /home/ubuntu/mysite/public;

try_files $uri/index.html $uri @mysite_app;

location / {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection '';
proxy_pass http://mysite_app;
## Block SQL injections
set $block_sql_injections 0;
if ($query_string ~ "union.*select.*\(") {
set $block_sql_injections 1;
}
if ($query_string ~ "union.*all.*select.*") {
set $block_sql_injections 1;
}
if ($query_string ~ "concat.*\(") {
set $block_sql_injections 1;
}
if ($block_sql_injections = 1) {
return 403;
}
}
}

How to add pretty print to your development env:

Set your pry to pretty print the output using `awesome_print` gem
set this in ~/.pryrc

#!/usr/bin/ruby
require 'irb/completion'
require 'rubygems'

$LOAD_PATH << '/home/alfie/.rvm/gems/ruby-2.3.1/gems/awesome_print-1.6.1/lib/'
require "awesome_print"
Pry.config.print = proc { |output, value| output.puts <a href="http://value.ai/" target="_blank" rel="noreferrer" data-referer-safe="1" data-referer-original-href="http://value.ai">value.ai</a> }