rubytter 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
data/examples/search.rb CHANGED
@@ -3,12 +3,7 @@
3
3
  require 'rubygems'
4
4
  require 'rubytter'
5
5
 
6
- if ARGV.size < 2
7
- puts "Usage: ruby #{File.basename(__FILE__)} user_id password"
8
- exit
9
- end
10
-
11
- client = Rubytter.new(ARGV[0], ARGV[1])
12
- client.search('ruby').results.each do |status|
6
+ client = Rubytter.new
7
+ client.search(ARGV[0] || 'rubytter').results.each do |status|
13
8
  puts "#{status.from_user}: #{status.text}"
14
9
  end
data/lib/rubytter.rb CHANGED
@@ -10,17 +10,17 @@ class Rubytter
10
10
  class APIError < StandardError; end
11
11
 
12
12
  APP_NAME = 'Rubytter'
13
- VERSION = '0.4.6'
13
+ VERSION = '0.4.7'
14
14
  HOMEPAGE = 'http://github.com/jugyo/rubytter'
15
15
 
16
16
  attr_reader :login
17
17
  attr_accessor :host, :header
18
18
 
19
- def initialize(login, password, options = {})
19
+ def initialize(login = nil, password = nil, options = {})
20
20
  @login = login
21
21
  @password = password
22
22
  @host = options[:host] || 'twitter.com'
23
- @header = options[:header] || {'User-Agent', "#{APP_NAME}/#{VERSION} (#{HOMEPAGE})"}
23
+ @header = options[:header] || {'User-Agent' => "#{APP_NAME}/#{VERSION} (#{HOMEPAGE})"}
24
24
  @connection = Connection.new(options)
25
25
  end
26
26
 
@@ -68,13 +68,13 @@ class Rubytter
68
68
  if /%s$/ =~ path
69
69
  eval <<-EOS
70
70
  def #{method}(id, params = {})
71
- #{http_method}(@host, '#{path}' % id, params)
71
+ #{http_method}('#{path}' % id, params)
72
72
  end
73
73
  EOS
74
74
  else
75
75
  eval <<-EOS
76
76
  def #{method}(params = {})
77
- #{http_method}(@host, '#{path}', params)
77
+ #{http_method}('#{path}', params)
78
78
  end
79
79
  EOS
80
80
  end
@@ -88,31 +88,27 @@ class Rubytter
88
88
  send_direct_message(params.merge({:user => user, :text => text}))
89
89
  end
90
90
 
91
- def search(arg)
92
- params = case arg
93
- when String
94
- {:q => arg}
95
- when Hash
96
- arg
97
- end
98
- get("search.#{@host}", '/search', params)
91
+ def search(query, params = {})
92
+ path = '/search.json'
93
+ param_str = '?' + to_param_str(params.merge({:q => query}))
94
+ path = path + param_str unless param_str.empty?
95
+ req = create_request(Net::HTTP::Get.new(path), false)
96
+ http_request("search.#{@host}", req)
99
97
  end
100
98
 
101
- def get(host, path, params = {})
102
- host ||= @host
99
+ def get(path, params = {})
103
100
  path += '.json'
104
101
  param_str = '?' + to_param_str(params)
105
102
  path = path + param_str unless param_str.empty?
106
103
  req = create_request(Net::HTTP::Get.new(path))
107
- http_request(host, req)
104
+ http_request(@host, req)
108
105
  end
109
106
 
110
- def post(host, path, params = {})
111
- host ||= @host
107
+ def post(path, params = {})
112
108
  path += '.json'
113
109
  param_str = to_param_str(params)
114
110
  req = create_request(Net::HTTP::Post.new(path))
115
- http_request(host, req, param_str)
111
+ http_request(@host, req, param_str)
116
112
  end
117
113
 
118
114
  alias delete post
@@ -134,9 +130,9 @@ class Rubytter
134
130
  end
135
131
  end
136
132
 
137
- def create_request(req)
133
+ def create_request(req, basic_auth = true)
138
134
  @header.each {|k, v| req.add_field(k, v) }
139
- req.basic_auth(@login, @password)
135
+ req.basic_auth(@login, @password) if basic_auth
140
136
  req
141
137
  end
142
138
 
@@ -20,87 +20,87 @@ class Rubytter
20
20
 
21
21
  it 'should get or post' do
22
22
  # TODO: split specs
23
- @rubytter.should_receive(:get).with('twitter.com', '/statuses/replies', {})
23
+ @rubytter.should_receive(:get).with('/statuses/replies', {})
24
24
  @rubytter.replies
25
25
 
26
- @rubytter.should_receive(:get).with('twitter.com', '/statuses/replies', {:page => 2})
26
+ @rubytter.should_receive(:get).with('/statuses/replies', {:page => 2})
27
27
  @rubytter.replies(:page => 2)
28
28
 
29
- @rubytter.should_receive(:get).with('twitter.com', '/statuses/user_timeline/1', {})
29
+ @rubytter.should_receive(:get).with('/statuses/user_timeline/1', {})
30
30
  @rubytter.user_timeline(1)
31
31
 
32
- @rubytter.should_receive(:get).with('twitter.com', '/users/show/1', {})
32
+ @rubytter.should_receive(:get).with('/users/show/1', {})
33
33
  @rubytter.user(1)
34
34
 
35
- @rubytter.should_receive(:delete).with('twitter.com', '/statuses/destroy/1', {})
35
+ @rubytter.should_receive(:delete).with('/statuses/destroy/1', {})
36
36
  @rubytter.remove_status(1)
37
37
  end
38
38
 
39
39
  # direct_messages
40
40
 
41
41
  it 'should respond to direct_messages' do
