linux


Problem
You want to send emails from a Ruby on Rails application, when there is a specific condition on a database table. If the database table gets modified by another application outside Rails you cannot use an observer model.

Solution
We already assume that:

  • You are using a database
  • You have a model named voicemail (id, number_id, audio, created_at, updated_at)
  • You have a model named number (id, voicemail_email_set, voicemail_email, ….)
  • A mail server to use (smtp in our case)
  • Another application (voice application) populates the voicemail table but with empty updated_at values

So the steps we have to follow are:

  1. Change the settings in your config/environment.rb file to use the settings for your mail server, and make sure you restart your application after the changes:
    ActionMailer::Base.smtp_settings = {
      :address        => "yourmailserver.com",
      :port           =>  25,
      :domain         => "your.domain.com",
      :authentication => :login,
      :user_name      => "your_smtp_username",
      :password       => "your_smtp_password",
      :raise_delivery_errors  => true}
  2. Create your mailer model (ie voicemail_mailer.rb), in app/models:
    class VoicemailMailer < ActionMailer::Base
       # We need the open-uri to be able to open url *** if the file to attach is in an http location ***
      require 'open-uri'
    
      def sent(email_to,email_from,email_subject,email_body,voicemail_to_send)
        # Check to see if we have a file for the email body message
        @subject    = email_subject
        @body       = email_body
        @recipients = email_to
        @from       = email_from
        @sent_on    = Time.now
    
        # Split the file in directory and filename
        file_path = File.split(voicemail_to_send)
        file_dir  = file_path[0]
        file_name = file_path[1]
    
        # Get the file
        tmp_file = open(voicemail_to_send).read
    
        part( :content_type => “application/wav”,
              :disposition => “attachment; filename=#{file_name}”,
              :transfer_encoding => “base64″) do |attachment|
                attachment.body = tmp_file
        end
      end
    end
    
  3. Create your email scheduler in file lib/email_scheduler.rb:
    #!/usr/bin/env /path_to_your_app/script/runner
    
    # get all the voicemails that have not been sent yet
    voicemails_to_email = VoiceMail.find(:all, :conditions => 'updated_at is null')
    
    # For all the voicemails we have, send them and update the field date_sent
    for vm2email in voicemails_to_email do
      # Get the number for the voicemail
      number = Number.find(vm2email.number_id)
    
      # check to see if the send to email is set for the number
      if number.voicemail_email_set
        # Get number details (email_to,email_from etc)
        email_to          = number.voicemail_email
        voicemail_to_send = vm2email.audio
        # Set other details
        email_from      = 'Service@yourdomain.com'
        email_subject   = 'Please find attached your voicemail message'
        email_body      = "Received on: #{Time.now} \n for number: #{number.phone_no}"
    
        # Now send the email
        VoicemailMailer.deliver_sent(email_to,email_from,email_subject,email_body,voicemail_to_send)
    
        # And update the record's date_sent field
        vm2email.updated_at = Time.now
        vm2email.save
      end
    end
    
  4. Create a task in your crontab that runs the scheduler (every five minutes):
    0,5,10,15,20,25,30,35,40,45,50,55 * * * * path_to_your_ror_app/lib/email_scheduler.rb

Today, there was an announcement about Dreamhost, having the new Passenger (mod_rails) feature ready for use with Ruby on Rails applications.
The full announcement is here.

It should be changing the way Rails applications are deployed, making it much easier.

Problem
You want to find out your UID for a specific domain that you have set up with google analytics.

Solution
When you first create the tracking code for your website, it’s quite obvious how to get the UID for that specific website.
If you decide later on to change your site, from drupal to wordrpess for example, it’s not quite clear where to get this UID, to use it with the new tracking plugin in the new site (which still uses the same domain name).
After some searching it turns out that to be able to find it you have to do the following:

  1. Log in to your Google Analytics Account
  2. Click on the ‘Edit’ link under Settings for the domain you want to find the UID
  3. In the new page click on the link ‘Check Status’ after Receiving Data at the top
  4. Your UID should be in the text box with something like _uacct = “UA-xxxxxxx-x”;

Problem
You want to pass the £ sign to an http service, but the ruby CGI.escape encodes it incorrectly.

Solution
After using ruby’s CGI.escape for the string as:

sms_msg_tmp=CGI.escape(sms_code)

then replace the encoding with the pound sign encoding as in:

sms_msg=sms_msg_tmp.gsub('%C2%A3','%A3')

It should then pass the correct value for the £ sign.

Problem
Trying to use the heroku gem to clone a project and do local modifications I got the following error:

git: fatal error: `chdir' failed: permission denied.

Solution
It turns out that in Mandriva, when using:

sudo urpmi git

what gets installed is the ‘GNU Interactive Tools’ that has nothing to do with the git version control system.
So make sure you first uninstall the git installed:

sudo rpm -e git

and then install the Git - Fast version control system, by doing:

sudo urpmi git-core

You should then be able to clone a heroku application:

heroku clone myapp

Problem
You want to convert some legacy tables created in Paradox (.db, .px) to another format so you can use it in MySQL.

Solution
Download the px tools from here.

Follow the instructions, in the INSTALL file after you untar the file.
You should have to do the usual three step linux installation:

configure
make
sudo make install

Afterwards to make sure that the .db file is a paradox file run:

pxinfo -f  path/to/paradox/db/file.db

The program should read the header and report back with something along the lines:

File-Version: Paradox 7.x
Filetype: indexed .DB
....

To export each Paradox db file to an sql statement run the following:

pxsqldump -d mysql -f path/to/paradox/file.db > path/to/mysql/exported/file.sql

Problem
After upgrading to Rails 2.0.2 when trying to install the sqlite3-ruby gem got the following error:

ERROR:  Error installing sqlite3-ruby:
ERROR: Failed to build gem native extension.
/usr/bin/ruby extconf.rb install sqlite3-ruby
checking for sqlite3.h... no
make
make: *** No rule to make target `ruby.h', needed by `sqlite3_api_wrap.o'.  Stop.
Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1/ext/sqlite3_api/gem_make.out

Solution
In Mandriva you need to install the ruby-sqlite3 and the development libraries first, like:

sudo urpmi ruby-sqlite3
sudo urpmi libsqlite3-devel

After that you should be able to install the gem as normal.

Problem
You want to use cvs to checkout a repository from a remote server.

Solution
If you were using subversion it would be very simple to do by using the remote url.
In CVS you have to to do the following:
First find out where the cvsroot in the remote server is. We will assume for this example that is located in /usr/local/cvsroot.
Then run the following on your local linux pc:

export CVSROOT=:ext:user_name@remote_pc:/path_to_csvroot export
export CVS_RSH=ssh

You then should be able to do a checkout by typing the following and supplying your password:

cvs checkout project_name

Problem
You want to be able to login to different servers with ssh, but don’t want to be using your password every time.

Solution
We assume that you already have installed ssh and have created ssh public keys in your local machine.

  1. Run the following in your local machine:
    ssh-copy-id -i ~/.ssh/id_rsa.pub user_name@remote_host
  2. If you want to add aliases in your bash profile so you don’t have to type the whole address of the remote host, edit your .bashrc and add:
    alias short_name="ssh user_name@remote_host"
  3. Restart your X server
  4. You should be able to login with ssh without using your password by typing in your command prompt in your local machine:
    short_name

Problem
You have a lot of ps files and you want to combine them into a single pdf file with multiple pages.

Solution
You can use Ghostscript:

gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf in1.ps in2.ps in3.ps