HYDRA – Ruby gem
September 23, 2010
Hydra is a distributed testing framework. It allows you to distribute your tests locally across multiple cores and processors, as well as run your tests remotely over SSH.
Hydra’s goals are to make distributed testing easy. So as long as you can ssh into a computer and run the tests, you can automate the distribution with Hydra.
nstalling Hydra
Installing Hydra is easy, just install the gem:
gem install hydra OR Download the gem file from http://gemcutter.org and install itSetting up Hydra
To set up Hydra, you need to edit your project’s Rakefile and add this code:
# require the hydra codebase require 'hydra' # require the hydra rake task helpers require 'hydra/tasks'Adding Cucumber Files
# set up a new hydra testing task named 'hydra:cucumber' run with "rake hydra:cucumber"
Hydra::TestTask.new('hydra:cucumber') do |t|
# add all files in the features directory that end with ".feature"
t.add_files 'features/**/*.feature'
end
Adding Test::Unit Files
# set up a new hydra testing task named 'hydra:units' run with "rake hydra:units"
Hydra::TestTask.new('hydra:units') do |t|
# add all files in the test/unit directory recursively that
# end with "_test.rb"
t.add_files 'test/unit/**/*_test.rb'
# and test/functional
t.add_files 'test/functional/**/*_test.rb'
# and test/integration
t.add_files 'test/integration/**/*_test.rb'
end
Adding RSpec Files
# set up a new hydra testing task named 'hydra:spec' run with "rake hydra:spec"
Hydra::TestTask.new('hydra:spec') do |t|
# you may or may not need this, depending on how you require
# spec_helper in your test files:
require 'spec/spec_helper'
# add all files in the spec directory that end with "_spec.rb"
t.add_files 'spec/**/*_spec.rb'
end
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
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
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,
For more details –>
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:
For more details about sinatra,
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
- Called when page parsing starts
def begin_page(arg = nil)
@content << “”
end
- record text that is drawn on the page
def show_text(string, *params)
@content.last << string.strip
end
- 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
- 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