42
- @rubytter.should_receive(:get).with('twitter.com', '/direct_messages', {})
42
+ @rubytter.should_receive(:get).with('/direct_messages', {})
43
43
  @rubytter.direct_messages()
44
44
  end
45
45
 
46
46
  it 'should respond to sent_direct_messages' do
47
- @rubytter.should_receive(:get).with('twitter.com', '/direct_messages/sent', {})
47
+ @rubytter.should_receive(:get).with('/direct_messages/sent', {})
48
48
  @rubytter.sent_direct_messages()
49
49
  end
50
50
 
51
51
  it 'should respond to send_direct_message' do
52
- @rubytter.should_receive(:post).with('twitter.com', '/direct_messages/new', {})
52
+ @rubytter.should_receive(:post).with('/direct_messages/new', {})
53
53
  @rubytter.send_direct_message()
54
54
  end
55
55
 
56
56
  it 'should respond to destroy_direct_message' do
57
- @rubytter.should_receive(:delete).with('twitter.com', '/direct_messages/destroy/1', {})
57
+ @rubytter.should_receive(:delete).with('/direct_messages/destroy/1', {})
58
58
  @rubytter.remove_direct_message(1)
59
59
  end
60
60
 
61
61
  it 'should respond to direct_message' do
62
- @rubytter.should_receive(:post).with('twitter.com', '/direct_messages/new', {:user => 'test', :text => 'aaaaaaaaaaaaa'})
62
+ @rubytter.should_receive(:post).with('/direct_messages/new', {:user => 'test', :text => 'aaaaaaaaaaaaa'})
63
63
  @rubytter.direct_message('test', 'aaaaaaaaaaaaa')
64
64
  end
65
65
 
66
66
  # statuses
67
67
 
68
68
  it 'should respond to update' do
69
- @rubytter.should_receive(:post).with('twitter.com', '/statuses/update', {:status => 'test'})
69
+ @rubytter.should_receive(:post).with('/statuses/update', {:status => 'test'})
70
70
  @rubytter.update('test')
71
71
  end
72
72
 
73
73
  it 'should respond to update_status' do
74
- @rubytter.should_receive(:post).with('twitter.com', '/statuses/update', {:status => 'test'})
74
+ @rubytter.should_receive(:post).with('/statuses/update', {:status => 'test'})
75
75
  @rubytter.update_status(:status => 'test')
76
76
  end
77
77
 
78
78
  # friendship
79
79
 
80
80
  it 'should respond to follow' do
81
- @rubytter.should_receive(:post).with('twitter.com', '/friendships/create/test', {})
81
+ @rubytter.should_receive(:post).with('/friendships/create/test', {})
82
82
  @rubytter.follow('test')
83
83
  end
84
84
 
85
85
  it 'should respond to leave' do
86
- @rubytter.should_receive(:delete).with('twitter.com', '/friendships/destroy/test', {})
86
+ @rubytter.should_receive(:delete).with('/friendships/destroy/test', {})
87
87
  @rubytter.leave('test')
88
88
  end
89
89
 
90
90
  it 'should respond to friendship_exists' do
91
- @rubytter.should_receive(:get).with('twitter.com', '/friendships/exists', {:user_a => 'a', :user_b => 'b'})
91
+ @rubytter.should_receive(:get).with('/friendships/exists', {:user_a => 'a', :user_b => 'b'})
92
92
  @rubytter.friendship_exists(:user_a => 'a', :user_b => 'b')
93
93
  end
94
94
 
95
95
  # Social Graph Methods
96
96
 
97
97
  it 'should respond to followers_ids' do
98
- @rubytter.should_receive(:get).with('twitter.com', '/friends/ids/test', {})
98
+ @rubytter.should_receive(:get).with('/friends/ids/test', {})
99
99
  @rubytter.friends_ids('test')
100
100
  end
101
101
 
102
102
  it 'should respond to followers_ids' do
103
- @rubytter.should_receive(:get).with('twitter.com', '/followers/ids/test', {})
103
+ @rubytter.should_receive(:get).with('/followers/ids/test', {})
104
104
  @rubytter.followers_ids('test')
105
105
  end
106
106
 
@@ -110,11 +110,6 @@ class Rubytter
110
110
  end
111
111
 
112
112
  it 'should respond to search (1)' do
113
- @rubytter.should_receive(:get).with('search.twitter.com', '/search', {:q => 'test'})
114
- @rubytter.search('test')
115
- end
116
-
117
- it 'should respond to search (2)' do
118
113
  @rubytter.should_receive(:http_request) do |host, req, param_str|
119
114
  req.path.should == '/search.json?q=test'
120
115
  host.should == 'search.twitter.com'
@@ -123,17 +118,12 @@ class Rubytter
123
118
  end
124
119
 
125
120
  it 'should respond to search with params (1)' do
126
- @rubytter.should_receive(:get).with('search.twitter.com', "/search", {:q=>"test", :lang=>"ja"})
127
- @rubytter.search(:q => 'test', :lang => 'ja')
128
- end
129
-
130
- it 'should respond to search with params (2)' do
131
121
  @rubytter.should_receive(:http_request) do |host, req, param_str|
132
122
  req.path.should =~ /\/search.json\?/
133
123
  req.path.should =~ /q=test/
134
124
  req.path.should =~ /lang=ja/
135
125
  end
136
- @rubytter.search(:q => 'test', :lang => 'ja')
126
+ @rubytter.search('test', :lang => 'ja')
137
127
  end
138
128
 
139
129
  it 'should respond to to_param_str' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubytter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-22 00:00:00 +09:00
12
+ date: 2009-03-05 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency