asin 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +0 -4
  2. data/lib/asin.rb +18 -6
  3. metadata +4 -4
data/README.rdoc CHANGED
@@ -35,7 +35,3 @@ The gem is tested against 1.9.1 (to be compatible with Heroku Bamboo Stack) and
35
35
  # access the internal data representation (Hashie::Mash)
36
36
  item.raw.ItemAttributes.ListPrice.FormattedPrice
37
37
  => $39.99
38
-
39
- == TODO
40
-
41
- * Logging
data/lib/asin.rb CHANGED
@@ -3,6 +3,7 @@ require 'httpclient'
3
3
  require 'crack/xml'
4
4
  require 'cgi'
5
5
  require 'base64'
6
+ require 'logger'
6
7
 
7
8
  # ASIN (Amazon Simple INterface) is a gem for easy access of the Amazon E-Commerce-API.
8
9
  # It is simple to configure and use. Since it's very small and flexible, it is easy to extend it to your needs.
@@ -76,12 +77,14 @@ module ASIN
76
77
  # [secret] the API secret key
77
78
  # [key] the API access key
78
79
  # [host] the host, which defaults to 'webservices.amazon.com'
80
+ # [logger] a different logger than logging to STDERR
79
81
  #
80
82
  def configure(options={})
81
83
  @options = {
82
84
  :host => 'webservices.amazon.com',
83
85
  :path => '/onca/xml',
84
86
  :digest => OpenSSL::Digest::Digest.new('sha256'),
87
+ :logger => Logger.new(STDERR),
85
88
  :key => '',
86
89
  :secret => '',
87
90
  } if @options.nil?
@@ -110,18 +113,23 @@ module ASIN
110
113
 
111
114
  def call(params)
112
115
  raise "you have to configure ASIN: 'configure :secret => 'your-secret', :key => 'your-key''" if @options.nil?
116
+ log(:debug, "calling with params=#{params}")
113
117
  signed = create_signed_query_string(params)
114
- resp = HTTPClient.new.get_content("http://#{@options[:host]}#{@options[:path]}?#{signed}")
115
- if(resp.respond_to? :force_encoding)
116
- resp = resp.force_encoding('UTF-8') # force utf-8 chars, works only on 1.9 string
117
- end
118
+ url = "http://#{@options[:host]}#{@options[:path]}?#{signed}"
119
+ log(:info, "performing rest call to url='#{url}'")
120
+ resp = HTTPClient.new.get_content(url)
121
+ # force utf-8 chars, works only on 1.9 string
122
+ resp = resp.force_encoding('UTF-8') if resp.respond_to? :force_encoding
123
+ log(:debug, "got response='#{resp}'")
118
124
  Crack::XML.parse(resp)
119
125
  end
120
126
 
121
- def create_signed_query_string(params) # http://cloudcarpenters.com/blog/amazon_products_api_request_signing/
127
+ def create_signed_query_string(params)
128
+ # nice tutorial http://cloudcarpenters.com/blog/amazon_products_api_request_signing/
122
129
  params[:Service] = :AWSECommerceService
123
130
  params[:AWSAccessKeyId] = @options[:key]
124
- params[:Timestamp] = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ') # utc timestamp needed for signing
131
+ # utc timestamp needed for signing
132
+ params[:Timestamp] = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
125
133
 
126
134
  query = params.map{|key, value| "#{key}=#{CGI.escape(value.to_s)}" }.sort.join('&')
127
135
 
@@ -131,5 +139,9 @@ module ASIN
131
139
  signature = CGI.escape(Base64.encode64(hmac).chomp)
132
140
  "#{query}&Signature=#{signature}"
133
141
  end
142
+
143
+ def log(severity, message)
144
+ @options[:logger].send severity, message if @options[:logger]
145
+ end
134
146
 
135
147
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asin
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Peter Schr\xC3\xB6der"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-22 00:00:00 +02:00
18
+ date: 2010-08-06 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency