Showing posts with label Ruby on rails. Show all posts
Showing posts with label Ruby on rails. Show all posts

Tuesday, March 22, 2011

Installing Ruby 1.9.2 and Rails 3 stable on Ubuntu

There are a zillion threads on internet regarding fresh installation of ruby 1.9.2 and rails 3 on ubuntu, however, most of them have some sort of dependency missing. Lately, i found a very helpful link on this topic, so i thought of sharing the post

http://torqueo.net/installing-ruby-192-and-rails-3-stable-on-ubuntu/

Wednesday, March 16, 2011

Fetch emails from Gmail using POP3

There are a couple of protocols for fetching emails from email services such as Gmail.. POP and IMAP. In this post, i'll be telling a simple way of fetching emails from Gmail using POP3. The code is in Rails (will try to post a php version later if get time). The method is extremely simple and trivial. But before we jump to the code, the first thing you need to do is to enable POP access from your Gmail account. You can found the POP settings under "Settings" tab. Look at the below snapshot


Make sure that you save the changes. Now, we are done with the settings, let's see the code now.

for this code you need to have "net/pop".

require 'net/pop'

pop_server = 'pop.gmail.com'
pop_port = 995
username = "example@gmail.com"
password = "example_password"

Net::POP3.start(pop_server, pop_port, username, password) do |pop|
       if pop.mails.empty?
             puts "No Mail Found"
       else
             pop.mails.each do |email|
                  MailReader.receive(email.pop)
             end
       end
end

now, the only piece missing from the above code is "MailRead.receive". Basically, we need to have a email handler which has to be extended from ActionMailer. We just need to pass the email details to this handler and it will do the parsing for us. So, now let's see the code for this:

class MailReader < ActionMailer::Base
    def receive(email)
        puts "Subject ==>" + email.subject
        puts "Body ==> " + email.body
    end
end

That's all about it :) .. if you want to fetch and save attachments as well, just get the paperclip. You can download it from https://github.com/thoughtbot/paperclip (for direct installation with rails use the command rails plugin install git://github.com/thoughtbot/paperclip.git ).

Thursday, February 10, 2011

Thrift installation on ubuntu (for usage with ruby)

(This tutorial is tested on thrift 0.5.0)

I was working on thrift (http://thrift.apache.org/), found a couple of issues during the installation and had to look at a couple of links to figure out the correct method.. so i thought to sum it up here.
  1. Download thrift (http://thrift.apache.org/)
  2. Extract the downloaded archive and go to the folder through terminal
  3. In order to install thrift, you'd need following dependencies: (as mentioned on the thrift wiki page)
    1.  g++ 3.3.5+
    2. boost 1.33.1+ (1.34.0 for building all tests)
    3. Runtime libraries for lex and yacc might be needed for the compiler
    4. The dependencies for each language are different which you can see at the link => http://wiki.apache.org/thrift/ThriftRequirements
    5. GNU build tools: autoconf 2.59+ (2.60+ recommended), automake 1.9+, libtool
    6. For C, you'll also might need to install "libglib2.0-dev"
  4. In order to install above dependencies, you can use the following command
    sudo apt-get install g++ libboost-dev libevent-dev automake pkg-config libtool flex bison
  5. You might need to bootstrap first, for that run ./bootstrap.sh (assuming you are in the directory of extracted thrift)
  6. Now, run the following commands (in sequence) to configure and install thrift
    1. ./configure
    2. make
    3. sudo make install
  7. Now, you are good to go with thrift. The final piece missing is the thrift gem. You can install that using command => sudo gem install thrift

Hope you find the post helpful. I will try to post example of thrift using ruby soon :)