weary 0.7.2 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.gitignore +4 -1
  2. data/.rspec +2 -0
  3. data/.travis.yml +10 -0
  4. data/Gemfile +11 -8
  5. data/Gemfile.lock +49 -53
  6. data/LICENSE +1 -1
  7. data/README.md +134 -208
  8. data/Rakefile +6 -47
  9. data/lib/weary.rb +4 -66
  10. data/lib/weary/adapter.rb +23 -0
  11. data/lib/weary/adapters/net_http.rb +68 -0
  12. data/lib/weary/client.rb +243 -0
  13. data/lib/weary/deferred.rb +35 -0
  14. data/lib/weary/env.rb +32 -0
  15. data/lib/weary/middleware.rb +9 -0
  16. data/lib/weary/middleware/basic_auth.rb +17 -0
  17. data/lib/weary/middleware/content_type.rb +28 -0
  18. data/lib/weary/middleware/oauth.rb +31 -0
  19. data/lib/weary/request.rb +137 -124
  20. data/lib/weary/resource.rb +152 -128
  21. data/lib/weary/response.rb +48 -99
  22. data/lib/weary/route.rb +53 -0
  23. data/lib/weary/version.rb +3 -0
  24. data/spec/spec_helper.rb +4 -56
  25. data/spec/support/shared_examples_for_a_rack_app.rb +70 -0
  26. data/spec/support/shared_examples_for_a_rack_env.rb +14 -0
  27. data/spec/support/shared_examples_for_a_uri.rb +9 -0
  28. data/spec/weary/adapter_spec.rb +26 -0
  29. data/spec/weary/adapters/nethttp_spec.rb +88 -0
  30. data/spec/weary/client_spec.rb +258 -0
  31. data/spec/weary/deferred_spec.rb +35 -0
  32. data/spec/weary/env_spec.rb +12 -0
  33. data/spec/weary/middleware/basic_auth_spec.rb +23 -0
  34. data/spec/weary/middleware/content_type_spec.rb +34 -0
  35. data/spec/weary/middleware/oauth_spec.rb +27 -0
  36. data/spec/weary/request_spec.rb +227 -315
  37. data/spec/weary/resource_spec.rb +233 -233
  38. data/spec/weary/response_spec.rb +82 -159
  39. data/spec/weary/route_spec.rb +72 -0
  40. data/spec/weary_spec.rb +3 -56
  41. data/weary.gemspec +16 -79
  42. metadata +138 -98
  43. data/VERSION +0 -1
  44. data/examples/batch.rb +0 -20
  45. data/examples/repo.rb +0 -16
  46. data/examples/status.rb +0 -26
  47. data/lib/weary/base.rb +0 -124
  48. data/lib/weary/batch.rb +0 -37
  49. data/lib/weary/exceptions.rb +0 -15
  50. data/lib/weary/httpverb.rb +0 -32
  51. data/spec/fixtures/github.yml +0 -11
  52. data/spec/fixtures/twitter.xml +0 -763
  53. data/spec/fixtures/vimeo.json +0 -1
  54. data/spec/weary/base_spec.rb +0 -320
  55. data/spec/weary/batch_spec.rb +0 -71
  56. data/spec/weary/httpverb_spec.rb +0 -25
