resthome 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -83,6 +83,10 @@ How to use replace query parameters with function arguments.
83
83
  service = LastFmWebService.new 'xxxxxxxxx'
84
84
  service.track 'cher', 'believe'
85
85
 
86
+ == Twilio Example send SMS message
87
+
88
+ TwilioWebService.service.send_sms_message '5551112222', 'Verification Code: 2121'
89
+
86
90
  == Contributing to RESTHome
87
91
 
88
92
  * 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.0
1
+ 0.7.1
@@ -0,0 +1,84 @@
1
+ require 'resthome'
2
+ require 'digest/sha2'
3
+ require 'base64'
4
+
5
+ class AmazonProductWebService < RESTHome
6
+ base_uri 'http://ecs.amazonaws.com'
7
+
8
+ DEFAULT_VERSION = '2009-03-31'
9
+
10
+ @@digest256 = OpenSSL::Digest::Digest.new("sha256")
11
+
12
+ attr_accessor :version, :access_key, :secret, :associate_tag
13
+
14
+ namespace '/onca' do
15
+ route :item_search, '/xml', :query => {'Keywords' => :arg1, 'SearchIndex' => :arg2, 'Operation' => 'ItemSearch', 'Service' => 'AWSECommerceService'} do |res|
16
+ res['ItemSearchResponse']['Items']['Item']
17
+ end
18
+
19
+ route :item_lookup, '/xml', :query => {'ItemId' => :arg1, 'Operation' => 'ItemLookup', 'Service' => 'AWSECommerceService', 'ResponseGroup' => 'Small'} do |res|
20
+ res['ItemLookupResponse']['Items']['Item']
21
+ end
22
+ end
23
+
24
+ def initialize(access_key, secret)
25
+ @access_key = access_key
26
+ @secret = secret
27
+ @version = DEFAULT_VERSION
28
+ @host = URI.parse(self.class.base_uri).host
29
+ end
30
+
31
+ def build_options!(options)
32
+ options[:query] ||= {}
33
+ options[:query]['AWSAccessKeyId'] = @access_key
34
+ options[:query]['Version'] = @version
35
+ options[:query]['Timestamp'] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
36
+ options[:query]['AssociateTag'] = self.associate_tag if self.associate_tag
37
+ end
38
+
39
+ def sign_request(method, path, options)
40
+ signed_query = self.class.sign_request_v2(@secret, options[:query], method, @host, path)
41
+ options.delete :query
42
+ signed_query
43
+ end
44
+
45
+ def aws_request(method, path, options)
46
+ build_options! options
47
+ url = build_url(path)
48
+ signed_query = sign_request(method, path, options)
49
+ if method == :post
50
+ options[:body] = signed_query
51
+ else
52
+ url += "?#{signed_query}"
53
+ end
54
+
55
+ @request_method = method
56
+ @request_url = url
57
+ @request_options = options
58
+
59
+ @response = self.class.send(method, url, options)
60
+ end
61
+
62
+ alias_method :original_request, :request
63
+ alias_method :request, :aws_request
64
+
65
+ # copied from RightAws::AwsUtils
66
+ def self.amz_escape(param)
67
+ param.to_s.gsub(/([^a-zA-Z0-9._~-]+)/n) do
68
+ '%' + $1.unpack('H2' * $1.size).join('%').upcase
69
+ end
70
+ end
71
+
72
+ # copied from RightAws::AwsUtils
73
+ def self.sign_request_v2(aws_secret_access_key, service_hash, http_verb, host, uri)
74
+ canonical_string = service_hash.keys.sort.map do |key|
75
+ "#{self.amz_escape(key)}=#{self.amz_escape(service_hash[key])}"
76
+ end.join('&')
77
+
78
+ string_to_sign = "#{http_verb.to_s.upcase}\n#{host.downcase}\n#{uri}\n#{canonical_string}"
79
+
80
+ signature = self.amz_escape(Base64.encode64(OpenSSL::HMAC.digest(@@digest256, aws_secret_access_key, string_to_sign)).strip)
81
+
82
+ "#{canonical_string}&Signature=#{signature}"
83
+ end
84
+ end
@@ -0,0 +1,31 @@
1
+ require 'resthome'
2
+
3
+ class TwilioWebService < RESTHome
4
+ base_uri 'https://api.twilio.com'
5
+
6
+ namespace '/2010-04-01' do
7
+ route :accounts, '/Accounts'
8
+
9
+ namespace '/Accounts' do
10
+ route :create_sms_message, '/:sid/SMS/Messages', :expected_status => 201 do |res|
11
+ res['TwilioResponse']['SMSMessage']
12
+ end
13
+ end
14
+ end
15
+
16
+ attr_accessor :number
17
+
18
+ def initialize(account_sid, auth_token, number)
19
+ @number = number
20
+ self.basic_auth = {:username => account_sid, :password => auth_token}
21
+ end
22
+
23
+ def self.service
24
+ config = YAML.load_file("#{Rails.root}/config/twilio.yml")
25
+ TwilioWebService.new config['twilio']['sid'], config['twilio']['token'], config['twilio']['number']
26
+ end
27
+
28
+ def send_sms_message(cell_phone, message)
29
+ self.create_sms_message self.basic_auth[:username], {'From' => @number, 'To' => cell_phone, 'Body' => message}
30
+ end
31
+ end
data/lib/resthome.rb CHANGED
@@ -54,7 +54,7 @@ class RESTHome
54
54
  query_args.compact!
