resthome 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -60,7 +60,7 @@ Create a simple lorem lipsum generator, using http://www.lipsum.com.
60
60
 
61
61
  == LastFM Example query arguments
62
62
 
63
- How to use replace query parameters with function arguments.
63
+ How to replace query parameters with function arguments.
64
64
 
65
65
  class LastFmWebService < RESTHome
66
66
  base_uri 'http://ws.audioscrobbler.com'
@@ -87,6 +87,42 @@ How to use replace query parameters with function arguments.
87
87
 
88
88
  TwilioWebService.service.send_sms_message '5551112222', 'Verification Code: 2121'
89
89
 
90
+ == Amazon Simple Email Service (SES)
91
+
92
+ How to replace body parameters with function arguments.
93
+
94
+ require 'digest/sha2'
95
+ require 'base64'
96
+
97
+ class AmazonSESService < RESTHome
98
+ base_uri 'https://email.us-east-1.amazonaws.com'
99
+
100
+ @@digest256 = OpenSSL::Digest::Digest.new("sha256")
101
+
102
+ route :verify_email_address, '/', :body => {'Action' => 'VerifyEmailAddress', 'EmailAddress' => :arg1}, :method => :post, :expected_status => 200, :no_body => true do |res|
103
+ res['VerifyEmailAddressResponse']
104
+ end
105
+
106
+ def initialize(access_key, secret)
107
+ @access_key = access_key
108
+ @secret = secret
109
+ end
110
+
111
+ def build_options!(options)
112
+ date = Time.now.getutc.httpdate
113
+ options[:headers] ||= {}
114
+ options[:headers]['Date'] = date
115
+ options[:headers]['X-Amzn-Authorization'] = "AWS3-HTTPS AWSAccessKeyId=#{@access_key},Algorithm=HMACSHA256,Signature=#{AmazonSESService.sign_request(@secret, date)}"
116
+ end
117
+
118
+ def self.sign_request(secret, date)
119
+ Base64.encode64(OpenSSL::HMAC.digest(@@digest256, secret, date)).gsub("\n","")
120
+ end
121
+ end
122
+
123
+ service = AmazonSESService.new 'my-access-key', 'it-s-a-secret'
124
+ service.verify_email_address 'test@test.dev'
125
+
90
126
  == Contributing to RESTHome
91
127
 
92
128
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
1
+ 0.8.0
@@ -0,0 +1,29 @@
1
+ require 'resthome'
2
+ require 'digest/sha2'
3
+ require 'base64'
4
+
5
+ class AmazonSESService < RESTHome
6
+ base_uri 'https://email.us-east-1.amazonaws.com'
7
+
8
+ @@digest256 = OpenSSL::Digest::Digest.new("sha256")
9
+
10
+ route :verify_email_address, '/', :body => {'Action' => 'VerifyEmailAddress', 'EmailAddress' => :arg1}, :method => :post, :expected_status => 200, :no_body => true do |res|
11
+ res['VerifyEmailAddressResponse']
12
+ end
13
+
14
+ def initialize(access_key, secret)
15
+ @access_key = access_key
16
+ @secret = secret
17
+ end
18
+
19
+ def build_options!(options)
20
+ date = Time.now.getutc.httpdate
21
+ options[:headers] ||= {}
22
+ options[:headers]['Date'] = date
23
+ options[:headers]['X-Amzn-Authorization'] = "AWS3-HTTPS AWSAccessKeyId=#{@access_key},Algorithm=HMACSHA256,Signature=#{AmazonSESService.sign_request(@secret, date)}"
24
+ end
25
+
26
+ def self.sign_request(secret, date)
27
+ Base64.encode64(OpenSSL::HMAC.digest(@@digest256, secret, date)).gsub("\n","")
28
+ end
29
+ end
@@ -38,22 +38,17 @@ class RESTHome
38
38
  # Removes the body argument from a post/put route
39
39
  # [:query]
40
40
  # Default set of query arguments
41
+ # [:body]
42
+ # Default set of body arguments
41
43
  def self.route(name, path, options={}, &block)
42
44
  args = path.scan /:[a-z_]+/
43
45
  path = "#{@path_prefix.join if @path_prefix}#{path}"
44
46
  function_args = args.collect{ |arg| arg[1..-1] }
45
47
 
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
48
+ body_args = get_function_args options, :body
49
+ function_args += body_args.map { |a| a.downcase.gsub(/[^a-z0-9_]/, '_').sub(/^\d+/, '') }
56
50
 
51
+ query_args = get_function_args options, :query
57
52
  function_args += query_args.map { |a| a.downcase.gsub(/[^a-z0-9_]/, '_').sub(/^\d+/, '') }
58
53
 
59
54
  method = options[:method]
@@ -96,10 +91,18 @@ class RESTHome
96
91
  method_src << "path.sub! '#{arg}', URI.escape(#{function_args[idx]}.to_s)\n"
97
92
  end
98
93
 
99
- if options[:no_body].nil?
94
+ if options[:no_body]
95
+ if options[:body]
96
+ method_src << "options[:body] = #{options[:body].inspect}.merge(options[:body] || {})\n"
97
+ elsif body_args.size > 0
98
+ method_src << "options[:body] ||= {}\n"
99
+ end
100
+ else
100
101
  if method == 'post' || method == 'put'
101
102
  if options[:resource]
102
103
  method_src << "options[:body] = {'#{options[:resource].to_s}' => body}\n"
104
+ elsif options[:body]
105
+ method_src << "options[:body] = #{options[:body].inspect}.merge(body || {})\n"
103
106
  else
104
107
  method_src << "options[:body] = body\n"
105
108
  end
@@ -112,8 +115,13 @@ class RESTHome
112
115
  method_src << "options[:query] ||= {}\n"
