Joker – Ruby gem

September 10, 2009

Joker – Ruby gem

Good Alternate for Regular expressions

require ‘rubygems’
require ‘joker’

wild = Wildcard['Fairy?ake*']

puts wild =~ ‘Fairycake’                     #=> true
puts wild =~ ‘Fairyfakes’                    #=> true
puts wild =~ ‘Fairylake is a cool place’     #=> true

puts wild =~ ‘Dairycake’                     #=> false
puts wild =~ ‘Fairysteakes’                  #=> false
puts wild =~ ‘fairycake’                     #=> false

puts wildi = Wildcard['Fairy?ake*\?', true]

puts wildi =~ ‘FairyCake?’                   #=> true
puts wildi =~ ‘fairyfakes?’                  #=> true
puts wildi =~ ‘FairyLake IS A COOL Place?’   #=> true

Wildcard.quote(‘*?\\’)                  #=> ‘\\*\\?\\\\’

For more details about this gem –> http://bit.ly/L9kcg

Faker – Ruby gem

September 1, 2009

Installation

cmd>gem install faker

Basic use:

require ‘faker’

puts c=Faker::Name.name

output:

lemon arora

Fill your MYSQL DB with faker gem.

Code:

require ‘mysql’
require ‘faker’

db = Mysql::new(“localhost”, “root”, “”, “jazzezravi_development”)
i=1
while i < 1000 do

