Skip to content
This repository
Browse code

Make Twitter request format configurable

  • Loading branch information...
commit d35d6447b25fa84447ae97558958431fa9f6aa29 1 parent e664502
Erik Michaels-Ober authored October 07, 2010
9  Gemfile.lock
@@ -4,7 +4,7 @@ PATH
4 4
     twitter (0.10.0)
5 5
       addressable (~> 2.2.1)
6 6
       faraday (~> 0.4.6)
7  
-      faraday_middleware (~> 0.1.0)
  7
+      faraday_middleware (~> 0.1.1)
8 8
       hashie (~> 0.4.0)
9 9
       roauth (~> 0.0.3)
10 10
 
@@ -17,16 +17,19 @@ GEM
17 17
     faraday (0.4.6)
18 18
       addressable (>= 2.1.1)
19 19
       rack (>= 1.0.1)
20  
-    faraday_middleware (0.1.0)
  20
+    faraday_middleware (0.1.1)
21 21
       faraday (~> 0.4.5)
22 22
       hashie (~> 0.4.0)
23 23
       multi_json (~> 0.0.4)
  24
+      multi_xml (~> 0.0.1)
24 25
       oauth2 (~> 0.0.13)
  26
+      roauth (~> 0.0.3)
25 27
     hashie (0.4.0)
26 28
     json_pure (1.4.6)
27 29
     mocha (0.9.8)
28 30
       rake
29 31
     multi_json (0.0.4)
  32
+    multi_xml (0.0.1)
30 33
     oauth2 (0.0.13)
31 34
       faraday (~> 0.4.1)
32 35
       multi_json (>= 0.0.4)
@@ -43,7 +46,7 @@ DEPENDENCIES
43 46
   addressable (~> 2.2.1)
44 47
   fakeweb (~> 1.3.0)
45 48
   faraday (~> 0.4.6)
46  
-  faraday_middleware (~> 0.1.0)
  49
+  faraday_middleware (~> 0.1.1)
47 50
   hashie (~> 0.4.0)
48 51
   json_pure (~> 1.4.6)
49 52
   mocha (~> 0.9.8)
10  lib/twitter.rb
@@ -28,6 +28,14 @@ def self.user_agent=(value)
28 28
     @user_agent = value
29 29
   end
30 30
 
  31
+  def self.format
  32
+    @format ||= 'json'
  33
+  end
  34
+
  35
+  def self.format=(value)
  36
+    @format = value
  37
+  end
  38
+
31 39
   def self.api_endpoint
32 40
     api_endpoint = "api.twitter.com/#{Twitter.api_version}"
33 41
     api_endpoint = Addressable::URI.heuristic_parse(api_endpoint).to_s
@@ -45,7 +53,7 @@ def self.api_version
45 53
   def self.api_version=(value)
46 54
     @api_version = value
47 55
   end
48  
-  
  56
+
49 57
   class << self
50 58
     attr_accessor :consumer_key
51 59
     attr_accessor :consumer_secret
189  lib/twitter/base.rb
... ...
@@ -1,6 +1,6 @@
1 1
 module Twitter
2 2
   class Base
3  
-    
  3
+
4 4
     attr_reader :consumer_key, :consumer_secret, :access_key, :access_secret
5 5
 
6 6
     def initialize(options={})
@@ -12,17 +12,17 @@ def initialize(options={})
12 12
 
13 13
     # Options: since_id, max_id, count, page
14 14
     def home_timeline(options={})
15  
-      perform_get("statuses/home_timeline.json", options)
  15
+      perform_get("statuses/home_timeline.#{Twitter.format}", options)
16 16
     end
17 17
 
18 18
     # Options: since_id, max_id, count, page, since
19 19
     def friends_timeline(options={})
20  
-      perform_get("statuses/friends_timeline.json", options)
  20
+      perform_get("statuses/friends_timeline.#{Twitter.format}", options)
21 21
     end
22 22
 
23 23
     # Options: id, user_id, screen_name, since_id, max_id, page, since, count
24 24
     def user_timeline(options={})
25  
-      perform_get("statuses/user_timeline.json", options)
  25
+      perform_get("statuses/user_timeline.#{Twitter.format}", options)
26 26
     end
27 27
 
28 28
     def retweeted_to_user(user_id_or_screen_name, options={})
@@ -32,7 +32,7 @@ def retweeted_to_user(user_id_or_screen_name, options={})
32 32
       when String
33 33
         options.merge!({:screen_name => user_id_or_screen_name})
34 34
       end
35  
-      perform_get("statuses/retweeted_to_user.json", options)
  35
+      perform_get("statuses/retweeted_to_user.#{Twitter.format}", options)
36 36
     end
37 37
 
38 38
     def retweeted_by_user(user_id_or_screen_name, options={})
@@ -42,67 +42,67 @@ def retweeted_by_user(user_id_or_screen_name, options={})
42 42
       when String
43 43
         options.merge!({:screen_name => user_id_or_screen_name})
44 44
       end
45  
-      perform_get("statuses/retweeted_by_user.json", options)
  45
+      perform_get("statuses/retweeted_by_user.#{Twitter.format}", options)
46 46
     end
47 47
 
48 48
     def status(id, options={})
49  
-      perform_get("statuses/show/#{id}.json", options)
  49
+      perform_get("statuses/show/#{id}.#{Twitter.format}", options)
50 50
     end
51 51
 
52 52
     # Options: in_reply_to_status_id
53 53
     def update(status, options={})
54  
-      perform_post("statuses/update.json", {:status => status}.merge(options))
  54
+      perform_post("statuses/update.#{Twitter.format}", {:status => status}.merge(options))
55 55
     end
56 56
 
57 57
     # Options: trim_user, include_entities
58 58
     def status_destroy(id, options={})
59  
-      perform_delete("statuses/destroy/#{id}.json", options)
  59
+      perform_delete("statuses/destroy/#{id}.#{Twitter.format}", options)
60 60
     end
61 61
 
62 62
     # Options: trim_user, include_entities
63 63
     def retweet(id, options={})
64  
-      perform_post("statuses/retweet/#{id}.json", options)
  64
+      perform_post("statuses/retweet/#{id}.#{Twitter.format}", options)
65 65
     end
66 66
 
67 67
     # Options: count
68 68
     def retweets(id, options={})
69  
-      perform_get("statuses/retweets/#{id}.json", options)
  69
+      perform_get("statuses/retweets/#{id}.#{Twitter.format}", options)
70 70
     end
71 71
 
72 72
     # Options: since_id, max_id, count, page
73 73
     def mentions(options={})
74  
-      perform_get("statuses/mentions.json", options)
  74
+      perform_get("statuses/mentions.#{Twitter.format}", options)
75 75
     end
76 76
 
77 77
     # Options: since_id, max_id, count, page
78 78
     def retweeted_by_me(options={})
79  
-      perform_get("statuses/retweeted_by_me.json", options)
  79
+      perform_get("statuses/retweeted_by_me.#{Twitter.format}", options)
80 80
     end
81 81
 
82 82
     # Options: since_id, max_id, count, page
83 83
     def retweeted_to_me(options={})
84  
-      perform_get("statuses/retweeted_to_me.json", options)
  84
+      perform_get("statuses/retweeted_to_me.#{Twitter.format}", options)
85 85
     end
86 86
 
87 87
     # Options: since_id, max_id, count, page
88 88
     def retweets_of_me(options={})
89  
-      perform_get("statuses/retweets_of_me.json", options)
  89
+      perform_get("statuses/retweets_of_me.#{Twitter.format}", options)
90 90
     end
91 91
 
92 92
     # options: count, page, ids_only
93 93
     def retweeters_of(id, options={})
94 94
       ids_only = !!(options.delete(:ids_only))
95  
-      perform_get("statuses/#{id}/retweeted_by#{"/ids" if ids_only}.json", options)
  95
+      perform_get("statuses/#{id}/retweeted_by#{"/ids" if ids_only}.#{Twitter.format}", options)
96 96
     end
97 97
 
98 98
     # Options: id, user_id, screen_name, page
99 99
     def friends(options={})
100  
-      perform_get("statuses/friends.json", options)
  100
+      perform_get("statuses/friends.#{Twitter.format}", options)
101 101
     end
102 102
 
103 103
     # Options: id, user_id, screen_name, page
104 104
     def followers(options={})
105  
-      perform_get("statuses/followers.json", options)
  105
+      perform_get("statuses/followers.#{Twitter.format}", options)
106 106
     end
107 107
 
108 108
     def user(user_id_or_screen_name, options={})
@@ -112,192 +112,198 @@ def user(user_id_or_screen_name, options={})
112 112
       when String
113 113
         options.merge!({:screen_name => user_id_or_screen_name})
114 114
       end
115  
-      perform_get("users/show.json", options)
  115
+      perform_get("users/show.#{Twitter.format}", options)
  116
+    end
  117
+
  118
+    def profile_image(screen_name, options={})
  119
+      connection_with_unparsed_response.get do |request|
  120
+        request.url "users/profile_image/#{screen_name}.#{Twitter.format}", options
  121
+      end.headers["location"]
116 122
     end
117 123
 
118 124
     def users(user_ids_or_screen_names, options={})
119 125
       merge_users_into_options!(Array(user_ids_or_screen_names), options)
120  
-      perform_get("users/lookup.json", options)
  126
+      perform_get("users/lookup.#{Twitter.format}", options)
121 127
     end
122 128
 
123 129
     # Options: page, per_page
124 130
     def user_search(query, options={})
125  
-      perform_get("users/search.json", {:q => query}.merge(options))
  131
+      perform_get("users/search.#{Twitter.format}", {:q => query}.merge(options))
126 132
     end
127 133
 
128 134
     def direct_message(id, options={})
129  
-      perform_get("direct_messages/show/#{id}.json", options)
  135
+      perform_get("direct_messages/show/#{id}.#{Twitter.format}", options)
130 136
     end
131 137
 
132 138
     # Options: since, since_id, page
133 139
     def direct_messages(options={})
134  
-      perform_get("direct_messages.json", options)
  140
+      perform_get("direct_messages.#{Twitter.format}", options)
135 141
     end
136 142
 
137 143
     # Options: since, since_id, page
138 144
     def direct_messages_sent(options={})
139  
-      perform_get("direct_messages/sent.json", options)
  145
+      perform_get("direct_messages/sent.#{Twitter.format}", options)
140 146
     end
141 147
 
142 148
     def direct_message_create(user_id_or_screen_name, text, options={})
143 149
       merge_user_into_options!(user_id_or_screen_name, options)
144 150
       options[:text] = text
145  
-      perform_post("direct_messages/new.json", options)
  151
+      perform_post("direct_messages/new.#{Twitter.format}", options)
146 152
     end
147 153
 
148 154
     def direct_message_destroy(id, options={})
149  
-      perform_delete("direct_messages/destroy/#{id}.json", options)
  155
+      perform_delete("direct_messages/destroy/#{id}.#{Twitter.format}", options)
150 156
     end
151 157
 
152 158
     def friendship(options={})
153  
-      perform_get("friendships/show.json", options)
  159
+      perform_get("friendships/show.#{Twitter.format}", options)
154 160
     end
155 161
     alias :friendship_show :friendship
156 162
 
157 163
     def friendships(user_ids_or_screen_names, options={})
158 164
       merge_users_into_options!(Array(user_ids_or_screen_names), options)
159  
-      perform_get("friendships/lookup.json", options)
  165
+      perform_get("friendships/lookup.#{Twitter.format}", options)
160 166
     end
161 167
 
162 168
     def friendship_create(user_id_or_screen_name, follow=false, options={})
163 169
       merge_user_into_options!(user_id_or_screen_name, options)
164 170
       options[:follow] = follow if follow
165  
-      perform_post("friendships/create.json", options)
  171
+      perform_post("friendships/create.#{Twitter.format}", options)
166 172
     end
167 173
 
168 174
     def friendship_update(user_id_or_screen_name, options={})
169 175
       merge_user_into_options!(user_id_or_screen_name, options)
170  
-      perform_post("friendships/update.json", options)
  176
+      perform_post("friendships/update.#{Twitter.format}", options)
171 177
     end
172 178
 
173 179
     def friendship_destroy(user_id_or_screen_name, options={})
174 180
       merge_user_into_options!(user_id_or_screen_name, options)
175  
-      perform_delete("friendships/destroy.json", options)
  181
+      perform_delete("friendships/destroy.#{Twitter.format}", options)
176 182
     end
177 183
 
178 184
     def friendship_exists?(user_id_or_screen_name_a, user_id_or_screen_name_b)
179  
-      perform_get("friendships/exists.json", {:user_a => user_id_or_screen_name_a, :user_b => user_id_or_screen_name_b})
  185
+      perform_get("friendships/exists.#{Twitter.format}", {:user_a => user_id_or_screen_name_a, :user_b => user_id_or_screen_name_b})
180 186
     end
181 187
 
182 188
     # Options: id, user_id, screen_name
183 189
     def friend_ids(options={})
184  
-      perform_get("friends/ids.json", options)
  190
+      perform_get("friends/ids.#{Twitter.format}", options)
185 191
     end
186 192
 
187 193
     # Options: id, user_id, screen_name
188 194
     def follower_ids(options={})
189  
-      perform_get("followers/ids.json", options)
  195
+      perform_get("followers/ids.#{Twitter.format}", options)
190 196
     end
191 197
 
192 198
     def verify_credentials(options={})
193  
-      perform_get("account/verify_credentials.json", options)
  199
+      perform_get("account/verify_credentials.#{Twitter.format}", options)
194 200
     end
195 201
 
196 202
     # Device must be sms, im or none
197 203
     def update_delivery_device(device, options={})
198 204
       options[:device] = device
199  
-      perform_post("account/update_delivery_device.json", options)
  205
+      perform_post("account/update_delivery_device.#{Twitter.format}", options)
200 206
     end
201 207
 
202 208
     # One or more of the following must be present:
203 209
     #   profile_background_color, profile_text_color, profile_link_color,
204 210
     #   profile_sidebar_fill_color, profile_sidebar_border_color
205 211
     def update_profile_colors(options={})
206  
-      perform_post("account/update_profile_colors.json", options)
  212
+      perform_post("account/update_profile_colors.#{Twitter.format}", options)
207 213
     end
208 214
 
209 215
     # file should respond to #read and #path
210 216
     def update_profile_image(file)
211  
-      perform_post("account/update_profile_image.json", build_multipart_bodies(:image => file))
  217
+      perform_post("account/update_profile_image.#{Twitter.format}", build_multipart_bodies(:image => file))
212 218
     end
213 219
 
214 220
     # file should respond to #read and #path
215 221
     def update_profile_background(file, tile = false)
216  
-      perform_post("account/update_profile_background_image.json", build_multipart_bodies(:image => file).merge(:tile => tile))
  222
+      perform_post("account/update_profile_background_image.#{Twitter.format}", build_multipart_bodies(:image => file).merge(:tile => tile))
217 223
     end
218 224
 
219 225
     # One or more of the following must be present:
220 226
     #   name, email, url, location, description
221 227
     def update_profile(options={})
222  
-      perform_post("account/update_profile.json", options)
  228
+      perform_post("account/update_profile.#{Twitter.format}", options)
223 229
     end
224 230
 
225 231
     def totals(options={})
226  
-      perform_get("account/totals.json", options)
  232
+      perform_get("account/totals.#{Twitter.format}", options)
227 233
     end
228 234
 
229 235
     def settings(options={})
230  
-      perform_get("account/settings.json", options)
  236
+      perform_get("account/settings.#{Twitter.format}", options)
231 237
     end
232 238
 
233 239
     # Options: id, page
234 240
     def favorites(options={})
235  
-      perform_get("favorites.json", options)
  241
+      perform_get("favorites.#{Twitter.format}", options)
236 242
     end
237 243
 
238 244
     def favorite_create(id, options={})
239  
-      perform_post("favorites/create/#{id}.json", options)
  245
+      perform_post("favorites/create/#{id}.#{Twitter.format}", options)
240 246
     end
241 247
 
242 248
     def favorite_destroy(id, options={})
243  
-      perform_delete("favorites/destroy/#{id}.json", options)
  249
+      perform_delete("favorites/destroy/#{id}.#{Twitter.format}", options)
244 250
     end
245 251
 
246 252
     def enable_notifications(user_id_or_screen_name, options={})
247 253
       merge_user_into_options!(user_id_or_screen_name, options)
248  
-      perform_post("notifications/follow.json", options)
  254
+      perform_post("notifications/follow.#{Twitter.format}", options)
249 255
     end
250 256
 
251 257
     def disable_notifications(user_id_or_screen_name, options={})
252 258
       merge_user_into_options!(user_id_or_screen_name, options)
253  
-      perform_post("notifications/leave.json", options)
  259
+      perform_post("notifications/leave.#{Twitter.format}", options)
254 260
     end
255 261
 
256 262
     def block(user_id_or_screen_name, options={})
257 263
       merge_user_into_options!(user_id_or_screen_name, options)
258  
-      perform_post("blocks/create.json")
  264
+      perform_post("blocks/create.#{Twitter.format}")
259 265
     end
260 266
 
261 267
     def unblock(user_id_or_screen_name, options={})
262 268
       merge_user_into_options!(user_id_or_screen_name, options)
263  
-      perform_delete("blocks/destroy.json")
  269
+      perform_delete("blocks/destroy.#{Twitter.format}")
264 270
     end
265 271
 
266 272
     def report_spam(user_id_or_screen_name, options={})
267 273
       merge_user_into_options!(user_id_or_screen_name, options)
268  
-      perform_post("report_spam.json", options)
  274
+      perform_post("report_spam.#{Twitter.format}", options)
269 275
     end
270 276
 
271 277
     def list_create(list_owner_screen_name, name, options={})
272 278
       options[:name] = name
273  
-      perform_post("#{list_owner_screen_name}/lists.json", options)
  279
+      perform_post("#{list_owner_screen_name}/lists.#{Twitter.format}", options)
274 280
     end
275 281
 
276 282
     def list_update(list_owner_screen_name, slug, options={})
277  
-      perform_put("#{list_owner_screen_name}/lists/#{slug}.json", options)
  283
+      perform_put("#{list_owner_screen_name}/lists/#{slug}.#{Twitter.format}", options)
278 284
     end
279 285
 
280 286
     def list_delete(list_owner_screen_name, slug, options={})
281  
-      perform_delete("#{list_owner_screen_name}/lists/#{slug}.json", options)
  287
+      perform_delete("#{list_owner_screen_name}/lists/#{slug}.#{Twitter.format}", options)
282 288
     end
283 289
 
284 290
     def list(list_owner_screen_name, slug, options={})
285  
-      perform_get("#{list_owner_screen_name}/lists/#{slug}.json", options)
  291
+      perform_get("#{list_owner_screen_name}/lists/#{slug}.#{Twitter.format}", options)
286 292
     end
287 293
 
288 294
     def lists_subscribed(options={})
289  
-      perform_get("lists/all.json", options)
  295
+      perform_get("lists/all.#{Twitter.format}", options)
290 296
     end
291 297
 
292 298
     def lists(list_owner_screen_name=nil, options={})
293 299
       path = case list_owner_screen_name
294 300
       when nil
295  
-        "lists.json"
  301
+        "lists.#{Twitter.format}"
296 302
       when Hash
297 303
         options = list_owner_screen_name
298  
-        "lists.json"
  304
+        "lists.#{Twitter.format}"
299 305
       else
300  
-        "#{list_owner_screen_name}/lists.json"
  306
+        "#{list_owner_screen_name}/lists.#{Twitter.format}"
301 307
       end
302 308
       perform_get(path, options)
303 309
     end
@@ -305,12 +311,12 @@ def lists(list_owner_screen_name=nil, options={})
305 311
     def suggestions(category_slug=nil, options={})
306 312
       path = case category_slug
307 313
       when nil
308  
-        "suggestions.json"
  314
+        "suggestions.#{Twitter.format}"
309 315
       when Hash
310 316
         options = category_slug
311  
-        "suggestions.json"
  317
+        "suggestions.#{Twitter.format}"
312 318
       else
313  
-        "users/suggestions/#{category_slug}/members.json"
  319
+        "users/suggestions/#{category_slug}/members.#{Twitter.format}"
314 320
       end
315 321
       perform_get(path, options)
316 322
     end
@@ -318,77 +324,77 @@ def suggestions(category_slug=nil, options={})
318 324
     # :per_page = max number of statues to get at once
319 325
     # :page = which page of tweets you wish to get
320 326
     def list_timeline(list_owner_screen_name, slug, options={})
321  
-      perform_get("#{list_owner_screen_name}/lists/#{slug}/statuses.json", options)
  327
+      perform_get("#{list_owner_screen_name}/lists/#{slug}/statuses.#{Twitter.format}", options)
322 328
     end
323 329
 
324 330
     def memberships(list_owner_screen_name, options={})
325  
-      perform_get("#{list_owner_screen_name}/lists/memberships.json", options)
  331
+      perform_get("#{list_owner_screen_name}/lists/memberships.#{Twitter.format}", options)
326 332
     end
327 333
 
328 334
     def subscriptions(list_owner_screen_name, options={})
329  
-      perform_get("#{list_owner_screen_name}/lists/subscriptions.json", options)
  335
+      perform_get("#{list_owner_screen_name}/lists/subscriptions.#{Twitter.format}", options)
330 336
     end
331 337
 
332 338
     def list_members(list_owner_screen_name, slug, options={})
333  
-      perform_get("#{list_owner_screen_name}/#{slug}/members.json", options)
  339
+      perform_get("#{list_owner_screen_name}/#{slug}/members.#{Twitter.format}", options)
334 340
     end
335 341
 
336 342
     def list_add_member(list_owner_screen_name, slug, new_id, options={})
337 343
       options[:id] = new_id
338  
-      perform_post("#{list_owner_screen_name}/#{slug}/members.json", options)
  344
+      perform_post("#{list_owner_screen_name}/#{slug}/members.#{Twitter.format}", options)
339 345
     end
340 346
 
341 347
     def list_remove_member(list_owner_screen_name, slug, id, options={})
342 348
       options[:id] = id
343  
-      perform_delete("#{list_owner_screen_name}/#{slug}/members.json", options)
  349
+      perform_delete("#{list_owner_screen_name}/#{slug}/members.#{Twitter.format}", options)
344 350
     end
345 351
 
346 352
     def is_list_member?(list_owner_screen_name, slug, user_id_or_screen_name, options={})
347  
-      perform_get("#{list_owner_screen_name}/#{slug}/members/#{user_id_or_screen_name}.json", options).error.nil?
  353
+      perform_get("#{list_owner_screen_name}/#{slug}/members/#{user_id_or_screen_name}.#{Twitter.format}", options).error.nil?
348 354
     end
349 355
 
350 356
     def list_subscribers(list_owner_screen_name, slug, options={})
351  
-      perform_get("#{list_owner_screen_name}/#{slug}/subscribers.json", options)
  357
+      perform_get("#{list_owner_screen_name}/#{slug}/subscribers.#{Twitter.format}", options)
352 358
     end
353 359
 
354 360
     def list_subscribe(list_owner_screen_name, slug, options={})
355  
-      perform_post("#{list_owner_screen_name}/#{slug}/subscribers.json", options)
  361
+      perform_post("#{list_owner_screen_name}/#{slug}/subscribers.#{Twitter.format}", options)
356 362
     end
357 363
 
358 364
     def list_unsubscribe(list_owner_screen_name, slug, options={})
359  
-      perform_delete("#{list_owner_screen_name}/#{slug}/subscribers.json", options)
  365
+      perform_delete("#{list_owner_screen_name}/#{slug}/subscribers.#{Twitter.format}", options)
360 366
     end
361 367
 
362 368
     def blocked_ids(options={})
363  
-      perform_get("blocks/blocking/ids.json", options)
  369
+      perform_get("blocks/blocking/ids.#{Twitter.format}", options)
364 370
     end
365 371
 
366 372
     def blocking(options={})
367  
-      perform_get("blocks/blocking.json", options)
  373
+      perform_get("blocks/blocking.#{Twitter.format}", options)
368 374
     end
369 375
 
370 376
     def saved_searches(options={})
371  
-      perform_get("saved_searches.json", options)
  377
+      perform_get("saved_searches.#{Twitter.format}", options)
372 378
     end
373 379
 
374 380
     def saved_search(id, options={})
375  
-      perform_get("saved_searches/show/#{id}.json", options)
  381
+      perform_get("saved_searches/show/#{id}.#{Twitter.format}", options)
376 382
     end
377 383
 
378 384
     def saved_search_create(options)
379  
-      perform_post("saved_searches/create.json", options)
  385
+      perform_post("saved_searches/create.#{Twitter.format}", options)
380 386
     end
381 387
 
382 388
     def saved_search_destroy(id, options={})
383  
-      perform_delete("saved_searches/destroy/#{id}.json", options)
  389
+      perform_delete("saved_searches/destroy/#{id}.#{Twitter.format}", options)
384 390
     end
385 391
 
386 392
     def related_results(id, options={})
387  
-      perform_get("related_results/show/#{id}.json", options)
  393
+      perform_get("related_results/show/#{id}.#{Twitter.format}", options)
388 394
     end
389 395
 
390 396
     def rate_limit_status(options={})
391  
-      perform_get("account/rate_limit_status.json", options)
  397
+      perform_get("account/rate_limit_status.#{Twitter.format}", options)
392 398
     end
393 399
 
394 400
     protected
@@ -433,17 +439,34 @@ def build_multipart_bodies(parts) self.class.build_multipart_bodies(parts) end
433 439
     private
434 440
 
435 441
     def connection
  442
+      builders = []
  443
+      builders << Faraday::Response::RaiseErrors
  444
+      case Twitter.format.to_s
  445
+      when "json"
  446
+        builders << Faraday::Response::ParseJson
  447
+      when "xml"
  448
+        builders << Faraday::Response::ParseXml
  449
+      end
  450
+      builders << Faraday::Response::Mashify
  451
+      connection_with_builders(builders)
  452
+    end
  453
+
  454
+    def connection_with_unparsed_response
  455
+      builders = []
  456
+      builders << Faraday::Response::RaiseErrors
  457
+      connection_with_builders(builders)
  458
+    end
  459
+
  460
+    def connection_with_builders(builders)
436 461
       headers = {
437 462
         :user_agent => Twitter.user_agent
438 463
       }
439 464
       @connection ||= Faraday::Connection.new(:url => Twitter.api_endpoint, :headers => headers) do |builder|
440 465
         builder.adapter(@adapter || Faraday.default_adapter)
441  
-        builder.use Faraday::Response::RaiseErrors
442  
-        builder.use Faraday::Response::ParseJson
443  
-        builder.use Faraday::Response::Mashify
  466
+        builders.each do |b| builder.use b end
444 467
       end
445 468
     end
446  
-    
  469
+
447 470
     def oauth_header(path, options)
