Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Monday, February 25, 2008

A local Ruby script example with fileutils, open-uri, rmagick and mysql

##################################
# a code example for writing a ruby script to be running locally
# to do some regular local jobs such as:
# 1, download files or images from Internet
# 2, local file operations like mkdir, cp and remove
# 3, access or store data into local database like mysql
# 4, image resizing or cropping using image-magick
##################################
require 'open-uri'
require 'fileutils'
require 'RMagick'
require 'mysql'

# init local directories
FileUtils.mkdir_p "original/montreal"
FileUtils.mkdir_p "thumbnailed/montreal"

# init the database access
db = Mysql::new('127.0.0.1', 'root', '12345', 'test')

# init links to Internet
base_url = "http://farm1.static.flickr.com/"
image_urls = ['40/123180238_552597a079_b.jpg',
'146/390735501_54f0b2e826_b.jpg',
'50/131278273_4ce6400bfa_b.jpg']

# create a table to store image names
db.query("DROP TABLE IF EXISTS images")
db.query("CREATE TABLE images (name VARCHAR(64))")
image_urls.each do |image|
db.query("INSERT INTO images (name) VALUES ('#{image}')")
end

# download images from Internet
for i in 0..2
open("original/montreal/#{i}.jpg", 'wb').write(open("#{base_url}#{image_urls[i]}").read)
end

# resize and crop images
for i in 0..2
image = Magick::Image.read("original/montreal/#{i}.jpg").first
image.crop_resized!(150, 150, Magick::NorthGravity)
image.write("thumbnailed/montreal/#{i}.jpg")
end
References:

Ruby MySQL: http://www.tmtm.org/en/mysql/ruby/
RMagick: Making thumbnails with RMagick
FileUtils: http://www.ruby-doc.org/stdlib/libdoc/fileutils/rdoc/index.html
Ruby OpenURI: http://www.ruby-doc.org/core/classes/OpenURI.html

Saturday, February 2, 2008

Run Ruby, Gems, Rails, MySQL and RMagick on Cygwin

Before using cygwin, I did not expect that the commands of rails do not work in cygwin. To make ruby, gems and rails to work, there are some additional tasks to do as following:

  • Add devel -> make + ruby + gcc + subversion modules from cygwin installation
  • Download gems tgz install package from Ruby Gems download home
  • tar xzvf rubygems-1.0.x.tgz
  • cd rubygems-1.0.x
  • unset RUBYOPT (before install gems, clear RUBYOPT=rubygems)
  • ruby setup.rb
  • gem install rails --include-dependencies
  • Download mysql source tar.gz file from MySQL download page
  • tar xzvf mysql-5.0.45.tar.gz
  • cd mysql-5.0.45
  • ./configure
  • make install ( or to do it faster, just make install under sub directories libmysql and include.
  • gem install mysql
  • Change the database server from localhost to 127.0.0.1 in the database.yml of your rails app
  • Install ImageMagick, libmagick-devel, XFree86-lib-compat, xorg-x11-devel, libbz2-devel module from cygwin installation file
  • gem install RMagick
  • ruby script/server. voila!

References:

Labnotes: Setting up Ruby + Gems on Cygwin
Softpedia: Install Rails on Windows with cygwin