asin 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/asin.rb +34 -22
- data/readme.textile +6 -11
- data/test/test_asin.rb +5 -4
- metadata +4 -4
data/lib/asin.rb
CHANGED
@@ -2,47 +2,59 @@ require 'hashie'
|
|
2
2
|
require 'httpclient'
|
3
3
|
require 'crack/xml'
|
4
4
|
require 'cgi'
|
5
|
+
require 'base64'
|
5
6
|
|
6
7
|
module ASIN
|
7
|
-
|
8
|
+
|
8
9
|
class Item
|
9
|
-
|
10
|
+
|
10
11
|
attr_reader :raw
|
11
|
-
|
12
|
+
|
12
13
|
def initialize(hash)
|
13
14
|
@raw = Hashie::Mash.new(hash).ItemLookupResponse.Items.Item
|
14
15
|
end
|
15
|
-
|
16
|
+
|
16
17
|
def title
|
17
18
|
@raw.ItemAttributes.Title
|
18
19
|
end
|
19
|
-
|
20
|
+
|
20
21
|
end
|
21
22
|
|
22
23
|
def configure(options={})
|
23
|
-
# some defaults that make sense
|
24
24
|
@options = {
|
25
|
-
:
|
26
|
-
:
|
27
|
-
:
|
28
|
-
:
|
25
|
+
:host => 'webservices.amazon.com',
|
26
|
+
:path => '/onca/xml',
|
27
|
+
:digest => OpenSSL::Digest::Digest.new('sha256'),
|
28
|
+
:key => '',
|
29
|
+
:secret => '',
|
29
30
|
}
|
30
|
-
@options
|
31
|
+
@options.merge! options
|
31
32
|
end
|
32
|
-
|
33
|
+
|
33
34
|
def lookup(asin, params={})
|
34
|
-
Item.new
|
35
|
+
Item.new(call(params.merge(:Operation => :ItemLookup, :ItemId => asin)))
|
35
36
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
def call(params={})
|
37
|
+
|
38
|
+
def call(params)
|
40
39
|
configure if @options.nil?
|
41
|
-
|
42
|
-
resp = HTTPClient.new.get_content(
|
43
|
-
|
44
|
-
|
45
|
-
|
40
|
+
signed = create_signed_query_string(params)
|
41
|
+
resp = HTTPClient.new.get_content("http://#{@options[:host]}#{@options[:path]}?#{signed}")
|
42
|
+
resp = resp.force_encoding('UTF-8') # shady workaround cause amazon returns bad utf-8 chars
|
43
|
+
Crack::XML.parse(resp)
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_signed_query_string(params) # http://cloudcarpenters.com/blog/amazon_products_api_request_signing/
|
47
|
+
params[:Service] = :AWSECommerceService
|
48
|
+
params[:AWSAccessKeyId] = @options[:key]
|
49
|
+
params[:Timestamp] = Time.now.strftime('%Y-%m-%dT%H:%M:%SZ') # needed for signing
|
50
|
+
|
51
|
+
query = params.map{|key, value| "#{key}=#{CGI.escape(value.to_s)}" }.sort.join('&')
|
52
|
+
|
53
|
+
request_to_sign = "GET\n#{@options[:host]}\n#{@options[:path]}\n#{query}"
|
54
|
+
hmac = OpenSSL::HMAC.digest(@options[:digest], @options[:secret], request_to_sign)
|
55
|
+
|
56
|
+
signature = CGI.escape(Base64.encode64(hmac).chomp)
|
57
|
+
"#{query}&Signature=#{signature}"
|
46
58
|
end
|
47
59
|
|
48
60
|
end
|
data/readme.textile
CHANGED
@@ -1,25 +1,20 @@
|
|
1
1
|
h1. Introduction
|
2
2
|
|
3
|
-
There is already a sophisticated
|
3
|
+
There is already a sophisticated amazon gem out there called "ruby-aaws":http://raa.ruby-lang.org/project/ruby-aws/, but ASIN in comparison is __reaaaaaaaaaly easy to use!__
|
4
4
|
|
5
|
-
It currently just supports the _ItemLookup_ via REST but is easy to extend and understand
|
5
|
+
It currently just supports the _ItemLookup_ via REST but is easy to extend and understand!
|
6
6
|
|
7
7
|
If you want to have a look at "the whole Amazon E-Commerce-API go here":http://docs.amazonwebservices.com/AWSEcommerceService/4-0/.
|
8
8
|
|
9
9
|
The code currently runs only on __Ruby 1.9__ due to encoding issues with the Amazon REST output (if YOU know how to backport this, you are welcome!).
|
10
10
|
|
11
|
-
h2. How-To
|
12
|
-
|
13
|
-
All Amazon APIs require their calls to be signed with your API-Keys.
|
14
|
-
|
15
|
-
Since there is already a "decent Service":http://apisigning.com/ for this, no signing is implemented.
|
16
|
-
|
17
|
-
So you need to create an account at http://apisigning.com/ before using the gem!
|
18
|
-
|
19
11
|
h2. Usage
|
20
12
|
|
21
13
|
<pre>
|
22
|
-
|
14
|
+
require 'asin'
|
15
|
+
include ASIN
|
16
|
+
configure :secret => 'your-secret', :key => 'your-key'
|
17
|
+
item = lookup '1430218150
|
23
18
|
item.title
|
24
19
|
=> Learn Objective-C on the Mac (Learn Series)
|
25
20
|
</pre>
|
data/test/test_asin.rb
CHANGED
@@ -8,9 +8,10 @@ class TestAsin < Test::Unit::TestCase
|
|
8
8
|
end
|
9
9
|
|
10
10
|
# comment in and add your key to test real calls
|
11
|
-
def test_r_type_from_engine
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def test_r_type_from_engine #
|
12
|
+
# @helper.configure :secret => 'your-secret', :key => 'your-key'
|
13
|
+
# p item = @helper.lookup('1430218150')
|
14
|
+
# assert_match(/Learn Objective/, item.title)
|
15
|
+
# end
|
15
16
|
|
16
17
|
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:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
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-
|
18
|
+
date: 2010-07-03 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|