Register now and start sharing your code snippets.
-->

Removing HTML tags from a string in Ruby

Ruby posted 7 months ago by marko

I don’t take credit for the regexp. The source for it is Mastering Regular Expressions by Jeffrey E.F. Frield.

   1  def remove_html_tags
   2      re = /<("[^"]*"|'[^']*'|[^'">])*>/
   3      self.title.gsub!(re, '')
   4      self.description.gsub!(re, '')
   5    end

Tagged regexp, ruby, removing html tags

Rolling back database migration one step in Rails

Ruby posted 7 months ago by marko

I found this awesome trick to rollback a Rails database migration one step. Add the following code to Rakefile.

   1  namespace :db do
   2    desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
   3    task :rollback => :environment do
   4      step = ENV['STEP'] ? ENV['STEP'].to_i : 1
   5      version = ActiveRecord::Migrator.current_version - step
   6      ActiveRecord::Migrator.migrate('db/migrate/', version)
   7    end
   8  end

And roll back with a breeze.

   1  rake db:migrate
   2  rake db:rollback

Tagged rails, active record migration

Geolocation with MaxMind's GeoIP and the geoip-city RubyGem

Ruby posted 7 months ago by christian

Install GeoIP library

   1  cd /usr/local/src/
   2  curl -O http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
   3  tar zxvf GeoIP.tar.gz 
   4  cd GeoIP-1.4.4
   5  
   6  ./configure
   7  make
   8  make check
   9  make install

Install the geoip-city gem

   1  git clone git://github.com/ry/geoip-city.git
   2  
   3  sudo gem install geoip_city -- --with-geoip-dir=/usr/local/src/GeoIP-1.4.4

Test the bindings

   1  curl -O http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
   2  gunzip GeoLiteCity.dat.gz

Fire up IRB and try the following code:

   1  require 'rubygems'
   2  require 'geoip_city'
   3  db = GeoIPCity::Database.new('GeoLiteCity.dat')
   4  result = db.look_up('192.143.34.23')
   5  p result

Another option is to use hostip.info’s database, as described in this article.

Tagged geoip, geolocation, maxmind

Hpricot's inner_text doesn't handle HTML entities correctly

Ruby posted 7 months ago by christian

Hpricot’s inner_text method is fubar and doesn’t handle HTML entities correctly, instead you’ll see questionmarks in the output. To fix this replace calls to Hpricot’s inner_text with a call to the following method (or Monkey patch Hpricot):

   1  require 'rubygems'
   2  require 'htmlentities'
   3  
   4    def inner_text(node)
   5       text = node.innerHTML.gsub(%r{<.*?>}, "").strip
   6       HTMLEntities.new.decode(text)
   7    end

Remember to install the htmlentities gem:

   1  sudo gem install htmlentities

Tagged hpricot, inner_text, problem, bug

Installing ImageMagick, mini-magick and rmagick on Mac OS X Leopard

Ruby posted 8 months ago by christian

I had no success installing ImageMagick and mini-magick with the instructions I found on this page but after some googling I found this blog post, which had the magic commands that worked for me:

   1  sudo port install tiff -macosx  #disables the linkage with Apple's open gl
   2  sudo port install ImageMagick
   3  
   4  sudo gem install rmagick
   5  sudo gem install mini_magick

To test mini-magick, open an irb console and paste in the following code:

   1  require 'rubygems'
   2  require 'mini_magick'
   3  
   4  path = "public/images/0000/0003/logo.jpg"
   5  image = MiniMagick::Image.new(path)
   6  
   7  #print width and height
   8  puts image[:width]
   9  puts image[:height]

Tagged imagemagick, attachment_fu, rails, ruby, mac, osx, leopard, mini-magick, rmagick