Thursday, 28 June 2018

SMS integration using msg91 in ruby

require 'uri'
require 'net/http'

PARAMS

sender = 'RAJKMAR'
route = 4
mobiles = '91********'
authkey = '**********'
message = 'Your message'
country = '91'

API CALL:

url = URI("http://api.msg91.com/api/sendhttp.php?sender=#{sender}&route=#{route}&mobiles=#{mobiles}&authkey=#{authkey}&encrypt=&country=#{country}&message=#{message}")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Get.new(url)

response = http.request(request)

Friday, 15 June 2018

Deploy rails application with nginx and unicorn server configurations

NGNIX SETUP With Rails Application
==================================
1. sudo apt-get install nginx

2. sudo vi /etc/nginx/sites-enabled/default -> Open config file

3.Update the below changes in nginx default file

root /home/rajkumar/Rajkumar/Projects/rajuthayaa
server_name localhost;
location @raj{
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://localhost:3001;
}

4. sudo service nginx start

5. if any errors in nginx kindly run the below command and check it.
 sudo vi /var/log/nginx/error.log



UNICORN SETUP With Rails Application
====================================
1. gem 'unicorn' in your gemfiles

2. create unicorn.rb in config/unicorn.rb

3. Update the below changes in unicorn.rb files

APP_PATH = "/home/rajkumar/Rajkumar/Projects/rajuthayaa"

working_directory APP_PATH
stderr_path APP_PATH + "/log/unicorn.stderr.log"
stdout_path APP_PATH + "/log/unicorn.stdout.log"

preload_app true

timeout 30

listen "/home/rajkumar/Rajkumar/Projects/rajuthayaa/shared/sockets/unicorn.rajutaya.sock", :backlog => 64

listen 3002, :tcp_nopush => true

pid  APP_PATH + "/unicorn.pid"

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
      ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
      ActiveRecord::Base.establish_connection
end

upstream raj(raj){
server unix:/home/rajkumar/Rajkumar/Projects/rajuthayaa/shared/sockets/unicorn.rajutaya.sock fail_timeout=0;
}

4. Add upstream in nginx config files -->sudo vi /etc/nginx/sites-enabled/default

upstream raj(raj){
server unix:/home/rajkumar/Rajkumar/Projects/rajuthayaa/shared/sockets/unicorn.rajutaya.sock fail_timeout=0;
}

5. create shared/sockets inside your rails app

6. unicorn_rails -c config/unicorn.rb -D for run the unicorn server.

7. if any issues kindly check the log/unicorn.stderr.log files.

8. Restart both nginx and unicorn server it will work.

Wednesday, 6 June 2018

Unix setup in rails application

Unicorn setup for deployment

1. gem 'unicorn' add in gem file

2. create config/unicorn.rb file

3. paste this file
# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir


# Set unicorn options
worker_processes 2
preload_app true
timeout 30

# Set up socket location
listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64

# Logging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"

# Set master PID location
pid "#{shared_dir}/pids/unicorn.pid"

4. mkdir -p shared/pids shared/sockets shared/log

5. create script in /etc/init.d/unicorn_appname

6. sudo chmod 755 /etc/init.d/unicorn_appname

7. sudo update-rc.d unicorn_appname defaults

8. sudo service unicorn_appname start

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...