55
55
  end
56
56
 
57
- function_args += query_args
57
+ function_args += query_args.map { |a| a.downcase.gsub(/[^a-z0-9_]/, '_').sub(/^\d+/, '') }
58
58
 
59
59
  method = options[:method]
60
60
  expected_status = options[:expected_status]
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.7.0"
8
+ s.version = "0.7.1"
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-12-15}
12
+ s.date = %q{2011-02-21}
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 = [
@@ -24,8 +24,10 @@ Gem::Specification.new do |s|
24
24
  "README.rdoc",
25
25
  "Rakefile",
26
26
  "VERSION",
27
+ "examples/amazon_product_web_service.rb",
27
28
  "examples/chargify_web_service.rb",
28
29
  "examples/last_fm_web_service.rb",
30
+ "examples/twilio_web_service.rb",
29
31
  "examples/wordpress_web_service.rb",
30
32
  "lib/resthome.rb",
31
33
  "resthome.gemspec",
@@ -39,8 +41,10 @@ Gem::Specification.new do |s|
39
41
  s.rubygems_version = %q{1.3.7}
40
42
  s.summary = %q{RESTful web services consumer}
41
43
  s.test_files = [
44
+ "examples/amazon_product_web_service.rb",
42
45
  "examples/chargify_web_service.rb",
43
46
  "examples/last_fm_web_service.rb",
47
+ "examples/twilio_web_service.rb",
44
48
  "examples/wordpress_web_service.rb",
45
49
  "spec/helper.rb",
46
50
  "spec/lib/resthome_spec.rb"
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: 3
4
+ hash: 1
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 0
10
- version: 0.7.0
9
+ - 1
10
+ version: 0.7.1
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-12-15 00:00:00 -05:00
18
+ date: 2011-02-21 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -137,8 +137,10 @@ files:
137
137
  - README.rdoc
138
138
  - Rakefile
139
139
  - VERSION
140
+ - examples/amazon_product_web_service.rb
140
141
  - examples/chargify_web_service.rb
141
142
  - examples/last_fm_web_service.rb
143
+ - examples/twilio_web_service.rb
142
144
  - examples/wordpress_web_service.rb
143
145
  - lib/resthome.rb
144
146
  - resthome.gemspec
@@ -180,8 +182,10 @@ signing_key:
180
182
  specification_version: 3
181
183
  summary: RESTful web services consumer
182
184
  test_files:
185
+ - examples/amazon_product_web_service.rb
183
186
  - examples/chargify_web_service.rb
184
187
  - examples/last_fm_web_service.rb
188
+ - examples/twilio_web_service.rb
185
189
  - examples/wordpress_web_service.rb
186
190
  - spec/helper.rb
187
191
  - spec/lib/resthome_spec.rb