Posts

Colors in Ruby Console Using Wirble 20 Dec 2009
Tags: log, console, gem, wirble

If you want to have colors in your console, install the wirble gem (Read the documentation).

For some reason, I can't override the colors when I follow the instructions in the README. In case someone's also experiencing this, you can try merging the colors in the Wirble::Colorize::DEFAULT_COLORS constant. Then call the init and colorize methods.

Here's mine (same as with vim's syntax highlighting for ruby and haml).

ruby
Wirble::Colorize::DEFAULT_COLORS.merge!({
  # delimiter colors
  :comma => :none,
  :refers => :none,

  # container colors (hash and array)
  :open_hash => :purple,
  :close_hash => :purple,
  :open_array => :purple,
  :close_array => :purple,

  # object colors
  :open_object => :light_red,
  :object_class => :white,
  :object_addr_prefix => :blue,
  :object_line_prefix => :blue,
  :close_object => :light_red,

  # symbol colors
  :symbol => :red,
  :symbol_prefix => :red,

  # string colors
  :open_string => :purple,
  :string => :red,
  :close_string => :purple,

  # misc colors
  :number => :red,
  :keyword => :brown,
  :class => :green,
  :range => :none
})

Nokogiri in Ubuntu 15 Mar 2010
Tags: gem, nokogiri, ubuntu

you need libxml and libxslt packages so

plain
>> sudo apt-get install libxml2 libxml2-dev libxslt1-de
>> sudo gem install nokogiri

enjoy nokogiri!

Awesome Print 07 Apr 2010
Tags: gem, awesome_print

There's another cool gem called awesome_print by michaeldv (Article).

I tried it out and I couldn't figure out how to customize it without editing the gem itself. So I forked the gem, which can be found on my github account.

To customize the configuration, add the following lines in your .irbrc

ruby
require 'ap'

AwesomePrint::OPTIONS.merge!(
  :indent => 2,
  :multiline => true
)

has_many file attachments using Paperclip 16 Jun 2010
Tags: gem, paperclip, has_many, multiple file attachments

I decided to use paperclip instead of attachment_fu for our new app. This is just a procedure on how to use paperclip with a has_many association.

We create two models: post and post_photo

plain
>> ruby script/generate model post title:text body:text
>> ruby script/generate model post_photo post_id:integer

and associate them as follows.

ruby
class Post < ActiveRecord::Base
  has_many :post_photos
end

class PostPhoto < ActiveRecord::Base
  belongs_to :post
  has_atttached_file :photo
end

Using paperclip's generator we create a migration to add columns to post_photo

plain
>> ruby script/generate paperclip post_photo photo

After that, create your views and controller methods. (Btw, I use haml for the views. I also like creating an associated record using virtual attributes. I also used the prototype based js, Builder, to create dom elements.)

haml
# Post#new.html.haml
- form_for @post do |f|
  %label Title
  = f.text_field :post

  %label Body
  = f.text_area :body

  %label Photos
  = file_field_tag 'post[photo_attributes][]'

  #new_photos
  = link_to_function 'Add', 'add_file_field()'

:javascript
  function add_file_field() {
    var div = Builder.new('div');
    var file = Builder.new('input', {type:'file', name:'post[photo_attributes][]'});
    var link = Builder.new('a', {onclick:'$("this").up().remove();}, 'Remove');

    $('div').appendChild(file);
    $('div').appendChild(link);
    $('new_photos').appendChild(div);
ruby
# PostsController
def create
  @post = Post.new(params[:post])

  if @post.save
    flash[:notice] = 'Post saved'
    redirect_to @post
  else
    render :action => :new
  end
end

# post.rb
def photo_attributes=(attribs)
  attribs.each do |attrib|
    post_photos.build :photo => attrib
  end
end

Using HTMLEntities 06 Oct 2010
Tags: gem

I recently needed to decode/encode a string which will be used as text on one of the sites I'm syndicating to.

The problem:

The site escapes all html entities and tags. And since I'm posting to the site using Mechanize, I need to format the text so that it would come out clean and nice.

The solution:

HTMLEntities. I never really bothered what the HTMLEntities gem was. Never even wondered why it was a dependency of some gems/plugins I regularly use. Now I know. :D

Installing ruby in ubuntu with RVM 11 Oct 2010
Tags: gem, ubuntu, rvm

I needed to use different ruby versions in one box so I looked for a good way to manage ruby versions. One google search and I've found what I'm looking for, RVM. So the first stop is Railscasts. Episode 200 shows how to install Rails 3 using RVM.

After some experimentation with the help of the screencast and the rvm documentation, I was able to make everything working after encountering errors with zlib and readlines. So as a general rule of thumb, install all available package that comes with rvm. Use the following command

ruby
rvm package install ree_dependencies

to install zlib, ncurses, readline, openssl and iconv. Then you can install any ruby version with the following command

ruby
rvm install <ruby version> -C --with-readline-dir=$rvm_path/usr,--with-iconv-dir=$rvm_path/usr,--with-zlib-dir=$rvm_path/usr,--with-openssl-dir=$rvm_path/usr

By the way, I'm using Ubuntu 10.10.

specify gem groups to install in heroku 24 Jan 2011
Tags: gem, config, heroku

While I was migrating my wife's site to Rails 3 on Heroku, I encountered the following error

ruby
Installing linecache19 (0.5.11) with native extensions /usr/ruby1.8.7/lib/ruby/site_ruby/1.8/rubygems/installer.rb:483:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

To solve the problem above, add a config variable in heroku specifying that gems listed under a specific group should not be installed.

ruby
heroku config:add BUNDLE_WITHOUT="development test"

So adding the code above will skip gems under the development and test groups.

using mechanize with ssl gives "OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed" error 26 Mar 2012
Tags: gem, openssl, mechanize

I was given a task to scrape a site that uses ssl. So the first thing I thought of was using Mechanize. When going to the site that uses ssl, I was presented with the following error

plain
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

At first it was a real pain having to find the right tutorial to set the certificates that you'd use and how to tell mechanize to use them. To help those who might need this in the future, here it is.

First, download the list of certificates from http://curl.haxx.se/docs/caextract.html and save it to a filename. We'll use cacert.pem for this blog. Move cacert.pem inside the lib folder.

Next part is setting up the scraper agent.

ruby
cert_store = OpenSSL::X509::Store.new
cert_store.add_file = 'lib/cacert.pem'

agent = Mechanize.new
agent.cert_store = cert_store
agent.get 'https://github.com'

And that's it! Enjoy!