@@ -1 +0,0 @@
1
- [{"title":"\"The Noises Rest\"","url":"http:\/\/vimeo.com\/2748889","clip_id":"2748889","caption":"We of You Look Nice Today (\"the Talent\") were invited, nay, commissioned to make a thing for a thing - http:\/\/bit.ly\/momedy - to be screened at a big fancy place in New York with a lot of fancy people and some interesting paintings. Okay, it was the MoMA. Perhaps you've heard of it.<br \/>\n<br \/>\nExplain ourselves at http:\/\/youlooknicetoday.com","upload_date":"2009-01-07 07:10:47","thumbnail_small":"http:\/\/images.vimeo.com\/24\/21\/93\/242193881\/242193881_80.jpg","thumbnail_medium":"http:\/\/images.vimeo.com\/24\/21\/93\/242193881\/242193881_100.jpg","thumbnail_large":"http:\/\/images.vimeo.com\/24\/21\/93\/242193881\/242193881_160.jpg","user_name":"lonelysandwich","user_url":"http:\/\/vimeo.com\/lonelysandwich","user_thumbnail_small":"http:\/\/20.media.vimeo.com\/d1\/5\/29\/58\/06\/portrait-29580642.jpg","user_thumbnail_large":"http:\/\/10.media.vimeo.com\/d1\/5\/29\/38\/95\/portrait-29389521.jpg","stats_number_of_likes":"595","stats_number_of_plays":"57039","stats_number_of_comments":78,"duration":"379","width":"504","height":"284","tags":"You Look Nice Today, MoMA, silent film, comedy, foley art"}]
@@ -1,320 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe Weary::Base do
4
- describe 'Class' do
5
-
6
- describe 'Resource Defaults' do
7
- before do
8
- @headers = {"User-Agent" => Weary::UserAgents["Safari 4.0.2 - Mac"]}
9
- end
10
-
11
- it 'sets default headers' do
12
- test = Class.new(Weary::Base)
13
- test.headers @headers
14
- test.instance_variable_get(:@headers).should == @headers
15
- end
16
-
17
- it "sets a domain to be used in default url's" do
18
- test = Class.new(Weary::Base)
19
- test.domain 'http://github.com'
20
- test.instance_variable_get(:@domain).should == 'http://github.com/'
21
- end
22
-
23
- it 'panics when a domain that is not a url is given' do
24
- test = Class.new(Weary::Base)
25
- lambda { test.domain 'foobar' }.should raise_error
26
- end
27
-
28
- it "sets a format to use in default url's" do
29
- test = Class.new(Weary::Base)
30
- test.format(:json)
31
- test.instance_variable_get(:@format).should == :json
32
- end
33
- end
34
-
35
- describe 'Resource Preparation' do
36
- before do
37
- @headers = {"User-Agent" => Weary::UserAgents["Safari 4.0.2 - Mac"]}
38
- prepTest = Class.new(Weary::Base)
39
- prepTest.headers @headers
40
- prepTest.domain 'http://foobar.com'
41
- prepTest.format :xml
42
- @t = prepTest.prepare_resource("test",:get)
43
- end
44
-
45
- it 'prepares a Resource' do
46
- @t.class.should == Weary::Resource
47
- end
48
-
49
- it 'has a name' do
50
- @t.name.should == "test"
51
- end
52
-
53
- it 'has an http method' do
54
- @t.via.should == :get
55
- end
56
-
57
- it 'has headers' do
58
- @t.headers.should == @headers
59
- end
60
-
61
- it 'builds a default url if a domain is provided' do
62
- @t.url.normalize.to_s.should == 'http://foobar.com/test.xml'
63
- end
64
-
65
- it 'builds a default url with a json extension if no format is explicitly named' do
66
- t = Class.new(Weary::Base)
67
- t.domain 'http://foobar.com'
68
- p = t.prepare_resource("test",:get)
69
- p.url.normalize.to_s.should == 'http://foobar.com/test.json'
70
- end
71
-
72
- it 'ignores the url if no domain is provided' do
73
- t = Class.new(Weary::Base).prepare_resource("test",:get)
74
- t.url.should == nil
75
- end
76
-
77
- it 'ignores headers if no headers are defined' do
78
- t = Class.new(Weary::Base).prepare_resource("test",:get)
79
- t.headers.should == nil
80
- end
81
- end
82
-
83
- describe 'Resource Storage' do
84
- before do
85
- @headers = {"User-Agent" => Weary::UserAgents["Safari 4.0.2 - Mac"]}
86
- @restest = Class.new(Weary::Base)
87
- @restest.headers @headers
88
- @restest.domain 'http://foobar.com'
89
- @r = @restest.prepare_resource("test",:get)
90
- end
91
-
92
- it 'has a store for resources' do
93
- @restest.class_variable_defined?(:@@resources).should == true
94
- end
95
-
96
- it 'stores the resource for future use' do
97
- @restest.store_resource(@r).should == @r
98
- @restest.resources.include?(:test).should == true
99
- end
100
- end
101
-
102
- describe 'Resource Construction' do
103
- before do
104
- @contest = Class.new(Weary::Base)
105
- @contest.domain 'http://foobar.com'
106
- end
107
-
108
- it 'prepares a resource to be used' do
109
- r = @contest.build_resource "test", :post
110
- r.name.should == "test"
111
- r.via.should == :post
112
- end
113
-
114
- it 'passes the resource into a block for further refinement' do
115
- r = @contest.build_resource("test", :post) {|res| res.via = :put }
116
- r.name.should == "test"
117
- r.via.should == :put
118
- end
119
-
120
- it 'stores the resource' do
121
- r = @contest.build_resource("test 2", :get)
122
- @contest.resources.include?(:test_2).should == true
123
- end
124
-
125
- it 'builds the method for the resource' do
126
- r = @contest.build_resource("test 3", :get)
127
- @contest.public_method_defined?(:test_3).should == true
128
- end
129
- end
130
-
131
- describe 'Resource Declaration' do
132
- before do
133
- @dectest = Class.new(Weary::Base)
134
- @dectest.domain 'http://foobar.com'
135
- end
136
-
137
- it 'gets a resource' do
138
- r = @dectest.get "get test"
139
- r.via.should == :get
140
- r.name.should == "get_test"
141
- end
142
-
143
- it 'posts a resource' do
144
- r = @dectest.post "post test"
145
- r.via.should == :post
146
- r.name.should == "post_test"
147
- end
148
-
149
- it 'puts a resource' do
150
- r = @dectest.put "put test"
151
- r.via.should == :put
152
- r.name.should == "put_test"
153
- end
154
-
155
- it 'deletes a resource' do
156
- r = @dectest.delete "del test"
157
- r.via.should == :delete
158
- r.name.should == "del_test"
159
- end
160
-
161
- it 'declares a resource' do
162
- r = @dectest.declare "generic test"
163
- r.via.should == :get
164
- r.name.should == "generic_test"
165
- end
166
-
167
- it 'stores the resource' do
168
- @dectest.get "storage test"
169
- @dectest.resources.include?(:storage_test).should == true
170
- end
171
- end
172
-
173
- describe 'Method Building' do
174
- before do
175
- @methtest = Class.new(Weary::Base)
176
- @methtest.domain 'http://foobar.com'
177
-
178
- r = @methtest.prepare_resource("method_test",:get)
179
- @methtest.build_method(r)
180
-
181
- a = @methtest.prepare_resource("authentication_test",:post)
182
- a.authenticates = true
183
- @methtest.build_method(a)
184
-
185
- d = @methtest.prepare_resource("params_test",:post)
186
- d.requires = :id
187
- d.with = [:message, :user]
188
- @methtest.build_method(d)
189
- end
190
-
191
- it 'builds a method with the name of the resource' do
192
- n = @methtest.new
193
- n.respond_to?(:method_test).should == true
194
- end
195
-
196
- it 'forms a Request according to the guidelines of the Resource' do
197
- n = @methtest.new
198
- n.method_test.class.should == Weary::Request
199
- end
200
-
201
- it 'passes in authentication credentials if defined' do
202
- n = @methtest.new
203
- cred = {:username => 'mwunsch', :password => 'secret'}
204
- lambda { n.authentication_test }.should raise_error
205
- n.credentials cred[:username], cred[:password]
206
- n.authentication_test.credentials.should == cred
207
- end
208
-
209
- it 'passes in default parameters if defined' do
210
- n = @methtest.new
211
- defaults = {:id => 1234, :message => "Hello world"}
212
- lambda { n.params_test }.should raise_error
213
- n.defaults = defaults
214
- n.params_test.with.should == defaults.to_params
215
- end
216
-
217
- it 'accepts parameters when given' do
218
- n = @methtest.new
219
- req = n.params_test :id => 1234, :message => "Hello world", :foo => "Bar"
220
- req.with.should == {:id => 1234, :message => "Hello world"}.to_params
221
- req.with.include?("foo").should == false
222
- end
223
-
224
-
225
- end
226
-
227
- end
228
-
229
- describe 'Object' do
230
- before do
231
- @klass = Class.new(Weary::Base)
232
- @klass.domain 'http://foobar.com'
233
- @klass.format :xml
234
- @klass.headers({"User-Agent" => Weary::UserAgents["Safari 4.0.2 - Mac"]})
235
- @klass.get "thing" do |r|
236
- r.requires = :id
237
- r.with = [:message, :user]
238
- end
239
- @klass.post "important" do |r|
240
- r.requires = :id
241
- r.authenticates = true
242
- end
243
- end
244
-
245
- it 'has methods defined by the class' do
246
- obj = @klass.new
247
- obj.respond_to?(:thing).should == true
248
- obj.respond_to?(:important).should == true
249
- end
250
-
251
- it 'can set authentication credentials' do
252
- obj = @klass.new
253
-
254
- obj.credentials "username", "password"
255
- obj.instance_variable_get(:@credentials).should == {:username => "username", :password => "password"}
256
- obj.important(:id => 1234).credentials.should == obj.instance_variable_get(:@credentials)
257
- end
258
-
259
- it 'credentials can be an OAuth access token' do
260
- oauth_consumer = OAuth::Consumer.new("consumer_token","consumer_secret",{:site => 'http://foo.bar'})
261
- oauth_token = OAuth::AccessToken.new(oauth_consumer, "token", "secret")
262
- obj = @klass.new
263
-
264
- obj.credentials oauth_token
265
- obj.instance_variable_get(:@credentials).class.should == OAuth::AccessToken
266
- obj.important(:id => 1234).credentials.should == oauth_token
267
- end
268
-
269
- it 'can set defaults to pass into requests' do
270
- obj = @klass.new
271
-
272
- obj.defaults = {:user => "mwunsch", :message => "hello world"}
273
- obj.defaults.should == {:user => "mwunsch", :message => "hello world"}
274
- obj.thing(:id => 1234).uri.query.should == {:user => "mwunsch", :message => "hello world", :id => 1234}.to_params
275
- end
276
-
277
- it 'has a list of resources' do
278
- obj = @klass.new
279
-
280
- obj.resources == @klass.resources
281
- end
282
-
283
- it 'should keep its resources separate' do
284
- obj = @klass.new
285
-
286
- obj.resources[:foo] = 'bar'
287
- obj.resources.has_key?(:foo).should == true
288
- @klass.resources.has_key?(:foo).should == false
289
- obj.resources.delete(:foo)
290
- end
291
-
292
- it 'is able to rebuild its request method, like a singleton' do
293
- obj1 = @klass.new
294
- require 'pp'
295
-
296
- obj1.resources[:thing].follows = false
297
- obj1.rebuild_method(obj1.resources[:thing])
298
-
299
- obj1.thing(:id => 1234).follows?.should == false
300
- @klass.new.thing(:id => 1234).follows?.should == true
301
- end
302
-
303
- it 'can modify a resource without modifying the resources of its class' do
304
- obj = @klass.new
305
-
306
- obj.modify_resource(:thing) {|r| r.url = "http://bar.foo" }
307
- obj.resources[:thing].url.normalize.to_s.should == "http://bar.foo/"
308
- obj.class.resources[:thing].url.normalize.to_s.should_not == "http://bar.foo/"
309
- end
310
-
311
- it 'modifying a resource rebuilds the method' do
312
- obj = @klass.new
313
-
314
- obj.credentials "username", "password"
315
- obj.modify_resource(:important) {|r| r.follows = false }
316
- obj.important(:id => 1234).follows?.should == false
317
- end
318
-
319
- end
320
- end
@@ -1,71 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe Weary::Batch do
4
- after do
5
- FakeWeb.clean_registry
6
- end
7
-
8
- it 'is a group of requests' do
9
- requests = [ Weary.get('http://twitter.com'),
10
- Weary.get('http://github.com'),
11
- Weary.get('http://vimeo.com'),
12
- Weary.get('http://tumblr.com')]
13
-
14
- test = Weary::Batch.new(requests)
15
- test.requests.should == requests
16
- end
17
-
18
- it 'contains a pool of threads, left empty until processing' do
19
- requests = [ Weary.get('http://twitter.com'),
20
- Weary.get('http://github.com'),
21
- Weary.get('http://vimeo.com'),
22
- Weary.get('http://tumblr.com')]
23
-
24
- test = Weary::Batch.new(requests)
25
- test.pool.blank?.should == true
26
- end
27
-
28
- it 'performs requests in parallel' do
29
- resources = %w[http://twitter.com http://github.com http://vimeo.com http://tumblr.com]
30
- resources.each {|url| FakeWeb.register_uri :get, url, :body => 'Hello world' }
31
-
32
- requests = [ Weary.get('http://twitter.com'),
33
- Weary.get('http://github.com'),
34
- Weary.get('http://vimeo.com'),
35
- Weary.get('http://tumblr.com')]
36
-
37
- test = Weary::Batch.new(requests)
38
- responses = test.perform
39
- responses.size.should == 4
40
- responses.each {|response| response.code.should == 200 }
41
- end
42
-
43
- describe 'Callbacks' do
44
- before do
45
- resources = %w[http://twitter.com http://github.com http://vimeo.com http://tumblr.com]
46
- resources.each {|url| FakeWeb.register_uri :get, url, :body => 'Hello world' }
47
- @requests = [ Weary.get('http://twitter.com'),
48
- Weary.get('http://github.com'),
49
- Weary.get('http://vimeo.com'),
50
- Weary.get('http://tumblr.com')]
51
- end
52
-
53
- it 'stores the on_complete callback' do
54
- test = Weary::Batch.new(@requests)
55
- test.on_complete do
56
- 'hello'
57
- end
58
- test.on_complete.call.should == 'hello'
59
- end
60
-
61
- it 'stores the before_send callback' do
62
- test = Weary::Batch.new(@requests)
63
- test.before_send do
64
- 'hello'
65
- end
66
- test.before_send.call.should == 'hello'
67
- end
68
-
69
-
70
- end
71
- end
@@ -1,25 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe Weary::HTTPVerb do
4
- before do
5
- @test = Weary::HTTPVerb.new("Get")
6
- end
7
-
8
- it 'should set the verb' do
9
- @test.verb.should == "Get"
10
- end
11
-
12
- it 'should normalize' do
13
- @test.normalize.should == :get
14
- end
15
-
16
- it 'should know what net/http class to request with' do
17
- @test.request_class.should == Net::HTTP::Get
18
- end
19
-
20
- it 'freaks out if you give it an unrecognized verb' do
21
- test = Weary::HTTPVerb.new('FooBar')
22
- lambda{ test.request_class }.should raise_error
23
- end
24
-
25
- end