Apr 4 2009

The Rubyist: March 2009 Edition

Here is a recap of the top Ruby-related links for the month of March 2009. Links for The Rubyist are provided by A Rubyist Railstastic Adventure, a tumblelog.

Ruby

Rails


Mar 1 2009

The Rubyist: February 2009 Edition

Here is a recap of the top Ruby-related links for the month of February 2009. Links for The Rubyist are provided by A Rubyist Railstastic Adventure, a tumblelog.

Ruby

Rails

Rails Caching and Scaling

JRuby


Feb 2 2009

The Rubyist: January Edition

Here is a recap of the top Ruby-related links for the month of January 2009. Links for The Rubyist are provided by A Rubyist Railstastic Adventure, a tumblelog.

Ruby

Ruby on Rails

JRuby


Jan 17 2009

The Rails Mythbuster

Everybody has an opinion on the most opinionated web application framework, Ruby on Rails. Some developers have spread a campaign of misinformation and FUD but this has not stopped the momentum or monumental growth in Rails adoption. But after five years of active development and pissing off committee full of astronaut architects DHH busts some common Ruby on Rails myths…


Jan 14 2009

ActiveRecord: Ruby on Rails Optional

Ruby on Rail is made up of a variety of code modules packaged as Ruby Gems. For example, Rails is composed of gems such as ActionController, ActionView, ActionRecord, and few other gems. It might sound surprising at first, but you can use ActiveRecord without the other Rails components. ActiveRecord is an Object Relational Mapper (ORM), it binds database records to Ruby classes that act as models to your data. ActiveRecord models allow you to post and query data to and from a relational database.

To use ActiveRecord, you will need to have it installed in your system. If you already have Rails, ActiveRecord is already part of your Ruby gems environment. If not, you can install it via the following command.

sudo gem install activerecord

Once the ActiveRecord gem is installed in your system, you can require it in your Ruby scripts.

require 'rubygems'
require 'active_record'

In a Ruby on Rails project, the only configuration not defaulted through convention is the information required to setup your database connection. In Rails, you usually have a config/database.yml file to point to the right database used in your web application. This information is passed to ActiveRecord to establish a database connection. In a Ruby script, a non-Rails project, you can manually create a database connection with the following snippet.

ActiveRecord::Base.establish_connection(
  :adapter => 'mysql',
  :database => DB_NAME,
  :username => DB_USERNAME,
  :password => DB_PASSWORD,
  :host => LOCALHOST,
  :port => DB_PORT
)

Obviously, you would need to replace the placeholders for the correct database name, username, and password to connect to your database.

There are times when you might need to enable the logger. You might get an undefined method or nil object error if you don’t set a value to the ActiveRecord logger. You can configure ActiveRecord to log to the standard error or standard output. To accomplish this, first require the logger gem then initialize and set the ActiveRecord logger.

ActiveRecord::Base.logger = Logger.new(STDERR)

That is essentially all that is required. Once you have a database connection, you can create models as you would in a Rails project.

class Post < ActiveRecord::Base
end

p = Post.new(:title => title, :body => body)
p.save

If you require ActiveRecord in your Ruby script, you can also use it to create database schema migrations. Once you have established an connection to the database you can run a schema migration such as the following.

create_table "users", :force => true do |t|
  t.integer "twitter_id", :default => 0, :null => false
  t.string "user_name", :limit => 20, :default => "", :null => false
  t.string "screen_name", :limit => 30, :default => "", :null => false
  t.string "location", :limit => 50, :default => "", :null => false
  t.string "url", :limit => 50, :default => "", :null => false
end

There are a few other ORMs written in Ruby, such as DataMapper and Sequel. One key factor of using ActiveRecord is that if you had dabbled with Rails before you already have it in your system, you don’t have to learn anything new, and you can be productive with it rather quickly.


Jan 12 2009

Twitter Ruby Gem

I been playing around with writing my own Twitter bot and client to backup my tweet feed and other natural language processing experiments. The good thing is that there are a ton of Twitter libraries in a wide variety of programming languages. At first I picked up a Java library, Twitter4J, but then quickly opted for a Ruby version instead because I just didn’t want to create large enterprise application before I can process with my Twitter timeline. As of this writing, Ruby has two good Twitter gems, Twitter4R and Twitter. I opted to use Twitter since it also has support for identi.ca micro blogging service.

Before you can get started, you need to have the Twitter gem installed.

sudo gem install twitter

Require the Twitter gem to start using it in your Ruby scripts.

require 'rubygems'
require 'yaml'
require 'twitter'

To access your Twitter account you will need to log in via your Twitter account username and password. For this example, I keep the login information in a yaml file. If you are more security conscious you might want to prompt for the password at run time or maybe simply encrypt it instead of keeping in a plain text file. That said, here is how to sign in…

tweet = YAML::load(File.open('tweet.yml'))
twitter = Twitter::Base.new(tweet['username'], tweet['password'])

That is it, to get your 100 most recent followers you can do so with the following.

twitter.followers().each { |f|
  puts f.name
}

To get the second page, the next 100 most recent followers, just add the :page argument, such as the following

twitter.followers(:page => 2).each { |f|
  puts f.name
}

The #followers method returns a list of Twitter users. The following attributes are present for users, id, name, screen_name, location, description, url, and profile_image_url. Users may also have values for the following attributes such as profile_background_color, profile_text_color, profile_link_color, friends_count, followers_count, statuses_count, status, amongst others.

The Ruby Twitter gem exposes not just your followers but it allows you to query all replies and favorites, allows you to search twitter public stream, update your location, send direct message, and update your status.

The author of this gem has posted a ton of useful examples with functional sample code on his GitHub account. That should get any butting Twitter bot author up and running. It would also be a good idea to be familiar with the public and official Twitter API since this gem maps nicely on top of that public API.