Ruby实例三个

微型ruby web framework.非常的简单使用

require ‘rubygems’
require ‘sinatra’
get ‘/hi’ do
“Hello World!”
end

$ gem install sinatra
$ ruby hi.rb
== Sinatra has taken the stage …
>> Listening on 0.0.0.0:4567

基于Ruby的Twitter API封装,方便好用。
require ‘twitter’
puts ‘begin…’
#login
client = Twitter::Client.new(:login => ‘tony_luo’, :password =>’gene0801′);
#get login user information
user = client.my(:info)
puts user.name
#get followers information
followers = client.my(:followers)
puts followers.size
#get status according to timeline
client.timeline_for(:public) do |status|
puts status.user.screen_name, status.text
end
#post status
status = client.status(:post, ‘Cisco的收购脚步越来越快了!’)
status = client.status(:get, status.id)
#get messages
messages = client.messages(:received)
messages.each do |message|
puts message.text
end
puts ‘end…’
基于Twitter4r和DSL的微型twitter framework,非常的方便好用。
设计本身也可圈可点,正在用它学习Ruby DSL的相关知识。

require ‘twibot’

# Receive messages, and tweet them publicly
#
message do |message, params|
post_tweet message
end

# Respond to @replies if they come from the right crowd
#
reply :from => [:cjno, :irbno] do |message, params|
post_tweet “@#{message.sender.screen_name} I agree”
end

# Listen in and log tweets
#
tweet do |message, params|
puts message
end



Tags: Ruby, sinatra, twibot, twitter4r

无觅相关文章插件,快速提升流量