Formal|Semi-Formal|Informal Letter writing tips

Well, if you are a professional no matter where; you surely need to write letter/emails often. Letters can be of various types: formal, semi-formal and informal. Though you might have written all types in same format; had been accepted, there might come a situation when you will be rejected. So, I have noted down some templates of letter:


These are letters written to  Airlines, Bank, Insurance company, etc; or, somebody you don’t know. Continue reading

Rails : Make test suite with HTTP requests run faster

I was developing a movie sorting ruby application which will fetch data from IMDB database. Unfortunately the gem imdb I have been using to fetch data from IMDB did not have any solution inbuilt to deal with HTTP requests in test mode. My test suite was insanely slow.

Then, I found that the gem imdb uses a gem called fakeweb to fake the HTTP request. fakeweb requires you to know every URL your suite invokes to fake it. Its quite cumbersome, because you may not always figure which URLs the suite is invoking. So, it was a bad idea for me.

Then I thought about the gem vcr which I found to be an awesome tool to record all your HTTP request’s responses as fixtures for the future requests.

require 'rubygems'
require 'test/unit'
require 'vcr'

VCR.configure do |config|
  config.cassette_library_dir = "fixtures/vcr_cassettes"
  config.hook_into :webmock # or :fakeweb

class VCRTest < Test::Unit::TestCase
  def test_example_dot_com
    VCR.use_cassette("synopsis") do
      response = Net::HTTP.get_response(URI('http://www.iana.org/domains/reserved'))
      assert_match /Example domains/, response.body

Every HTTP request you or your code make inside the code_block passed to `VCR.use_cassette(“synopsis”)` will stubbed with the fixture data if present. If not, it will fetch once and use the same fixture data for future requests.




Rails : Upload secret files using Capistrano before booting

This answer is tested with Capistrano 3.6.1

Well, in that case, you would have to make capistrano upload your application.yml(database.yml if its ignored from git).

# in config/deploy.rb

# Default value for :linked_files is []
append :linked_files, 'config/database.yml', 'config/application.yml'

Now write some code to upload the files to the server

namespace :upload do
  desc 'Upload shared files to the server'
  task :yml_files do
    on roles(:web) do |host|
      fetch(:linked_files).each do |common_file|
        upload! common_file, "#{fetch(:deploy_to)}/shared/#{common_file}"

Now you need to trigger the above mentioned capistrano task just before a very specific event.

# config/deploy.rb

before 'deploy:check:linked_files', 'upload:yml_files'

This will make sure the necessary environment variables are set before your deployed tries to boot your application in the production server.


Ruby : Passing data to including module

Sometimes but not often in programming career in Ruby, you need to pass some sort of data to the module that is being included into your class. Firstly, I wondered how would I do that. Luckly Ruby is the only language ( I think) where you can do anything to solve problems.

The only limitation is your imagination and the Language never stops you doing anything. Since you can modify anything,  you end up with your own version of Ruby which you have created using Metaprogramming. Continue reading


Setting aliases in Unix system

Its so handy to execute long commands or series of commands with just a word in Unix systems like Linux, Mac, BSDs, etc. In Bash’s ~/.bashrc you can see a line like

 # ~/.bash_aliases, instead of adding them here directly.
if [ -f ~/.bash_aliases ]; then
 . ~/.bash_aliases

What it says is, you can have a file ~/.bash_aliases in which you can define alias to commands you need to run every once and while.

You can define alias like

# ~/.bash_aliases
alias greproute='rake routes | grep '
alias myshortcut='cd ~/projects/myproject && bundle exec rails c'

Solution 2

If you wished not to update the .bash_aliases file, you can have those lines defined in .bashrc file itself.

Note: The above mentioned commands/procedure is not gonna work for shells other than Bourne Again Shell(Bash).

For ZShell users

Open your ~/zshrc file in nano editor or any and paste the following line.

if [ -f ~/.bash_aliases ]; then
 . ~/.bash_aliases

Make sure, you have followed the instruction illustrated above to define aliases and have ~/.bashaliases

This works because syntax for bash and zsh are almost same.

Rails : Common SMTP settings


