handcrafted-a2ws 0.0.3 → 0.1.0
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/VERSION.yml +2 -2
- data/a2ws.gemspec +7 -2
- data/lib/a2ws.rb +6 -51
- data/lib/a2ws/base.rb +26 -0
- data/lib/a2ws/image.rb +19 -0
- data/lib/a2ws/image_search.rb +22 -0
- data/lib/a2ws/item.rb +39 -0
- data/lib/a2ws/item_search.rb +28 -0
- metadata +7 -2
data/VERSION.yml
CHANGED
data/a2ws.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{a2ws}
|
5
|
-
s.version = "0.0
|
5
|
+
s.version = "0.1.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Andy Shen"]
|
9
|
-
s.date = %q{2009-05-
|
9
|
+
s.date = %q{2009-05-31}
|
10
10
|
s.email = %q{andy@shenie.info}
|
11
11
|
s.extra_rdoc_files = [
|
12
12
|
"LICENSE",
|
@@ -21,6 +21,11 @@ Gem::Specification.new do |s|
|
|
21
21
|
"VERSION.yml",
|
22
22
|
"a2ws.gemspec",
|
23
23
|
"lib/a2ws.rb",
|
24
|
+
"lib/a2ws/base.rb",
|
25
|
+
"lib/a2ws/image.rb",
|
26
|
+
"lib/a2ws/image_search.rb",
|
27
|
+
"lib/a2ws/item.rb",
|
28
|
+
"lib/a2ws/item_search.rb",
|
24
29
|
"spec/a2ws_spec.rb",
|
25
30
|
"spec/spec_helper.rb"
|
26
31
|
]
|
data/lib/a2ws.rb
CHANGED
@@ -4,54 +4,9 @@ require 'mash'
|
|
4
4
|
require 'activesupport'
|
5
5
|
require 'pp'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
class ItemSearch < Base
|
15
|
-
attr_accessor :items
|
16
|
-
|
17
|
-
default_params :Operation => 'ItemSearch'
|
18
|
-
|
19
|
-
def initialize(api_key, search_index = :All)
|
20
|
-
self.class.default_params :SearchIndex => search_index, :AWSAccessKeyId => api_key
|
21
|
-
end
|
22
|
-
|
23
|
-
def find(keywords, options = {})
|
24
|
-
options.merge!({:Keywords => keywords})
|
25
|
-
puts options.inspect
|
26
|
-
result = self.class.get('/onca/xml', :query => options)
|
27
|
-
|
28
|
-
@items = result["ItemSearchResponse"]["Items"]
|
29
|
-
if @items['Request']['IsValid'] == 'True'
|
30
|
-
@items['Item'].collect do |i|
|
31
|
-
Mash.new downcase_keys(i)
|
32
|
-
end
|
33
|
-
else
|
34
|
-
raise @items['Request']['Errors']['Error']['Message']
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
protected
|
39
|
-
|
40
|
-
def downcase_keys(hash)
|
41
|
-
new_hash = {}
|
42
|
-
hash.keys.each do |key|
|
43
|
-
value = hash.delete(key)
|
44
|
-
new_hash[downcase_key(key)] = value
|
45
|
-
new_hash[downcase_key(key)] = downcase_keys(value) if value.is_a?(Hash)
|
46
|
-
new_hash[downcase_key(key)] = value.each{|p| downcase_keys(p) if p.is_a?(Hash)} if value.is_a?(Array)
|
47
|
-
end
|
48
|
-
new_hash
|
49
|
-
end
|
50
|
-
|
51
|
-
def downcase_key(key)
|
52
|
-
key.titlecase.downcase.gsub(' ', '_')
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
7
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
8
|
+
require 'a2ws/base'
|
9
|
+
require 'a2ws/item'
|
10
|
+
require 'a2ws/image'
|
11
|
+
require 'a2ws/item_search'
|
12
|
+
require 'a2ws/image_search'
|
data/lib/a2ws/base.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module A2WS
|
2
|
+
class Base
|
3
|
+
include HTTParty
|
4
|
+
base_uri 'http://ecs.amazonaws.com'
|
5
|
+
default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch'
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def downcase_keys(hash)
|
10
|
+
new_hash = {}
|
11
|
+
hash.keys.each do |key|
|
12
|
+
value = hash.delete(key)
|
13
|
+
new_hash[downcase_key(key)] = value
|
14
|
+
new_hash[downcase_key(key)] = downcase_keys(value) if value.is_a?(Hash)
|
15
|
+
new_hash[downcase_key(key)] = value.each{|p| downcase_keys(p) if p.is_a?(Hash)} if value.is_a?(Array)
|
16
|
+
end
|
17
|
+
new_hash
|
18
|
+
end
|
19
|
+
|
20
|
+
def downcase_key(key)
|
21
|
+
key.titlecase.downcase.gsub(' ', '_')
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/lib/a2ws/image.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module A2WS
|
2
|
+
|
3
|
+
|
4
|
+
class ImageSearch < Base
|
5
|
+
attr_accessor :item_id
|
6
|
+
|
7
|
+
def initialize(item_id)
|
8
|
+
@item_id = item_id
|
9
|
+
end
|
10
|
+
|
11
|
+
def find
|
12
|
+
self.class.default_params :ResponseGroup => "Images"
|
13
|
+
result = self.class.get('/onca/xml', :query => {:ItemId => @item_id, :Operation => "ItemLookup"})
|
14
|
+
result["ItemLookupResponse"]["Items"]["Item"].delete("ImageSets")
|
15
|
+
result["ItemLookupResponse"]["Items"]["Item"].delete("ASIN")
|
16
|
+
downcase_keys(result["ItemLookupResponse"]["Items"]["Item"]).collect { |size, data| Image.new(size, data) }
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end
|
data/lib/a2ws/item.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module A2WS
|
2
|
+
|
3
|
+
|
4
|
+
class Item
|
5
|
+
attr_accessor :item_hash
|
6
|
+
|
7
|
+
def initialize(item)
|
8
|
+
@item_hash = item
|
9
|
+
end
|
10
|
+
|
11
|
+
def item_attributes
|
12
|
+
ItemAttributes.new @item_hash["item_attributes"]
|
13
|
+
end
|
14
|
+
|
15
|
+
def images
|
16
|
+
ImageSearch.new(@item_hash["asin"]).find
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(meth,*args)
|
20
|
+
@item_hash[meth.to_s]
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class ItemAttributes
|
26
|
+
attr_accessor :item_attributes_hash
|
27
|
+
|
28
|
+
def initialize(item_attributes)
|
29
|
+
@item_attributes_hash = item_attributes
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing(meth,*args)
|
33
|
+
@item_attributes_hash[meth.to_s]
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module A2WS
|
2
|
+
|
3
|
+
class ItemSearch < Base
|
4
|
+
attr_accessor :items, :search_index
|
5
|
+
|
6
|
+
def initialize(api_key, search_index = :All)
|
7
|
+
@search_index = search_index
|
8
|
+
self.class.default_params :AWSAccessKeyId => api_key
|
9
|
+
end
|
10
|
+
|
11
|
+
def find(keywords, options = {})
|
12
|
+
options.merge!({:Keywords => keywords, :SearchIndex => @search_index})
|
13
|
+
puts options.inspect
|
14
|
+
result = self.class.get('/onca/xml', :query => options)
|
15
|
+
|
16
|
+
@items = result["ItemSearchResponse"]["Items"]
|
17
|
+
if @items['Request']['IsValid'] == 'True'
|
18
|
+
@items['Item'].collect do |i|
|
19
|
+
Item.new downcase_keys(i)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
raise @items['Request']['Errors']['Error']['Message']
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handcrafted-a2ws
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Shen
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -60,6 +60,11 @@ files:
|
|
60
60
|
- VERSION.yml
|
61
61
|
- a2ws.gemspec
|
62
62
|
- lib/a2ws.rb
|
63
|
+
- lib/a2ws/base.rb
|
64
|
+
- lib/a2ws/image.rb
|
65
|
+
- lib/a2ws/image_search.rb
|
66
|
+
- lib/a2ws/item.rb
|
67
|
+
- lib/a2ws/item_search.rb
|
63
68
|
- spec/a2ws_spec.rb
|
64
69
|
- spec/spec_helper.rb
|
65
70
|
has_rdoc: true
|