448 471
       oauth_params = {
449 472
         :consumer_key    => self.consumer_key,
16  lib/twitter/geo.rb
@@ -11,19 +11,19 @@ def initialize(options={})
11 11
 
12 12
     def place(place_id, options={})
13 13
       results = connection.get do |request|
14  
-        request.url "id/#{place_id}.json", options
  14
+        request.url "id/#{place_id}.#{Twitter.format}", options
15 15
       end.body
16 16
     end
17 17
 
18 18
     def search(options={})
19 19
       results = connection.get do |request|
20  
-        request.url "search.json", options
  20
+        request.url "search.#{Twitter.format}", options
21 21
       end.body
22 22
     end
23 23
 
24 24
     def reverse_geocode(options={})
25 25
       results = connection.get do |request|
26  
-        request.url "reverse_geocode.json", options
  26
+        request.url "reverse_geocode.#{Twitter.format}", options
27 27
       end.body
28 28
     end
29 29
 
@@ -31,13 +31,21 @@ def self.client; self.new end
31 31
 
32 32
     def_delegators :client, :place, :search, :reverse_geocode
33 33
 
  34
+    private
  35
+
34 36
     def connection
35 37
       headers = {
36 38
         :user_agent => Twitter.user_agent
37 39
       }
38 40
       @connection ||= Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
39 41
         builder.adapter(@adapter || Faraday.default_adapter)
40  
-        builder.use Faraday::Response::ParseJson
  42
+        builder.use Faraday::Response::RaiseErrors
  43
+        case Twitter.format.to_s
  44
+        when "json"
  45
+          builder.use Faraday::Response::ParseJson
  46
+        when "xml"
  47
+          builder.use Faraday::Response::ParseXml
  48
+        end
41 49
         builder.use Faraday::Response::Mashify
42 50
       end
43 51
     end
12  lib/twitter/search.rb
@@ -8,7 +8,7 @@ def initialize(q=nil, options={})
8 8
       @options = options
9 9
       clear
10 10
       containing(q) if q && q.strip != ""
11  
-      @api_endpoint = 'search.twitter.com/search.json'
  11
+      @api_endpoint = "search.twitter.com/search.#{Twitter.format}"
12 12
       @api_endpoint = Addressable::URI.heuristic_parse(@api_endpoint)
13 13
     end
14 14
 
@@ -170,13 +170,21 @@ def fetch_next_page
170 170
       end
171 171
     end
172 172
 
  173
+    private
  174
+
173 175
     def connection
174 176
       headers = {
175 177
         :user_agent => user_agent
176 178
       }
177 179
       @connection ||= Faraday::Connection.new(:url => @api_endpoint.omit(:path), :headers => headers) do |builder|
178 180
         builder.adapter(@adapter || Faraday.default_adapter)
179  
-        builder.use Faraday::Response::ParseJson
  181
+        builder.use Faraday::Response::RaiseErrors
  182
+        case Twitter.format.to_s
  183
+        when "json"
  184
+          builder.use Faraday::Response::ParseJson
  185
+        when "xml"
  186
+          builder.use Faraday::Response::ParseXml
  187
+        end
180 188
         builder.use Faraday::Response::Mashify
181 189
       end
182 190
     end
20  lib/twitter/trends.rb
@@ -12,7 +12,7 @@ def initialize(options={})
@@ -20,7 +20,7 @@ def current(options={})
@@ -28,19 +28,19 @@ def daily(options={})
@@ -48,13 +48,21 @@ def self.client; self.new end
75  lib/twitter/unauthenticated.rb
@@ -10,26 +10,32 @@ def initialize(options={})
10 10
 
11 11
     def firehose(options = {})
12 12
       results = connection.get do |request|
13  
-        request.url "statuses/public_timeline.json", options
  13
+        request.url "statuses/public_timeline.#{Twitter.format}", options
14 14
       end.body
15 15
     end
16 16
 
17 17
     def user(user_id_or_screen_name, options={})
18 18
       merge_user_into_options!(user_id_or_screen_name, options)
19 19
       results = connection.get do |request|
20  
-        request.url "users/show.json", options
  20
+        request.url "users/show.#{Twitter.format}", options
21 21
       end.body
22 22
     end
23 23
 
  24
+    def profile_image(screen_name, options={})
  25
+      connection_with_unparsed_response.get do |request|
  26
+        request.url "users/profile_image/#{screen_name}.#{Twitter.format}", options
  27
+      end.headers["location"]
  28
+    end
  29
+
24 30
     def suggestions(category_slug=nil, options={})
25 31
       path = case category_slug
26 32
       when nil
27  
-        "suggestions.json"
  33
+        "suggestions.#{Twitter.format}"
28 34
       when Hash
29 35
         options = category_slug
30  
-        "suggestions.json"
  36
+        "suggestions.#{Twitter.format}"
31 37
       else
32  
-        "users/suggestions/#{category_slug}/members.json"
  38
+        "users/suggestions/#{category_slug}/members.#{Twitter.format}"
33 39
       end
34 40
       results = connection.get do |request|
35 41
         request.url path, options
@@ -39,48 +45,48 @@ def suggestions(category_slug=nil, options={})
39 45
     def retweeted_to_user(user_id_or_screen_name, options={})
40 46
       merge_user_into_options!(user_id_or_screen_name, options)
41 47
       results = connection.get do |request|
42  
-        request.url "statuses/retweeted_to_user.json", options
  48
+        request.url "statuses/retweeted_to_user.#{Twitter.format}", options
43 49
       end.body
44 50
     end
45 51
 
46 52
     def retweeted_by_user(user_id_or_screen_name, options={})
47 53
       merge_user_into_options!(user_id_or_screen_name, options)
48 54
       results = connection.get do |request|
49  
-        request.url "statuses/retweeted_by_user.json", options
  55
+        request.url "statuses/retweeted_by_user.#{Twitter.format}", options
50 56
       end.body
51 57
     end
52 58
 
53 59
     def status(id, options={})
54 60
       results = connection.get do |request|
55  
-        request.url "statuses/show/#{id}.json", options
  61
+        request.url "statuses/show/#{id}.#{Twitter.format}", options
56 62
       end.body
57 63
     end
58 64
 
59 65
     def friend_ids(user_id_or_screen_name, options={})
60 66
       merge_user_into_options!(user_id_or_screen_name, options)
61 67
       results = connection.get do |request|
62  
-        request.url "friends/ids.json", options
  68
+        request.url "friends/ids.#{Twitter.format}", options
63 69
       end.body
64 70
     end
65 71
 
66 72
     def follower_ids(user_id_or_screen_name, options={})
67 73
       merge_user_into_options!(user_id_or_screen_name, options)
68 74
       results = connection.get do |request|
69  
-        request.url "followers/ids.json", options
  75
+        request.url "followers/ids.#{Twitter.format}", options
70 76
       end.body
71 77
     end
72 78
 
73 79
     def timeline(user_id_or_screen_name, options={})
74 80
       merge_user_into_options!(user_id_or_screen_name, options)
75 81
       results = connection.get do |request|
76  
-        request.url "statuses/user_timeline.json", options
  82
+        request.url "statuses/user_timeline.#{Twitter.format}", options
77 83
       end.body
78 84
     end
79 85
 
80 86
     def lists_subscribed(user_id_or_screen_name, options={})
81 87
       merge_user_into_options!(user_id_or_screen_name, options)
82 88
       results = connection.get do |request|
83  
-        request.url "lists/all.json", options
  89
+        request.url "lists/all.#{Twitter.format}", options
84 90
       end.body
85 91
     end
86 92
 
@@ -88,25 +94,40 @@ def lists_subscribed(user_id_or_screen_name, options={})
88 94
     # :page = which page of tweets you wish to get
89 95
     def list_timeline(list_owner_screen_name, slug, options = {})
90 96
       results = connection.get do |request|
91  
-        request.url "#{list_owner_screen_name}/lists/#{slug}/statuses.json", options
  97
+        request.url "#{list_owner_screen_name}/lists/#{slug}/statuses.#{Twitter.format}", options
92 98
       end.body
93 99
     end
94 100
 
95  
-    def profile_image(screen_name)
96  
-      connection_with_unparsed_response.get do |request|
97  
-        request.url "users/profile_image/#{screen_name}.json"
98  
-      end.headers["location"]
99  
-    end
100  
-    
  101
+    private
  102
+
101 103
     def connection
102  
-      connection_with_builders(Faraday::Response::RaiseErrors, Faraday::Response::ParseJson, Faraday::Response::Mashify)
  104
+      builders = []
  105
+      builders << Faraday::Response::RaiseErrors
  106
+      case Twitter.format.to_s
  107
+      when "json"
  108
+        builders << Faraday::Response::ParseJson
  109
+      when "xml"
  110
+        builders << Faraday::Response::ParseXml
  111
+      end
  112
+      builders << Faraday::Response::Mashify
  113
+      connection_with_builders(builders)
103 114
     end
104 115
 
105 116
     def connection_with_unparsed_response
106  
-      connection_with_builders(Faraday::Response::RaiseErrors, Faraday::Response::Mashify)
  117
+      builders = []
  118
+      builders << Faraday::Response::RaiseErrors
  119
+      connection_with_builders(builders)
107 120
     end
108 121
 
109  
-    private
  122
+    def connection_with_builders(builders)
  123
+      headers = {
  124
+        :user_agent => Twitter.user_agent
  125
+      }
  126
+      Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
  127
+        builder.adapter(@adapter || Faraday.default_adapter)
  128
+        builders.each do |b| builder.use b end
  129
+      end
  130
+    end
110 131
 
111 132
     def merge_user_into_options!(user_id_or_screen_name, options={})
112 133
       case user_id_or_screen_name
@@ -117,16 +138,6 @@ def merge_user_into_options!(user_id_or_screen_name, options={})
117 138
       end
118 139
       options
119 140
     end
120  
-    
121  
-    def connection_with_builders(*builders)
122  
-      headers = {
123  
-        :user_agent => Twitter.user_agent
124  
-      }
125  
-      Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
126  
-        builder.adapter(@adapter || Faraday.default_adapter)
127  
-        builders.each do |b| builder.use b end
128  
-      end
129  
-    end
130 141
 
131 142
   end
132 143
 end
BIN  test/fixtures/n605431196_2079896_558_normal.jpg
11  test/twitter/base_test.rb
... ...
@@ -1,7 +1,7 @@
1 1
 require 'test_helper'
2 2
 
3 3
 class BaseTest < Test::Unit::TestCase
4  
-  
  4
+
5 5
   context "base" do
6 6
     setup do
7 7
       Twitter.configure do |config|
@@ -10,7 +10,7 @@ class BaseTest < Test::Unit::TestCase
10 10
         config.access_key = 'atoken'
11 11
         config.access_secret = 'asecret'
12 12
       end
13  
-      
  13
+
14 14
       @client = Twitter::Base.new
15 15
     end
16 16
 
@@ -18,7 +18,7 @@ class BaseTest < Test::Unit::TestCase
18 18
       should "accept oauth params" do
19 19
         assert_equal @client.consumer_key, 'ctoken'
20 20
       end
21  
-      
  21
+
22 22
       should "override oauth params" do
23 23
         oauth = {
24 24
           :access_key      => "atoken",
@@ -140,6 +140,11 @@ class BaseTest < Test::Unit::TestCase
140 140
         assert @client.user('sferik')
141 141
       end
142 142
 
  143
+      should "get a user's profile image" do
  144
+        stub_get('/1/users/profile_image/ratherchad.json', 'n605431196_2079896_558_normal.jpg', 302, 'http://a3.twimg.com/profile_images/1107413683/n605431196_2079896_558_normal.jpg')
  145
+        assert @client.profile_image('ratherchad')
  146
+      end
  147
+
143 148
       should "get single user with the users method" do
144 149
         stub_get('/1/users/lookup.json?screen_name=sferik', 'array.json')
145 150
         assert @client.users('sferik')
11  test/twitter/unauthenticated_test.rb
@@ -18,6 +18,11 @@ class UnauthenticatedTest < Test::Unit::TestCase
18 18
     assert Twitter.user('sferik')
19 19
   end
20 20
 
  21
+  should "get a user's profile image" do
  22
+    stub_get('/1/users/profile_image/ratherchad.json', 'n605431196_2079896_558_normal.jpg', 302, 'http://a3.twimg.com/profile_images/1107413683/n605431196_2079896_558_normal.jpg')
  23
+    assert Twitter.profile_image('ratherchad')
  24
+  end
  25
+
21 26
   should "get suggestions" do
22 27
     stub_get('/1/suggestions.json', 'array.json')
23 28
     assert Twitter.suggestions
@@ -134,10 +139,4 @@ class UnauthenticatedTest < Test::Unit::TestCase
134 139
       assert Twitter.list_timeline('pengwynn', 'rubyists', {:page => 2, :per_page => 1})
135 140
     end
136 141
   end
137  
-  
138  
-  should "get a user's profile image" do
139  
-    stub_get('/1/users/profile_image/ratherchad.json', 'array.json', nil, 'http://a3.twimg.com/profile_images/1107413683/n605431196_2079896_558_normal.jpg')
140  
-    assert_equal 'http://a3.twimg.com/profile_images/1107413683/n605431196_2079896_558_normal.jpg',  Twitter.profile_image( 'ratherchad' )
141  
-  end
142  
-
143 142
 end
30  test/twitter_test.rb
@@ -3,7 +3,6 @@
3 3
 class TwitterTest < Test::Unit::TestCase
4 4
 
5 5
   should "configure consumer and access keys for easy access" do
6  
-
7 6
     Twitter.configure do |config|
8 7
       config.consumer_key = 'OU812'
9 8
       config.consumer_secret = 'vh5150'
@@ -18,15 +17,16 @@ class TwitterTest < Test::Unit::TestCase
18 17
     assert_equal '8675309', client.access_key
19 18
     assert_equal '8008135', client.access_secret
20 19
   end
21  
-  
  20
+
22 21
   should "default adapter to Faraday.default_adapter" do
23 22
     assert_equal Faraday.default_adapter, Twitter.adapter
24 23
   end
25 24
 
26  
-  context 'when overriding the adapter' do
  25
+  context "when overriding the adapter" do
27 26
     should "be able to specify the adapter" do
28 27
       Twitter.adapter = :typhoeus
29 28
       assert_equal :typhoeus, Twitter.adapter
  29
+      # Reset
30 30
       Twitter.adapter = Faraday.default_adapter
31 31
     end
32 32
   end
@@ -35,10 +35,11 @@ class TwitterTest < Test::Unit::TestCase
35 35
     assert_equal 'Ruby Twitter Gem', Twitter.user_agent
36 36
   end
37 37
 
38  
-  context 'when overriding the user_agent' do
  38
+  context "when overriding the user_agent" do
39 39
     should "be able to specify the user_agent" do
40 40
       Twitter.user_agent = 'My Twitter Gem'
41 41
       assert_equal 'My Twitter Gem', Twitter.user_agent
  42
+      # Reset
42 43
       Twitter.user_agent = 'Ruby Twitter Gem'
43 44
     end
44 45
   end
@@ -47,24 +48,39 @@ class TwitterTest < Test::Unit::TestCase
47 48
     assert_equal "http://api.twitter.com/#{Twitter.api_version}", Twitter.api_endpoint
48 49
   end
49 50
 
50  
-  context 'when overriding the api_endpoint' do
  51
+  context "when overriding the api_endpoint" do
51 52
     should "be able to specify the api_endpoint" do
52 53
       Twitter.api_endpoint = 'tumblr.com'
53 54
       assert_equal 'http://tumblr.com', Twitter.api_endpoint
  55
+      # Reset
54 56
       Twitter.api_endpoint = "api.twitter.com/#{Twitter.api_version}"
55 57
     end
56 58
   end
57 59
 
58  
-  should "default api_version to '1'" do
  60
+  should "default api_version to 1" do
59 61
     assert_equal 1, Twitter.api_version
60 62
   end
61 63
 
62  
-  context 'when overriding the api_version' do
  64
+  context "when overriding the api_version" do
63 65
     should "be able to specify the api_version" do
64 66
       Twitter.api_version = 2
65 67
       assert_equal 2, Twitter.api_version
  68
+      # Reset
66 69
       Twitter.api_version = 1
67 70
     end
68 71
   end
69 72
 
  73
+  should "default format to json" do
  74
+    assert_equal "json", Twitter.format
  75
+  end
  76
+
  77
+  context "when overriding the format" do
  78
+    should "be able to specify the format" do
  79
+      Twitter.format = "xml"
  80
+      assert_equal "xml", Twitter.format
  81
+      # Reset
  82
+      Twitter.format = "json"
  83
+    end
  84
+  end
  85
+
70 86
 end
2  twitter.gemspec
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11 11
   s.add_runtime_dependency("roauth", "~> 0.0.3")
12 12
   s.add_runtime_dependency("addressable", ["~> 2.2.1"])
13 13
   s.add_runtime_dependency("faraday", ["~> 0.4.6"])
14  
-  s.add_runtime_dependency("faraday_middleware", ["~> 0.1.0"])
  14
+  s.add_runtime_dependency("faraday_middleware", ["~> 0.1.1"])
15 15
   s.add_runtime_dependency("hashie", ["~> 0.4.0"])
16 16
   s.authors = ["John Nunemaker", "Wynn Netherland", "Erik Michaels-Ober"]
17 17
   s.description = %q{A Ruby wrapper for the Twitter REST and Search APIs.}

0 notes on commit d35d644

Please sign in to comment.
Something went wrong with that request. Please try again.