c=Faker::Name.name
c.gsub(“‘”,”")
sql = “INSERT INTO datas VALUES(#{i},’#{c}’,'0′,’0′);”
db.query(sql)
i+=1
end

For more details about faker gem –> http://faker.rubyforge.org

Ruby +Mysql

August 12, 2009

Connecting, retriving data from MYSQL database:

Code

require ‘mysql’

db = Mysql::new(“localhost”, “root”, “”, “new_development”)
sql = “SELECT url FROM sites;”
x= db.query(sql)
x.each do |record|
puts record
end

Note:

root – SQL  db user name

“” – Password for root access

new_development – DB Name

url -  Field Name

sites – Table Name

Output:

From sites table –> url field –>

http://yahoomail.com
http://70.85.16.159/
google.com
yahoo.com
google.com

Action Mailer – Ruby Gem

August 12, 2009

Mail attachment in Ruby

Code:

require ‘rubygems’
require ‘action_mailer’

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => “ADDRESS”,
:domain => “DOMAIN”,
:port    => 2(PORT NUMBER),
:authentication => :login,
:user_name => “USERNAME”,
:password => “PASSWORD”
}
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = “utf-8″

class Emailer < ActionMailer::Base
def test_email()
subject    “Test mail from jazzez”
from       “jazzezravi@gmail.com”
recipients ‘jazzezravi@gmail.com’
body    ”Test mail with attachment”
attachment :content_type => “application/text”,
:body =>File.read(“ANYTEXTFILE”), :filename => “ANYTEXTFILE”
end

end

Emailer.deliver_test_email()

Import contacts from Gmail, Yahoo, Hotmail using contacts gem

Installation:

1. gem install contacts

2. OPTIONAL — Not required –> Install acts_as_authenticated plugin
–> http://raveendran.wordpress.com/2007/11/02/acts_as_authenticated-plungin-in-ror/

3. config/environment.rb file –> Add first line –> require ‘contacts’

4. controller:

controller.rb
def import_contacts

end

def import
@users = User.find(params[:id]) #or use — @users = User.find(1)
begin
@sites = {“gmail” => Contacts::Gmail, “yahoo”=> Contacts::Yahoo, “hotmail” => Contacts::Hotmail}
@contacts = @sites[params[:from]].new(params[:login], params[:password]).contacts
@users , @no_users = [], []
@contacts.each do |contact|
if u = User.find(:first , :conditions => “email = ‘#{contact[1]}’” )
@users << u
else
@no_users << {:name => contact[0] , :email => contact[1]}
end
end
respond_to do |format|
format.html {render :template => ‘CONTROLLER/_list, :layout => false}
format.xml {render :x ml => @contacts.to_xml}
end
end

5. view file :

import_contacts.html.erb

<h2>Welcome to Raveendran’s Blog(<a href=”http://raveendran.wordpress.com”>http://raveendran.wordpress.com</a>) Contact import Page</h2><br />
<b> Please select the domain here: </b><br />
<% form_tag :action => ‘import’, :id => @user do %>
<select name=”from” id=”from”>
<option value=”">Select Id</option>
<option value=”gmail”>Gmail</option>
<option value=”yahoo”>Yahoo</option>
<option value=”hotmail”>Hotmail</option>
</select>
<br />
<p><b>Please Enter Your Email Address Below : (Ex. test1@gmail.com)</b><BR />
<input type=”text” name=”login”></p>
<p><b>Enter Your Password :</b><br />
<input type=”password” name=”password”></p>
<p><%= submit_tag ‘Fetch Friends’ %>
<% end %>

6. Result page:

_list.html.erb

<% for i in @contacts %>
<input type=”checkbox” name=”email[]” id=”email_<%= i %>” value=”<%= i %>” /><%= i %><br>
<% end %>

7. http://localhost:3000/CONTROLLER/import_contacts

8. Enjoy with your old contacts

Note: currently import hotmail contact gives some protocol error. Once the issue fixed in contacts gem I will update here. comments are always welcome

ramaze — Ruby gem

March 2, 2009

Ramaze — Ruby gem
1.gem install ramaze

2. Write code:

jazzez.rb

require ‘rubygems’
require ‘ramaze’

class AnotherController < Ramaze::Controller
map ‘/another’

# http://localhost:7000/another/hello
def hello
home=”http://raveendran.wordpress.com”
link=”<a href=#{home}>#{home}</a>”
“If you want to know about jazzez then go to #{link}”
end
end

Ramaze.start

3. Run the program –  cmd> ruby jazzez.rb

4. Open this link in browser –> http://localhost:7000/another/hello

Output looks like,

ramaze

For more details –>

1. http://ramaze.net

sinatra — Ruby gem

March 2, 2009

Sinatra – Ruby Gem:


1.gem install sinatra

2. Write code:

jazzez.rb:

require ‘rubygems’
require ’sinatra’
get ‘/jazzez’ do
home=”http://raveendran.wordpress.com”
link=”<a href=#{home}>#{home}</a>”
“If you want to know about jazzez then go to #{link}”
end

3. Run the program –  cmd> ruby jazzez.rb

4. Open this link in browser –> http://localhost:4567/jazzez

Output looks like:

sinatra

For more details about sinatra,

1. http://www.sinatrarb.com/

Ruby gem –> fxruby

February 13, 2009

Fxruby sample program

1. gem install fxruby

Code:

require ‘fox16′
include Fox
theApp = FXApp.new
theMainWindow = FXMainWindow.new(theApp, “Hello”)
theButton = FXButton.new(theMainWindow, “Hello, World!”)
theButton.connect(SEL_COMMAND) do |sender, selector, data|
exit
end
theApp.create
theMainWindow.show
theApp.run

pdf-reader — Ruby gem

February 13, 2009

PDF To Doc coversion with the help of pdf-reader gem

require ‘rubygems’
require ‘pdf/reader’
class PageTextReceiver
attr_accessor :content

def initialize
@content = []
end

  1. Called when page parsing starts

def begin_page(arg = nil)
@content << “”
end

  1. record text that is drawn on the page

def show_text(string, *params)
@content.last << string.strip
end

  1. there’s a few text callbacks, so make sure we process them all

alias :super_show_text :show_text
alias :move_to_next_line_and_show_text :show_text
alias :set_spacing_next_line_show_text :show_text

  1. this final text callback takes slightly different arguments

def show_text_with_positioning(*params)
params = params.first
params.each { |str| show_text(str) if str.kind_of?(String)}
end
end

receiver = PageTextReceiver.new
pdf = PDF::Reader.file(“c:\\user.pdf”, receiver)
file=File.open(“test.doc”,”w”)

file.puts receiver.content.inspect
file.close

Mack Frmaework – Installation

C:\Documents and Settings\raveendran>gem install mack
Need to update 15 gems from http://gems.rubyforge.org
……………
complete
Install required dependency mack_ruby_core_extensions? [Yn] y
Install required dependency application_configuration? [Yn] y
Install required dependency cachetastic? [Yn] y
Install required dependency thin? [Yn] y
Select which gem to install for your platform (i386-mswin32)
1. thin 0.8.0 (i386-mswin32)
2. thin 0.8.0 (ruby)
3. Skip this gem
4. Cancel installation
> 1
Install required dependency eventmachine? [Yn] y
Select which gem to install for your platform (i386-mswin32)
1. eventmachine 0.8.1 (i386-mswin32)
2. eventmachine 0.8.1 (ruby)
3. Skip this gem
4. Cancel installation
> 1
Install required dependency crypt? [Yn] y
Successfully installed mack-0.4.6
Successfully installed mack_ruby_core_extensions-0.1.5
Successfully installed application_configuration-1.2.2
Successfully installed cachetastic-1.4.2
Successfully installed thin-0.8.0-i386-mswin32
Successfully installed eventmachine-0.8.1-i386-mswin32
Successfully installed crypt-1.1.4
Installing ri documentation for mack-0.4.6…
Installing ri documentation for mack_ruby_core_extensions-0.1.5…
Installing ri documentation for cachetastic-1.4.2…
Installing ri documentation for thin-0.8.0-i386-mswin32…
Installing ri documentation for eventmachine-0.8.1-i386-mswin32…
Installing RDoc documentation for mack-0.4.6…
Installing RDoc documentation for mack_ruby_core_extensions-0.1.5…
Installing RDoc documentation for cachetastic-1.4.2…
Installing RDoc documentation for thin-0.8.0-i386-mswin32…
Installing RDoc documentation for eventmachine-0.8.1-i386-mswin32…