CSS

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

Live number in ruby

Live number in ruby


Live number is a number of the total sum for birthday year,months and days.
Below is the ruby implementation:

def live_number(birthday)
  reduce("#{birthday.year}#{birthday.month}#{birthday.day}")
end

def reduce(sum)
  add_sum = each_digits(sum).reduce(:+)
  add_sum >= 10 ? reduce(add_sum) : add_sum
end

def each_digits(num)
  num.to_s.split("").map{|c| c.to_i }
end


spec:

describe "live_number" do 

  before :each do 
    @birthdays = [Time.new(2001,3,5),
                  Time.new(1978,5,26),
                  Time.new(1962,10,1)]
    @birth = {:day => Time.new(1981,6,11), :ans => 9 }
  end

  it "return number of live by birthday" do 
    @birthdays.each do |day|
      live_number(day).should be_a_kind_of(Fixnum)
    end
  end

  it "return the correct number of live" do
    @birthdays.each do |day|
      live_number(day).should == 2
    end
  end

  it "return correct number with specific birthday" do 
    live_number(@birth[:day]).should == @birth[:ans]
  end
end

Ruby Warrior!!



Ruby Warrior is a game that teach you how to programming with Ruby, and also how to write AI script.


Install


gem install rubywarrior
rubywarrior


And you will have a player.rb script and README,


sample code for player.rb

def play_turn(warrior)
    if warrior.feel.empty?     
      warrior.attack!
    elsif warrior.health <= 5
      warrior.rest!
    else
      warrior.walk!
    end
  end

follow the instruction to implement player.rb, then run the rubywarrior to start fighting with your warrior!!

Watchr : simple continuous testing tool

Watchr is a pretty cool and simple tool for saving developer's time.

According to the preject description, watchr is:
Agile development tool that monitors a directory tree, and triggers a user defined action whenever an observed file is modified. Its most typical use is continuous testing, and as such it is a more flexible alternative to autotest.

But moreover, Watchr can be used on every simple task that need to execute a file, like markdown generate, coffee script compile, and the syntax is so simple to add those task in watchr script.

The syntax is like:

watch 'regular expression' { |matched_file| action_for matched_file }

#listen to all file and print filename when it modified
watch('.*') {|match| puts match[1]}

#listen to ruby file and run test
watch('lib/(.*)\.rb') {|match| system("ruby test/test_#{match[1]}.rb") }

#match[0] is full filename, match[1] is filename without extname.

running under command:
watchr script_name

Continous Testing


Continous Testing is a concept born from MIT Program Analysis Group,
It improve the Test Driven Development, which is auto run the corresponding test
in TDD right after the code saved. In this way, the test time lag will be reduced to zero,
and programer won't need to run the test to know the program is work or not. Makes testing become natural behavior in development.

Refers


ihower's rails3 watchr script template
Doc generate template from watchr project itself (dog fooding)

Foursquare API with oauth2 gem

The oauth things is pretty complex, here is some notes that may avoid you fall into the same pitfall as me.

You can read the whole spec here.

But basically, it provide a interface let website can access the user's data in other website.
User will first be redirect to that website, click confirm, and redirected back to the callback page.

In the back end, the process of server is:
1.Send request to target website for token, with the server's client_id & secret_id
2.Redirect user to authorize page with token (and callback url)
3.After user confirm, user will be redirect to the callback page with a access_token
4.Save the access_token, and free to call target server for user data.

However, using gem to manage those things seen like a smart way, but still, it may not work correctly because of some error:

#pitfall 1

Check the api is using oauth or oauth2,
which is different when choosing the oauth gem.

#pitfall 2

check the oauth_path, access_token_path is correct
the default setting of gem may not work with the api you choose.

#pitfall 3

check the api address,
in foursquare, the authorize url is : https://foursquare.com/oauth2/authorize
however, the api is not under the same subdomain, is under https://api.foursquare.com/

make sure those setting is correct, and you will feel the sweet of oauth!

here's the code with sinatra:

require 'sinatra'
require 'oauth2'

get '/auth' do
  redirect to(client.web_server.authorize_url(:redirect_uri =>
                                              "http://myhost/callback"))
end

get '/callback' do
  #make sure the access token is named "code"
  access_token = client.web_server.get_access_token(params[:code],
                                      :redirect_uri => "http://myhost/callback")
  access_token.get('https://api.foursquare.com/v2/users/self')
  # save your access token in session or db...
end

def client
  client ||= OAuth2::Client.new(
  YOUR_CLIENT_ID,YOUR_SECRET_ID,
  :site => 'https://foursquare.com',
  :authorize_path => '/oauth2/authorize',
  :access_token_path => '/oauth2/access_token')
end