# config/initializers/smtp_settings.rb

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :user_name            => "xxx@gmail.com",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true



ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings   = {
    :address              => 'smtp.sendgrid.net',
    :port                 => '587',
    :user_name            => ENV['smtp_username'],
    :password             => ENV['smtp_password'],
    :authentication       => 'plain',
    :enable_starttls_auto => true

Many hosting providers and ISPs block port 25 as a default practice. When trying to connect to smtp.sendgrid.net remember that ports 25, 2525, 587, and 465 are all available for use.

We recommend port 587 to avoid any rate limiting that your server host may apply.

Setting attributes and their meaning

Allows detailed configuration for :smtp delivery method:

  • :address – Allows you to use a remote mail server. Just change it from its default "localhost" setting.
  • :port – On the off chance that your mail server doesn’t run on port 25, you can change it.
  • :domain – If you need to specify a HELO domain, you can do it here.
  • :user_name – If your mail server requires authentication, set the username in this setting.
  • :password – If your mail server requires authentication, set the password in this setting.
  • :authentication – If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain (will send the password in the clear), :login (will send password Base64 encoded) or :cram_md5 (combines a Challenge/Response mechanism to exchange information and a cryptographic Message Digest 5 algorithm to hash important information)
  • :enable_starttls_auto – Detects if STARTTLS is enabled in your SMTP server and starts to use it. Defaults to true.
  • :openssl_verify_mode – When using TLS, you can set how OpenSSL checks the certificate. This is really useful if you need to validate a self-signed and/or a wildcard certificate. You can use the name of an OpenSSL verify constant (‘none’, ‘peer’, ‘client_once’, ‘fail_if_no_peer_cert’) or directly the constant (OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER, …).






Install and Configure Monit web interface

Install Monit

Monit is easiest to install through apt-get:

$ sudo apt-get install monit

Once monit downloads, you can add programs and processes to the configuration file:

$ sudo nano /etc/monit/monitrc

Monit can be started up with a command that then keeps it running in the background

$ monit

Typing monit status displays monit’s details:

The Monit daemon 5.3.2 uptime: 1h 25m 

System 'myhost.mydomain.tld'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.03] [0.14] [0.20]
  cpu                               3.5%us 5.9%sy 0.0%wa
  memory usage                      26100 kB [10.4%]
  swap usage                        0 kB [0.0%]
  data collected                    Thu, 30 Aug 2012 18:35:00

Configure Monit

Monit is very easy to use nearly out of the box. By default, it is set up to check that services are running every 2 minutes and stores its log file in “/var/log/monit.log”.

These settings can be altered at the beginning of the configuration file in the set daemon and set logfile lines respectively.

Web Service

Monit comes with it’s own web server running on port 2812. To configure the web interface, find and uncomment the section that begins with set httpd port 2812. Once the section is uncommented, write in your server’s IP or domain name as the address, allow anyone to connect, and then create a monit user and password

set httpd port 2812
    use address  # only accept connection from localhost
    allow        # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'

Once this is configured, monit should reload and reread the configuration file, and the web interface will be available:

$ monit reload

When you are done, try

You will see a dialogbox appear in the browser.

General Monit Scripts


# /etc/monit/conf-available/sidekiq
check process sidekiq_thepact_staging0 with pidfile "/home/deployer/www/staging/shared/tmp/pids/sidekiq.pid"  
  start program = "/bin/su - deployer -c 'cd /home/deployer/www/staging/current && /usr/local/rvm/bin/rvm default do bundle exec sidekiq --config /home/deployer/www/staging/current/config/sidekiq.yml --index 0 -e staging -d'" with timeout 30 seconds  
  stop program = "/bin/su - deployer -c 'cd /home/deployer/www/staging/current && /usr/local/rvm/bin/rvm default do bundle exec sidekiqctl stop /home/deployer/www/staging/shared/tmp/pids/sidekiq.pid'" with timeout 110 seconds  group thepact-sidekiq-0


# /etc/monit/conf-available/pg
check process postgres with pidfile /var/run/postgresql/9.5-main.pid
 group database
 start program = "/etc/init.d/postgresql start"
 stop program = "/etc/init.d/postgresql stop"
 if failed unixsocket /var/run/postgresql/.s.PGSQL.5432 protocol pgsql
 then restart


# /etc/monit/conf-available/puma
check process puma with pidfile /var/www/myapp/production/shared/tmp/pids/puma.pid
 group www

 start program   = "/bin/su - shiva -c '~/.rvm/bin/rvm default do bundle exec puma -C /var/www/myapp/production/shared/puma.rb --daemon'"
 stop program    = "/bin/su - shiva -c 'pumactl stop -P /var/www/myapp/production/shared/tmp/pids/puma.pid'"
 restart program = "/bin/su - shiva -c 'pumactl restart -P /var/www/myapp/production/shared/tmp/pids/puma.pid'"


# /etc/monit/conf-available/puma
check process puma with pidfile /var/www/myapp/production/shared/tmp/pids/puma.pid
 group www
 start program   = "/bin/su - shiva -c '~/.rvm/bin/rvm default do bundle exec puma -C /var/www/myapp/production/shared/puma.rb --daemon'"
 stop program    = "/bin/su - shiva -c '~/.rvm/bin/rvm default do bundle exec pumactl -S /var/www/myapp/production/shared/tmp/pids/puma.state stop'"
 restart program = "/bin/su - shiva -c '~/.rvm/bin/rvm default do bundle exec pumactl -S /var/www/myapp/production/shared/tmp/pids/puma.state restart'"

Using the scripts

By default Monit doesnot load conf-scripts in conf-available. You have to copy the scripts to conf-enabled to have them executed by Monit. So, we are going to link those files that folder instead of copying/duplicating them. Its a good practice.

$ sudo ln /etc/monit/conf-available/puma /etc/monit/conf-enabled/puma

This will link the file there, same file will act as if it is there too.