rubytter 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rubytter.rb +35 -15
  2. data/spec/rubytter_spec.rb +43 -1
  3. metadata +2 -2
data/lib/rubytter.rb CHANGED
@@ -16,7 +16,7 @@ class Rubytter
16
16
  end
17
17
  end
18
18
 
19
- VERSION = '0.5.0'
19
+ VERSION = '0.6.0'
20
20
 
21
21
  attr_reader :login
22
22
  attr_accessor :host, :header
@@ -101,31 +101,51 @@ class Rubytter
101
101
  send_direct_message(params.merge({:user => user, :text => text}))
102
102
  end
103
103
 
104
- def search(query, params = {})
105
- path = '/search.json'
106
- param_str = '?' + to_param_str(params.merge({:q => query}))
107
- path = path + param_str unless param_str.empty?
108
- req = create_request(Net::HTTP::Get.new(path), false)
109
- http_request("search.#{@host}", req)
110
- end
111
-
112
104
  def get(path, params = {})
113
105
  path += '.json'
114
106
  param_str = '?' + to_param_str(params)
115
107
  path = path + param_str unless param_str.empty?
116
108
  req = create_request(Net::HTTP::Get.new(path))
117
- http_request(@host, req)
109
+ json_to_struct(http_request(@host, req))
118
110
  end
119
111
 
120
112
  def post(path, params = {})
121
113
  path += '.json'
122
114
  param_str = to_param_str(params)
123
115
  req = create_request(Net::HTTP::Post.new(path))
124
- http_request(@host, req, param_str)
116
+ json_to_struct(http_request(@host, req, param_str))
125
117
  end
126
-
127
118
  alias delete post
128
119
 
120
+ def search(query, params = {})
121
+ path = '/search.json'
122
+ param_str = '?' + to_param_str(params.merge({:q => query}))
123
+ path = path + param_str unless param_str.empty?
124
+ req = create_request(Net::HTTP::Get.new(path), false)
125
+ json_data = http_request("search.#{@host}", req)
126
+ json_to_struct(
127
+ json_data['results'].map do |result|
128
+ search_result_to_struct(result)
129
+ end
130
+ )
131
+ end
132
+
133
+ def search_result_to_struct(json)
134
+ {
135
+ 'id' => json['id'],
136
+ 'text' => json['text'],
137
+ 'source' => CGI.unescapeHTML(json['source']),
138
+ 'in_reply_to_user_id' => json['to_usre_id'],
139
+ 'in_reply_to_screen_name' => json['to_usre'],
140
+ 'in_reply_to_status_id' => nil,
141
+ 'user' => {
142
+ 'id' => json['from_user_id'],
143
+ 'screen_name' => json['from_user'],
144
+ 'profile_image_url' => json['profile_image_url']
145
+ }
146
+ }
147
+ end
148
+
129
149
  def http_request(host, req, param_str = nil)
130
150
  res = @connection.start(host) do |http|
131
151
  if param_str
@@ -134,12 +154,12 @@ class Rubytter
134
154
  http.request(req)
135
155
  end
136
156
  end
137
- struct = json_to_struct(JSON.parse(res.body))
157
+ json_data = JSON.parse(res.body)
138
158
  case res.code
139
159
  when "200"
140
- struct
160
+ json_data
141
161
  else
142
- raise APIError.new(struct.error, res)
162
+ raise APIError.new(json_data['error'], res)
143
163
  end
144
164
  end
145
165
 
@@ -113,6 +113,7 @@ class Rubytter
113
113
  @rubytter.should_receive(:http_request) do |host, req, param_str|
114
114
  req.path.should == '/search.json?q=test'
115
115
  host.should == 'search.twitter.com'
116
+ {'results' => []}
116
117
  end
117
118
  @rubytter.search('test')
118
119
  end
@@ -122,13 +123,13 @@ class Rubytter
122
123
  req.path.should =~ /\/search.json\?/
123
124
  req.path.should =~ /q=test/
124
125
  req.path.should =~ /lang=ja/
126
+ {'results' => []}
125
127
  end
126
128
  @rubytter.search('test', :lang => 'ja')
127
129
  end
128
130
 
129
131
  it 'should respond to to_param_str' do
130
132
  param_str = @rubytter.to_param_str(:page => 2, :foo => 'bar')
131
- p param_str
132
133
  param_str.should =~ /^.+?=.+?&.+?=.+?$/
133
134
  param_str.should =~ /page=2/
134
135
  param_str.should =~ /foo=bar/
@@ -197,5 +198,46 @@ class Rubytter
197
198
  rubytter.should_receive(:__update_status).with({:status => 'test', :source => "Foo"})
198
199
  rubytter.update('test')
199
200
  end
201
+
202
+ it 'should convert search results to struct' do
203
+ json_data = {
204
+ 'id' => '123',
205
+ 'text' => 'foo foo bar bar',
206
+ 'source' => '<a href="http:\/\/twitter.com\/">web<\/a>',
207
+ 'to_usre_id' => '20660692',
208
+ 'to_usre' => 'jugyo_test',
209
+ 'from_user_id' => '3748631',
210
+ 'from_user' => 'jugyo',
211
+ 'profile_image_url' => 'http://s3.amazonaws.com/twitter_production/profile_images/63467667/megane2_normal.png'
212
+ }
213
+ rubytter = Rubytter.new('test', 'teat')
214
+ result = rubytter.search_result_to_struct(json_data)
215
+ result['id'].should == '123'
216
+ result['text'].should == 'foo foo bar bar'
217
+ result['source'].should == "<a href=\"http:\\/\\/twitter.com\\/\">web<\\/a>"
218
+ result['in_reply_to_user_id'].should == '20660692'
219
+ result['in_reply_to_screen_name'].should == 'jugyo_test'
220
+ result['user']['id'].should == '3748631'
221
+ result['user']['screen_name'].should == 'jugyo'
222
+ result['user']['profile_image_url'].should == 'http://s3.amazonaws.com/twitter_production/profile_images/63467667/megane2_normal.png'
223
+ end
224
+
225
+ it 'should work search' do
226
+ json_data = JSON.parse open(File.dirname(__FILE__) + '/search.json').read
227
+
228
+ @rubytter.stub!(:http_request).and_return(json_data)
229
+ statuses = @rubytter.search('termtter')
230
+ status = statuses[0]
231
+
232
+ status.id.should == 1365281728
233
+ status.source.should == "<a href=\"http://twitter.com/\">web</a>"
234
+ status.text.should == "よし、add_hook 呼んでるところが無くなった #termtter"
235
+ status.in_reply_to_user_id.should == nil
236
+ status.in_reply_to_screen_name.should == nil
237
+ status.in_reply_to_status_id.should == nil
238
+ status.user.id.should == 74941
239
+ status.user.screen_name.should == "jugyo"
240
+ status.user.profile_image_url.should == "http://s3.amazonaws.com/twitter_production/profile_images/63467667/megane2_normal.png"
241
+ end
200
242
  end
201
243
  end
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.5.0
4
+ version: 0.6.0
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-03-19 00:00:00 +09:00
12
+ date: 2009-03-21 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency