resthome 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -48,13 +48,9 @@ Create a simple lorem lipsum generator, using http://www.lipsum.com.
48
48
 
49
49
 
50
50
  class LoremLipsumService < RESTHome
51
- route :generate, '/feed/json', :method => :post, :return => :parse_feed
51
+ base_uri 'http://www.lipsum.com'
52
52
 
53
- def initialize
54
- self.base_uri = 'http://www.lipsum.com'
55
- end
56
-
57
- def parse_feed(res)
53
+ route :generate, '/feed/json', :method => :post do |res|
58
54
  res['feed']['lipsum']
59
55
  end
60
56
  end
@@ -62,6 +58,30 @@ Create a simple lorem lipsum generator, using http://www.lipsum.com.
62
58
  service = LoremLipsumService.new
63
59
  words = service.generate(:what => 'words', :amount => 20)
64
60
 
61
+ == LastFM Example query arguments
62
+
63
+ How to use replace query parameters with function arguments.
64
+
65
+ class LastFmWebService < RESTHome
66
+ base_uri 'http://ws.audioscrobbler.com'
67
+
68
+ namespace '/2.0' do
69
+ route :track, '/', :query => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}, :resource => 'track'
70
+ end
71
+
72
+ def initialize(api_key)
73
+ @api_key = api_key
74
+ end
75
+
76
+ def build_options!(options)
77
+ options[:query] ||= {}
78
+ options[:query]['format'] = 'json'
79
+ options[:query]['api_key'] = @api_key
80
+ end
81
+ end
82
+
83
+ service = LastFmWebService.new 'xxxxxxxxx'
84
+ service.track 'cher', 'believe'
65
85
 
66
86
  == Contributing to RESTHome
67
87
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.4
1
+ 0.7.0
@@ -50,4 +50,6 @@ class ChargifyWebService < RESTHome
50
50
  super
51
51
  options[:body] = options[:body].to_json if options[:body]
52
52
  end
53
+
54
+ def parse_response!; end
53
55
  end
@@ -1,30 +1,28 @@
1
1
  require 'resthome'
2
2
 
3
3
  class LastFmWebService < RESTHome
4
- route :find_albums, '/2.0/', :query => {'method' => 'library.getalbums'}, :resource => 'albums'
5
- # find_albums_by_user
4
+ base_uri 'http://ws.audioscrobbler.com'
6
5
 
7
- route :find_neighbors, '/2.0/', :query => {'method' => 'user.getneighbours'} do |res|
8
- res['neighbours']['user']
9
- end
10
- # find_neighbors_by_user
6
+ namespace '/2.0' do
7
+ route :user_neighbors, '/', :query => {'method' => 'user.getneighbours', 'user' => :arg1} do |res|
8
+ res['neighbours']['user']
9
+ end
11
10
 
12
- route :find_top_tracks, '/2.0/', :query => {'method' => 'user.gettoptracks'} do |res|
13
- res['track']['track']
14
- end
15
- # find_top_tracks_by_user
11
+ route :track, '/', :query => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}, :resource => 'track'
12
+
13
+ route :user_albums, '/', :query => {'method' => 'library.getalbums', 'user' => :arg1}, :resource => 'albums'
16
14
 
17
- route :find_track, '/2.0/', :query => {'method' => 'track.getinfo'}, :resource => 'track'
18
- # find_track_by_artist_and_track
15
+ route :user_top_albums, '/', :query => {'method' => 'user.gettopalbums', 'user' => :arg1} do |res|
16
+ res['topalbums']['album']
17
+ end
19
18
 
20
- route :find_top_albums, '/2.0/', :query => {'method' => 'user.gettopalbums'} do |res|
21
- res['topalbums']['album']
19
+ route :user_top_tracks, '/', :query => {'method' => 'user.gettoptracks', 'user' => :arg1} do |res|
20
+ res['toptracks']['track']
21
+ end
22
22
  end
23
- # find_top_albums_by_user
24
23
 
25
24
  def initialize(api_key)
26
25
  @api_key = api_key
27
- self.base_uri = "http://ws.audioscrobbler.com"
28
26
  end
29
27
 
30
28
  def build_options!(options)
data/lib/resthome.rb CHANGED
@@ -40,8 +40,22 @@ class RESTHome
40
40
  # Default set of query arguments
41
41
  def self.route(name, path, options={}, &block)
42
42
  args = path.scan /:[a-z_]+/
43
+ path = "#{@path_prefix.join if @path_prefix}#{path}"
43
44
  function_args = args.collect{ |arg| arg[1..-1] }
44
45
 
46
+ query_args = []
47
+ if options[:query]
48
+ options[:query].each do |n, v|
49
+ next unless v.is_a?(Symbol)
50
+ idx = v.to_s.gsub(/[^\d]/, '').to_i
51
+ query_args[idx] = n.to_s
52
+ options[:query].delete n
53
+ end
54
+ query_args.compact!
55
+ end
56
+
57
+ function_args += query_args
58
+
45
59
  method = options[:method]
46
60
  expected_status = options[:expected_status]
47
61
  if method.nil?
@@ -79,7 +93,7 @@ class RESTHome
79
93
  METHOD
