resthome 0.6.2 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/resthome.rb +32 -6
- data/resthome.gemspec +2 -2
- data/spec/lib/resthome_spec.rb +90 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.3
|
data/lib/resthome.rb
CHANGED
@@ -31,12 +31,14 @@ class RESTHome
|
|
31
31
|
# [:expected_status]
|
32
32
|
# Expected status code of the response, will raise InvalidResponse. Can be an array of codes.
|
33
33
|
# [:return]
|
34
|
-
# The method to call
|
34
|
+
# The method to call, the class to create or a Proc to call before method returns.
|
35
35
|
# [:resource]
|
36
36
|
# The name of the element to return from the response.
|
37
37
|
# [:no_body]
|
38
38
|
# Removes the body argument from a post/put route
|
39
|
-
|
39
|
+
# [:query]
|
40
|
+
# Default set of query arguments
|
41
|
+
def self.route(name, path, options={}, &block)
|
40
42
|
args = path.scan /:[a-z_]+/
|
41
43
|
function_args = args.collect{ |arg| arg[1..-1] }
|
42
44
|
|
@@ -90,6 +92,10 @@ class RESTHome
|
|
90
92
|
end
|
91
93
|
end
|
92
94
|
|
95
|
+
if options[:query]
|
96
|
+
method_src << "options[:query] = #{options[:query].inspect}.merge(options[:query] || {})\n"
|
97
|
+
end
|
98
|
+
|
93
99
|
method_src << "request :#{method}, path, options\n"
|
94
100
|
|
95
101
|
if expected_status
|
@@ -101,8 +107,12 @@ class RESTHome
|
|
101
107
|
end
|
102
108
|
|
103
109
|
return_method = 'nil'
|
104
|
-
if options[:return].nil?
|
105
|
-
|
110
|
+
if options[:return].nil? || options[:return].is_a?(Proc)
|
111
|
+
block ||= options[:return]
|
112
|
+
if block
|
113
|
+
register_route_block name, block
|
114
|
+
return_method = "self.class.route_blocks['#{name}']"
|
115
|
+
end
|
106
116
|
elsif options[:return].is_a?(Class)
|
107
117
|
return_method = options[:return].to_s
|
108
118
|
else
|
@@ -249,8 +259,8 @@ class RESTHome
|
|
249
259
|
# Parse an array of Set-cookie headers
|
250
260
|
def save_cookies(data)
|
251
261
|
@cookies ||= {}
|
252
|
-
data.collect { |cookie| cookie.split("\; ")[0].split('=') }.each do |c|
|
253
|
-
@cookies[c[0]] = c[1]
|
262
|
+
data.delete_if{ |c| c.blank? }.collect { |cookie| parts = cookie.split("\; "); parts[0] ? parts[0].split('=') : nil }.each do |c|
|
263
|
+
@cookies[c[0].strip] = c[1].strip if c && c[0] && c[1]
|
254
264
|
end
|
255
265
|
end
|
256
266
|
|
@@ -260,6 +270,20 @@ class RESTHome
|
|
260
270
|
save_cookies!
|
261
271
|
end
|
262
272
|
|
273
|
+
def self.route_blocks #:nodoc:
|
274
|
+
{}
|
275
|
+
end
|
276
|
+
|
277
|
+
def self.register_route_block(route, proc) #:nodoc:
|
278
|
+
blocks = self.route_blocks
|
279
|
+
blocks[route.to_s] = proc
|
280
|
+
|
281
|
+
sing = class << self; self; end
|
282
|
+
sing.send :define_method, :route_blocks do
|
283
|
+
blocks
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
263
287
|
protected
|
264
288
|
|
265
289
|
def _handle_response(response, opts={}, &block) #:nodoc:
|
@@ -277,6 +301,8 @@ class RESTHome
|
|
277
301
|
if opts[:return]
|
278
302
|
if opts[:return].is_a?(Class)
|
279
303
|
obj = opts[:return].new obj
|
304
|
+
elsif opts[:return].is_a?(Proc)
|
305
|
+
obj = opts[:return].call obj
|
280
306
|
else
|
281
307
|
obj = send opts[:return], obj
|
282
308
|
end
|
data/resthome.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{resthome}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Doug Youch"]
|
12
|
-
s.date = %q{2010-11-
|
12
|
+
s.date = %q{2010-11-30}
|
13
13
|
s.description = %q{Simple wrapper class generator for consuming RESTful web services}
|
14
14
|
s.email = %q{doug@cykod.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/lib/resthome_spec.rb
CHANGED
@@ -314,4 +314,94 @@ describe RESTHome do
|
|
314
314
|
@service.request_url.should == 'http://test.dev/products.json'
|
315
315
|
@service.request_options.should == {}
|
316
316
|
end
|
317
|
+
|
318
|
+
it "should be able to use blocks on the response" do
|
319
|
+
@service_class.route :products, '/products.json', :resource => 'product', :return => Proc.new { |res| res['name'] }
|
320
|
+
@service_class.method_defined?(:products).should be_true
|
321
|
+
|
322
|
+
@service = @service_class.new
|
323
|
+
@service.base_uri = 'http://test.dev'
|
324
|
+
|
325
|
+
fakeweb_response(:get, 'http://test.dev/products.json', 200,
|
326
|
+
[{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
|
327
|
+
@names = @service.products
|
328
|
+
|
329
|
+
@names.should == ['Item1', 'Item2']
|
330
|
+
@service.request_method.should == :get
|
331
|
+
@service.request_url.should == 'http://test.dev/products.json'
|
332
|
+
@service.request_options.should == {}
|
333
|
+
end
|
334
|
+
|
335
|
+
it "should be able to use blocks on the response" do
|
336
|
+
@service_class.route :products, '/products.json', :resource => 'product', :return => lambda { |res| res['name'] }
|
337
|
+
@service_class.method_defined?(:products).should be_true
|
338
|
+
|
339
|
+
@service = @service_class.new
|
340
|
+
@service.base_uri = 'http://test.dev'
|
341
|
+
|
342
|
+
fakeweb_response(:get, 'http://test.dev/products.json', 200,
|
343
|
+
[{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
|
344
|
+
@names = @service.products
|
345
|
+
|
346
|
+
@names.should == ['Item1', 'Item2']
|
347
|
+
@service.request_method.should == :get
|
348
|
+
@service.request_url.should == 'http://test.dev/products.json'
|
349
|
+
@service.request_options.should == {}
|
350
|
+
end
|
351
|
+
|
352
|
+
it "should be able to use blocks on the response" do
|
353
|
+
@service_class.route :products, '/products.json', :resource => 'product' do |res|
|
354
|
+
res['name']
|
355
|
+
end
|
356
|
+
@service_class.method_defined?(:products).should be_true
|
357
|
+
|
358
|
+
@service = @service_class.new
|
359
|
+
@service.base_uri = 'http://test.dev'
|
360
|
+
|
361
|
+
fakeweb_response(:get, 'http://test.dev/products.json', 200,
|
362
|
+
[{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
|
363
|
+
@names = @service.products
|
364
|
+
|
365
|
+
@names.should == ['Item1', 'Item2']
|
366
|
+
@service.request_method.should == :get
|
367
|
+
@service.request_url.should == 'http://test.dev/products.json'
|
368
|
+
@service.request_options.should == {}
|
369
|
+
end
|
370
|
+
|
371
|
+
it "should be able to parse cookies" do
|
372
|
+
@service = RESTHome.new
|
373
|
+
@service.save_cookies [nil, '', "test=1111; path=/; expires=Sat, 30-Nov-2002 00:00:00 GMT", "dev=1", "missing="]
|
374
|
+
@service.cookies['test'].should == '1111'
|
375
|
+
@service.cookies['dev'].should == '1'
|
376
|
+
@service.cookies['missing'].should be_nil
|
377
|
+
end
|
378
|
+
|
379
|
+
it "should be able to setup default query parameters" do
|
380
|
+
@service_class.route :find_songs, '/songs.json', :query => {'search' => 'all'}
|
381
|
+
@service_class.method_defined?(:find_songs).should be_true
|
382
|
+
|
383
|
+
@service = @service_class.new
|
384
|
+
@service.base_uri = 'http://test.dev'
|
385
|
+
|
386
|
+
fakeweb_response(:get, 'http://test.dev/songs.json?search=all', 200, [])
|
387
|
+
@names = @service.find_songs
|
388
|
+
|
389
|
+
@service.request_method.should == :get
|
390
|
+
@service.request_url.should == 'http://test.dev/songs.json'
|
391
|
+
@service.request_options.should == {:query => {'search' => 'all'}}
|
392
|
+
|
393
|
+
fakeweb_response(:get, 'http://test.dev/songs.json?search=test', 200, [])
|
394
|
+
@names = @service.find_songs_by_search 'test'
|
395
|
+
|
396
|
+
@service.request_method.should == :get
|
397
|
+
@service.request_url.should == 'http://test.dev/songs.json'
|
398
|
+
@service.request_options.should == {:query => {'search' => 'test'}}
|
399
|
+
|
400
|
+
fakeweb_response(:get, 'http://test.dev/songs.json?search=test2', 200, [])
|
401
|
+
@names = @service.find_songs :query => {'search' => 'test2'}
|
402
|
+
|
403
|
+
@service.request_method.should == :get
|
404
|
+
@service.request_url.should == 'http://test.dev/songs.json'
|
405
|
+
@service.request_options.should == {:query => {'search' => 'test2'}}
|
406
|
+
end
|
317
407
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resthome
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 3
|
10
|
+
version: 0.6.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Doug Youch
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-30 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|