asin 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/asin.rb +48 -0
- data/readme.textile +25 -0
- data/test/test_asin.rb +16 -0
- data/test/test_helper.rb +5 -0
- metadata +120 -0
data/lib/asin.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'httpclient'
|
3
|
+
require 'crack/xml'
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
module ASIN
|
7
|
+
|
8
|
+
class Item
|
9
|
+
|
10
|
+
attr_reader :raw
|
11
|
+
|
12
|
+
def initialize(hash)
|
13
|
+
@raw = Hashie::Mash.new(hash).ItemLookupResponse.Items.Item
|
14
|
+
end
|
15
|
+
|
16
|
+
def title
|
17
|
+
@raw.ItemAttributes.Title
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def configure(options={})
|
23
|
+
# some defaults that make sense
|
24
|
+
@options = {
|
25
|
+
:Service => :AWSECommerceService,
|
26
|
+
:XMLEscaping => :Double, # => :Single, :Double
|
27
|
+
:ContentType => 'text/plain;charset="UTF-8"', # trying to force utf-8
|
28
|
+
:ResponseGroup => :Small, # => :Small, :Medium, :Large
|
29
|
+
}
|
30
|
+
@options = @options.merge options
|
31
|
+
end
|
32
|
+
|
33
|
+
def lookup(asin, params={})
|
34
|
+
Item.new call(params.merge(:Operation => :ItemLookup, :ItemId => asin))
|
35
|
+
end
|
36
|
+
|
37
|
+
BASE_URL = 'http://free.apisigning.com/onca/xml?'
|
38
|
+
|
39
|
+
def call(params={})
|
40
|
+
configure if @options.nil?
|
41
|
+
p url = BASE_URL + @options.merge(params).map{|key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join('&')
|
42
|
+
resp = HTTPClient.new.get_content(url)
|
43
|
+
p resp = resp.force_encoding('UTF-8') # shady workaround cause amazon returns bad utf-8 chars
|
44
|
+
resp = Crack::XML.parse(resp)
|
45
|
+
resp
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/readme.textile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
h1. Introduction
|
2
|
+
|
3
|
+
There is already a sophisticated Amazon gem out there called ruby-aaws, but ASIN is __reaaaaaaaaaly easy to use!__
|
4
|
+
|
5
|
+
It currently just supports the _ItemLookup_ via REST but is easy to extend and understand.
|
6
|
+
|
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
|
+
|
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
|
+
|
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
|
+
h2. Usage
|
20
|
+
|
21
|
+
<pre>
|
22
|
+
item = ASIN.lookup '1430218150', :AWSAccessKeyId => 'your-api-key'
|
23
|
+
item.title
|
24
|
+
=> Learn Objective-C on the Mac (Learn Series)
|
25
|
+
</pre>
|
data/test/test_asin.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestAsin < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@helper = Object.new
|
7
|
+
@helper.extend ASIN
|
8
|
+
end
|
9
|
+
|
10
|
+
# comment in and add your key to test real calls
|
11
|
+
def test_r_type_from_engine
|
12
|
+
p item = @helper.lookup('1430218150', :AWSAccessKeyId => 'AKIAJFA5X7RTOKFNPVZQ', :ResponseGroup => :Medium)
|
13
|
+
assert_match(/Learn Objective/, item.title)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Peter Schr\xC3\xB6der"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-02 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: crack
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 1
|
33
|
+
- 0
|
34
|
+
version: 0.1.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: hashie
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 2
|
49
|
+
- 0
|
50
|
+
version: 0.2.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: httpclient
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 11
|
62
|
+
segments:
|
63
|
+
- 2
|
64
|
+
- 1
|
65
|
+
- 0
|
66
|
+
version: 2.1.0
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
69
|
+
description: A mazon S imple IN terface or whatever you want to call this
|
70
|
+
email: phoetmail@googlemail.com
|
71
|
+
executables: []
|
72
|
+
|
73
|
+
extensions: []
|
74
|
+
|
75
|
+
extra_rdoc_files:
|
76
|
+
- readme.textile
|
77
|
+
files:
|
78
|
+
- readme.textile
|
79
|
+
- lib/asin.rb
|
80
|
+
- test/test_helper.rb
|
81
|
+
- test/test_asin.rb
|
82
|
+
has_rdoc: true
|
83
|
+
homepage: http://github.com/phoet/asin
|
84
|
+
licenses: []
|
85
|
+
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options:
|
88
|
+
- -a
|
89
|
+
- --inline-source
|
90
|
+
- --charset=UTF-8
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
hash: 3
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
hash: 3
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
version: "0"
|
111
|
+
requirements: []
|
112
|
+
|
113
|
+
rubyforge_project: none
|
114
|
+
rubygems_version: 1.3.7
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: Simple interface to Amazon Item lookup over apisigning.com
|
118
|
+
test_files:
|
119
|
+
- test/test_helper.rb
|
120
|
+
- test/test_asin.rb
|