80
94
 
81
95
  args.each_with_index do |arg, idx|
82
- method_src << "path.sub! '#{arg}', #{function_args[idx]}.to_s\n"
96
+ method_src << "path.sub! '#{arg}', URI.escape(#{function_args[idx]}.to_s)\n"
83
97
  end
84
98
 
85
99
  if options[:no_body].nil?
@@ -94,6 +108,13 @@ class RESTHome
94
108
 
95
109
  if options[:query]
96
110
  method_src << "options[:query] = #{options[:query].inspect}.merge(options[:query] || {})\n"
111
+ elsif query_args.size > 0
112
+ method_src << "options[:query] ||= {}\n"
113
+ end
114
+
115
+ query_args.each_with_index do |arg, idx|
116
+ idx += args.size
117
+ method_src << "options[:query]['#{arg}'] = #{function_args[idx]}\n"
97
118
  end
98
119
 
99
120
  method_src << "request :#{method}, path, options\n"
@@ -141,6 +162,13 @@ class RESTHome
141
162
  self.class.route name, path, options.merge(:instance => self)
142
163
  end
143
164
 
165
+ def self.namespace(path_prefix)
166
+ @path_prefix ||= []
167
+ @path_prefix.push path_prefix
168
+ yield
169
+ @path_prefix.pop
170
+ end
171
+
144
172
  # Creates routes for a RESTful API
145
173
  #
146
174
  # *resource_name* is the name of the items returned by the API,
@@ -180,7 +208,7 @@ class RESTHome
180
208
 
181
209
  # Creates the url
182
210
  def build_url(path)
183
- "#{self.base_uri}#{path}"
211
+ "#{self.base_uri || self.class.base_uri}#{path}"
184
212
  end
185
213
 
186
214
  # Adds the basic_auth and cookie options
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.4"
8
+ s.version = "0.7.0"
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-30}
12
+ s.date = %q{2010-12-15}
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 = [
@@ -404,4 +404,73 @@ describe RESTHome do
404
404
  @service.request_url.should == 'http://test.dev/songs.json'
405
405
  @service.request_options.should == {:query => {'search' => 'test2'}}
406
406
  end
407
+
408
+ it "namespaces for convenience" do
409
+ @service_class.class_eval do
410
+ base_uri 'http://test.dev'
411
+
412
+ namespace '/users/1' do
413
+ route :find_songs, '/songs.json'
414
+ route :find_albums, '/albums.json'
415
+ namespace '/videos' do
416
+ route :find_videos, '/search.json'
417
+ end
418
+ route :find_tracks, '/tracks.json'
419
+ end
420
+ route :tracks, '/tracks.json'
421
+ end
422
+
423
+ @service_class.method_defined?(:find_songs).should be_true
424
+ @service_class.method_defined?(:find_albums).should be_true
425
+ @service_class.method_defined?(:tracks).should be_true
426
+
427
+ @service = @service_class.new
428
+
429
+ fakeweb_response(:get, 'http://test.dev/users/1/songs.json', 200, [])
430
+ @service.find_songs
431
+
432
+ @service.request_method.should == :get
433
+ @service.request_url.should == 'http://test.dev/users/1/songs.json'
434
+
435
+ fakeweb_response(:get, 'http://test.dev/users/1/albums.json', 200, [])
436
+ @service.find_albums
437
+
438
+ @service.request_method.should == :get
439
+ @service.request_url.should == 'http://test.dev/users/1/albums.json'
440
+
441
+ fakeweb_response(:get, 'http://test.dev/users/1/tracks.json', 200, [])
442
+ @service.find_tracks
443
+
444
+ @service.request_method.should == :get
445
+ @service.request_url.should == 'http://test.dev/users/1/tracks.json'
446
+
447
+ fakeweb_response(:get, 'http://test.dev/users/1/videos/search.json', 200, [])
448
+ @service.find_videos
449
+
450
+ @service.request_method.should == :get
451
+ @service.request_url.should == 'http://test.dev/users/1/videos/search.json'
452
+
453
+ fakeweb_response(:get, 'http://test.dev/tracks.json', 200, [])
454
+ @service.tracks
455
+
456
+ @service.request_method.should == :get
457
+ @service.request_url.should == 'http://test.dev/tracks.json'
458
+ end
459
+
460
+ it "should support query arguments" do
461
+ @service_class.class_eval do
462
+ base_uri 'http://test.dev'
463
+ route :tracks, '/:version/', :query => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}
464
+ end
465
+ @service = @service_class.new
466
+
467
+ fakeweb_response(:get, %r|http://test.dev/2.0/|, 200, [])
468
+ @service.tracks '2.0', 'cher', 'believe'
469
+
470
+ @service.request_method.should == :get
471
+ @service.request_url.should == 'http://test.dev/2.0/'
472
+ @service.request_options[:query]['method'].should == 'track.getinfo'
473
+ @service.request_options[:query]['artist'].should == 'cher'
474
+ @service.request_options[:query]['track'].should == 'believe'
475
+ end
407
476
  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: 15
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 4
10
- version: 0.6.4
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
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-30 00:00:00 -05:00
18
+ date: 2010-12-15 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency