HYDRA – Ruby gem

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 it

Setting 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

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

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

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

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()

contacts – Ruby gem – Import contacts from Gmail, Yahoo, hotmail

Import contacts from Gmail, Yahoo, Hotmail using contacts gem

Installation:

1. gem install contacts

2. OPTIONAL — Not required –> Install acts_as_authenticated plugin
–> https://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=”https://raveendran.wordpress.com”>https://raveendran.wordpress.com</a&gt;) 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

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=”https://raveendran.wordpress.com&#8221;
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