113
116
  end
114
117
 
115
- query_args.each_with_index do |arg, idx|
118
+ body_args.each_with_index do |arg, idx|
116
119
  idx += args.size
120
+ method_src << "options[:body]['#{arg}'] = #{function_args[idx]}\n"
121
+ end
122
+
123
+ query_args.each_with_index do |arg, idx|
124
+ idx += body_args.size + args.size
117
125
  method_src << "options[:query]['#{arg}'] = #{function_args[idx]}\n"
118
126
  end
119
127
 
@@ -338,4 +346,20 @@ class RESTHome
338
346
  obj = yield(obj) if block_given?
339
347
  obj
340
348
  end
349
+
350
+ private
351
+
352
+ def self.get_function_args(options, fld)
353
+ args = []
354
+ if options[fld]
355
+ options[fld].each do |n, v|
356
+ next unless v.is_a?(Symbol)
357
+ idx = v.to_s.gsub(/[^\d]/, '').to_i
358
+ args[idx] = n.to_s
359
+ options[fld].delete n
360
+ end
361
+ args.compact!
362
+ end
363
+ args
364
+ end
341
365
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{resthome}
8
- s.version = "0.7.1"
8
+ s.version = "0.8.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{2011-02-21}
12
+ s.date = %q{2011-03-09}
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 = [
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "examples/amazon_product_web_service.rb",
28
+ "examples/amazon_ses_service.rb",
28
29
  "examples/chargify_web_service.rb",
29
30
  "examples/last_fm_web_service.rb",
30
31
  "examples/twilio_web_service.rb",
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
42
43
  s.summary = %q{RESTful web services consumer}
43
44
  s.test_files = [
44
45
  "examples/amazon_product_web_service.rb",
46
+ "examples/amazon_ses_service.rb",
45
47
  "examples/chargify_web_service.rb",
46
48
  "examples/last_fm_web_service.rb",
47
49
  "examples/twilio_web_service.rb",
@@ -473,4 +473,55 @@ describe RESTHome do
473
473
  @service.request_options[:query]['artist'].should == 'cher'
474
474
  @service.request_options[:query]['track'].should == 'believe'
475
475
  end
476
+
477
+ it "should support body arguments" do
478
+ @service_class.class_eval do
479
+ base_uri 'http://test.dev'
480
+ route :tracks, '/:version/', :body => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}, :method => :post, :no_body => true
481
+ end
482
+ @service = @service_class.new
483
+
484
+ fakeweb_response(:post, %r|http://test.dev/2.0/|, 200, [])
485
+ @service.tracks '2.0', 'cher', 'believe'
486
+
487
+ @service.request_method.should == :post
488
+ @service.request_url.should == 'http://test.dev/2.0/'
489
+ @service.request_options[:body]['method'].should == 'track.getinfo'
490
+ @service.request_options[:body]['artist'].should == 'cher'
491
+ @service.request_options[:body]['track'].should == 'believe'
492
+ end
493
+
494
+ it "should support body arguments" do
495
+ @service_class.class_eval do
496
+ base_uri 'http://test.dev'
497
+ route :tracks, '/:version/', :body => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}, :method => :post
498
+ end
499
+ @service = @service_class.new
500
+
501
+ fakeweb_response(:post, %r|http://test.dev/2.0/|, 200, [])
502
+ @service.tracks '2.0', 'cher', 'believe', 'method' => 'track.getinfo2'
503
+
504
+ @service.request_method.should == :post
505
+ @service.request_url.should == 'http://test.dev/2.0/'
506
+ @service.request_options[:body]['method'].should == 'track.getinfo2'
507
+ @service.request_options[:body]['artist'].should == 'cher'
508
+ @service.request_options[:body]['track'].should == 'believe'
509
+ end
510
+
511
+ it "should support body and query arguments" do
512
+ @service_class.class_eval do
513
+ base_uri 'http://test.dev'
514
+ route :tracks, '/:version/', :body => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}, :query => {'auth' => 'xx', 'Sign' => :arg1}, :method => :post
515
+ end
516
+ @service = @service_class.new
517
+
518
+ fakeweb_response(:post, 'http://test.dev/2.0/?auth=xx&Sign=secret', 200, [])
519
+ @service.tracks '2.0', 'cher', 'believe', 'secret', 'method' => 'track.getinfo2'
520
+
521
+ @service.request_method.should == :post
522
+ @service.request_url.should == 'http://test.dev/2.0/'
523
+ @service.request_options[:body]['method'].should == 'track.getinfo2'
524
+ @service.request_options[:body]['artist'].should == 'cher'
525
+ @service.request_options[:body]['track'].should == 'believe'
526
+ end
476
527
  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: 1
4
+ hash: 63
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 7
9
- - 1
10
- version: 0.7.1
8
+ - 8
9
+ - 0
10
+ version: 0.8.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: 2011-02-21 00:00:00 -05:00
18
+ date: 2011-03-09 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -138,6 +138,7 @@ files:
138
138
  - Rakefile
139
139
  - VERSION
140
140
  - examples/amazon_product_web_service.rb
141
+ - examples/amazon_ses_service.rb
141
142
  - examples/chargify_web_service.rb
142
143
  - examples/last_fm_web_service.rb
143
144
  - examples/twilio_web_service.rb
@@ -183,6 +184,7 @@ specification_version: 3
183
184
  summary: RESTful web services consumer
184
185
  test_files:
185
186
  - examples/amazon_product_web_service.rb
187
+ - examples/amazon_ses_service.rb
186
188
  - examples/chargify_web_service.rb
187
189
  - examples/last_fm_web_service.rb
188
190
  - examples/twilio_web_service.rb