标签 ruby使用ruby调用twitter服务 
Published on 2007-9-25 16:55:00 by http://www.3code.cn/user/ningle

Published on 2007-9-25 16:55:00 by http://www.3code.cn/user/ningle
// 这里使用了Twitter4R
* v0.2.0,Twitter4R是用ruby写的一个lib,å¯ä»¥ç”¨æ
* ¥è®¿é—®Twitter REST APIs
// Twitter4Rçš„åœ°å€ http://twitter4r.rubyforge.org
1 require('rubygems')
2 gem('twitter4r', '>=0.2.0')
3 require('twitter')
4
5 login = 'mylogin' # change this
6 password = 'mypass' # change this
7 friend = 'myfriend' # change this
8
9 client = Twitter::Client.new(:login => login, :password =>
password)
10 public_timeline = client.timeline_for(:public) do |status|
11 # do something here with each individual status in
timeline that is also returned
12 puts status.text
13 end
14
15 # can also pass a block like above to process each
individual status object returned in timeline
16 friends_timeline = client.timeline_for(:friends)
17
18 # same as above with block if you want
19 friend_timeline = client.timeline_for(:friend, friend)
20
21 # Retrieve the user object (with all public profile
information) for my friend
22 user = Twitter::User.find(friend)
23
24 # If I don't want to be friends any more I "defriend" the
user...
25 user.defriend
26
27 # Now I realize that was a terrible mistake and "befriend"
them...
28 user.befriend
29
30 # At this point I want to send them a private message to let
them know I made a mistake
31 # You can do this in two ways.
32 message = Twitter::Message.create(:client => client, :user
=> user,
33 :text => "I didn't really mean to defriend you. Sorry!")
34 # OR...
35 message = client.message(:post, "I didn't really mean to
defriend you. Sorry!", user)
36
37 # Now I want to post a new status to my own timeline.
38 # This can be done one of two ways...
39 status = Twitter::Status.create(:client => client, :text =>
'Sleeping off the beer from last night')
40 # OR...
41 status = client.status(:post, 'Sleeping off the beer from
last night')
42
43 # Now I realize my mother is on twitter and wouldn't like to
see me talking about beer,
44 # so assuming she doesn't have IM or SMS alerts we can
delete the evidence....
45 client.status(:delete, status)
46
ruby s3上传的客户端 
Published on 2007-6-7 4:33:00 by http://www.3code.cn/user/ningle

Published on 2007-6-7 4:33:00 by http://www.3code.cn/user/ningle
aws/s3指Amazon Web Service Simple Storage Service
需要两个gem,aws/s3和main
gem install aws/s3
gem install main
1 #!/bin/env ruby
2
3 require 'rubygems'
4 require 'main'
5 require 'aws/s3'
6 include AWS::S3
7
8 Main {
9 argument('source_filename') {
10 cast :string
11 description 'source filename to copy to S3'
12 }
13
14 argument('bucket_name') {
15 cast :string
16 description 'bucket to place the file in on S3'
17 }
18
19 option('access_key_id') {
20 argument :optional
21 description 'specify the access_key_id manually'
22 default 'put your access key here if you want'
23 }
24
25 option('secret_access_key') {
26 argument :optional
27 description 'specify the secret key manually'
28 default 'put your secret key here if you want'
29 }
30
31 def run
32 bucket_name = params['bucket_name'].value
33 source_filename = params['source_filename'].value
34
35 Base.establish_connection!(
36 :access_key_id => params['access_key_id'].value,
37 :secret_access_key =>
params['secret_access_key'].value
38 )
39
40 begin
41 Bucket.find(bucket_name)
42 rescue
43 puts "Need to make bucket #{bucket_name}.."
44 Bucket.create(bucket_name)
45
46 # Confirm its existence..
47 Bucket.find(bucket_name)
48 end
49
50 puts "Got bucket.."
51 puts "Uploading #{File.basename(source_filename)}.."
52 S3Object.store(File.basename(source_filename),
open(source_filename), bucket_name)
53 puts "Stored!"
54
55 exit_success!
56 end
57 }
在rails中改变对fieldWithErrors的处理 
Published on 2007-5-30 9:18:00 by http://www.3code.cn/user/ningle

Published on 2007-5-30 9:18:00 by http://www.3code.cn/user/ningle
// 在config/environment.rb中添加
1 ActionView::Base.field_error_proc = Proc.new {|html_tag,
instance|
2 %(<span class="field-with-errors">#{html_tag}</span>)}
用collect组合id字符串进行查询 
Published on 2007-5-23 22:48:00 by http://www.3code.cn/user/ningle

Published on 2007-5-23 22:48:00 by http://www.3code.cn/user/ningle
// 代码描述
1 posts = Post.find(:all)
2 p_ids = posts.collect{|p| p.id}.join(',')
3 replies = Reply.find(:all, {:conditions => ["post_id in
(#{p_ids})"]})
在rails中使用Gruff和显示中文的问题 
Published on 2007-5-22 14:50:00 by http://www.3code.cn/user/ningle

Published on 2007-5-22 14:50:00 by http://www.3code.cn/user/ningle
要在图形上显示中文,需要中文字体,比如sim
* hei.ttf
1 require 'gruff'
2
3 g = Gruff::Line.new
4 g.font = "#{RAILS_ROOT}/public/fonts/simhei.ttf"
5 g.title = "中文"
6
7 g.data("苹果", [1, 2, 3, 4, 4, 3])
8 g.data("橘子", [4, 8, 7, 9, 8, 9])
9 g.data("西瓜", [2, 3, 1, 5, 6, 8])
10 g.data("桃子", [9, 9, 10, 8, 7, 9])
11
12 g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}
13
14 g.write('my_fruity_graph.png')
将rails的datetime_select传递的格式转换成mysql的datetime格式 
Published on 2007-5-20 11:10:00 by http://www.3code.cn/user/ningle

Published on 2007-5-20 11:10:00 by http://www.3code.cn/user/ningle
1 @start_at = params[:event]['start_at(1i)'] + "-" +
params[:event]['start_at(2i)'] + "-" +
params[:event]['start_at(3i)'] + " " +
params[:event]['start_at(4i)'] + ":" +
params[:event]['start_at(5i)'] + ":00"
rails朋友关系的实现 
Published on 2007-5-19 9:44:00 by http://www.3code.cn/user/ningle

Published on 2007-5-19 9:44:00 by http://www.3code.cn/user/ningle
// 朋友关系Friend的model
1 class Friend < User
2 has_and_belongs_to_many :users, :join_table => "friends",
:foreign_key => "to_id", :association_foreign_key =>
"from_id"
3 end
Friend的数据库表
1 CREATE TABLE `friends` (
2 `from_id` int( 11 ) NOT NULL default '0',
3 `to_id` int( 11 ) NOT NULL default '0',
4 PRIMARY KEY ( `from_id` , `to_id` ) ,
5 KEY `fk_fu_from` ( `from_id` ) ,
6 KEY `fk_fu_to` ( `to_id` )
7 ) ENGINE = InnoDB DEFAULT CHARSET = utf8;
在子模型(model)中对父模型(model)进行验证 
Published on 2007-5-11 11:14:00 by http://www.3code.cn/user/ningle

Published on 2007-5-11 11:14:00 by http://www.3code.cn/user/ningle
Book和Page是1:n的关系
book.rb
1 class Book < ActiveRecord::Base
2 has_many :pages
3 end
page.rb
1 class Page < ActiveRecord::Base
2 belongs_to :book
3
4 validates_presence_of :page_id
5
6 # 验证父对象是否有效
7 validate do |page|
8 if page.blank?
9 unless page.book(true)
10 errors.add(:page_id, "must be a valid Book id")
11 end
12 end
13 end
14
15 end
给before_filter传递参数的例子 
Published on 2007-5-11 11:06:00 by http://www.3code.cn/user/ningle

Published on 2007-5-11 11:06:00 by http://www.3code.cn/user/ningle
application.rb中的代码
1 class ApplicationController < ActionController::Base
2
3 def hello(name)
4 "Hello #{name}"
5 end
6
7 end
user_controller.rb中代码
1 class UserController < ApplicationController
2
3 before_filter :only => :index do |u|
4 u.hello('Master')
5 end
6
7 end
一个描述向量的类 
Published on 2007-5-5 15:48:00 by http://www.3code.cn/user/xavier

Published on 2007-5-5 15:48:00 by http://www.3code.cn/user/xavier
1 class Vector
2 attr_reader :x , :y, :mod
3 attr_writer :x, :y
4 #初始化
5 def initialize(x,y)
6 @x = x
7 @y = y
8 @mod = (x**2 + y**2) ** 0.5
9 end
10 #加
11 def +(vector)
12 if vector.class != Vector
13 puts "#{vector}不是一个向量!"
14 else
15 @x += vector.x
16 @y += vector.y
17 puts "(#@x,#@y)"
18 end
19 end
20 #减
21 def -(vector)
22 if vector.class != Vector
23 puts "#{vector}不是一个向量!"
24 else
25 @x -= vector.x
26 @y -= vector.y
27 puts "(#@x,#@y)"
28 end
29 end
30 #是否平行
31 def parallel?(vector)
32 if vector.class != Vector
33 puts "#{vector}不是一个向量!"
34 else
35 puts((vector.x / @x) == (vector.y / @y))
36 end
37 end
38 #与实数的积
39 def multiply(number)
40 if (number.class == Fixnum) or (number.class == Bignum)
or (number.class == Float)
41 @x *= number
42 @y *= number
43 puts "(#@x,#@y)"
44 else
45 puts "#{number}不是一个数字!"
46 end
47 end
48 #数量积
49 def innerproduct(vector)
50 if vector.class != Vector
51 puts "#{vector}不是一个向量!"
52 else
53 puts @x * vector.x + @y * vector.y
54 end
55 end
56 #是否垂直
57 def vertical?(vector)
58 if vector.class != Vector
59 puts "#{vector}不是一个向量!"
60 else
61 puts((@x * vector.x + @y * vector.y) == 0)
62 end
63 end
64 #夹角
65 def inclination(vector)
66 if vector.class != Vector
67 puts "#{vector}不是一个向量!"
68 else
69 puts Math.acos((@x * vector.x + @y * vector.y) / (@mod
* vector.mod))
70 end
71 end
72 #平移
73 def translate(vector)
74 if vector.class != Vector
75 puts "#{vector}不是一个向量!"
76 else
77 @x += vector.x
78 @y += vector.y
79 puts "(#@x,#@y)"
80 end
81 end
82 end
10 items








标签 ruby