sucker 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Sucker
2
2
  ======
3
3
 
4
- Sucker is a thin Ruby wrapper to the Amazon Product Advertising API. It runs on Curb and Crack.
4
+ Sucker is a thin Ruby wrapper to the [Amazon Product Advertising API](https://affiliate-program.amazon.co.uk/gp/advertising/api/detail/main.html). It runs on Curb and Crack and supports __everything__ in the API.
5
5
 
6
6
  ![Sucker](http://upload.wikimedia.org/wikipedia/en/7/71/Vacuum_cleaner_1910.JPG)
7
7
 
@@ -10,30 +10,35 @@ Examples
10
10
 
11
11
  Set up a worker.
12
12
 
13
- @worker = Sucker.new(
13
+ worker = Sucker.new(
14
14
  :locale => "us",
15
15
  :key => "API KEY",
16
16
  :secret => "API SECRET")
17
17
 
18
18
  Fiddle with curl.
19
19
 
20
- @worker.curl { |c| c.interface = "eth1" }
20
+ worker.curl { |c| c.interface = "eth1" }
21
21
 
22
22
  Set up a request.
23
23
 
24
- @worker << {
24
+ worker << {
25
25
  "Operation" => "ItemLookup",
26
26
  "IdType" => "ASIN",
27
27
  "ItemId" => ["0816614024", "0143105825"] }
28
28
 
29
29
  Hit Amazon and do something with the response.
30
30
 
31
- pp @worker.get["ItemLookupResponse"]["Items"]["Item"]
31
+ response = worker.get
32
+ p response.code
33
+ p response.time
34
+ p response.to_h["ItemLookupResponse"]["Items"]["Item"]
32
35
 
33
36
  Hit Amazon again.
34
37
 
35
- @worker << {
38
+ worker << {
36
39
  "ItemId" => ["0393329259", "0393317757"] }
37
- @worker.get
40
+ response = worker.get
38
41
 
39
- Check the integration specs for some more examples.
42
+ For some more examples, check the integration specs.
43
+
44
+ The unit specs should run out of the box, but the integration specs require you to create [an amazon.yml file with valid credentials](http://github.com/papercavalier/sucker/blob/master/spec/support/amazon.yml.example) in the spec/support folder. Of course, bundle install first.
@@ -1,7 +1,6 @@
1
- # = Sucker
2
- # Sucker is a thin Ruby wrapper to the {Amazon Product Advertising API}[http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/].
3
- #
4
1
  module Sucker
2
+
3
+ # A wrapper around the API request
5
4
  class Request
6
5
  HOSTS = {
7
6
  :us => 'ecs.amazonaws.com',
@@ -44,13 +43,12 @@ module Sucker
44
43
  @curl
45
44
  end
46
45
 
47
- # Makes a request to Amazon and returns the response as a hash
48
- # Todo: Handle errors
46
+ # Performs the request and returns a response object
49
47
  def get
50
48
  curl.url = uri.to_s
51
49
  curl.perform
52
50
 
53
- Crack::XML.parse(curl.body_str)
51
+ Response.new(curl)
54
52
  end
55
53
 
56
54
  # A helper method that sets the AWS Access Key ID
@@ -0,0 +1,17 @@
1
+ module Sucker
2
+
3
+ # A wrapper around the cURL response
4
+ class Response
5
+ attr_accessor :body, :code, :time
6
+
7
+ def initialize(curl)
8
+ self.body = curl.body_str
9
+ self.code = curl.response_code
10
+ self.time = curl.total_time
11
+ end
12
+
13
+ def to_h
14
+ Crack::XML.parse(body)
15
+ end
16
+ end
17
+ end
data/lib/sucker.rb CHANGED
@@ -2,7 +2,10 @@ require "cgi"
2
2
  require "crack/xml"
3
3
  require "curb"
4
4
  require "sucker/request"
5
+ require "sucker/response"
5
6
 
7
+ # = Sucker
8
+ # Sucker is a thin Ruby wrapper to the Amazon Product Advertising API.
6
9
  module Sucker
7
10
  AMAZON_API_VERSION = "2009-11-01"
8
11
 
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  module Sucker
4
- describe "Item Lookup" do
4
+ describe "Item lookup" do
5
5
  before do
6
6
  @worker = Sucker.new(
7
7
  :locale => "us",
@@ -11,14 +11,17 @@ module Sucker
11
11
  # @worker.curl { |curl| curl.verbose = true }
12
12
 
13
13
  @worker << {
14
- "Operation" => "ItemLookup",
15
- "IdType" => "ASIN" }
14
+ "Operation" => "ItemLookup",
15
+ "IdType" => "ASIN",
16
+ "Condition" => "All",
17
+ "MerchantId" => "All",
18
+ "ResponseGroup" => ["ItemAttributes", "OfferFull"] }
16
19
  end
17
20
 
18
21
  context "single item" do
19
22
  before do
20
23
  @worker << { "ItemId" => "0816614024" }
21
- @item = @worker.get["ItemLookupResponse"]["Items"]["Item"]
24
+ @item = @worker.get.to_h["ItemLookupResponse"]["Items"]["Item"]
22
25
  end
23
26
 
24
27
  it "returns an item" do
@@ -29,15 +32,16 @@ module Sucker
29
32
  @item["ASIN"].should eql "0816614024"
30
33
  end
31
34
 
32
- it "includes the item attributes" do
35
+ it "includes requested response groups" do
33
36
  @item["ItemAttributes"].should be_an_instance_of Hash
37
+ @item["Offers"].should be_an_instance_of Hash
34
38
  end
35
39
  end
36
40
 
37
41
  context "multiple items" do
38
42
  before do
39
43
  @worker << { "ItemId" => ["0816614024", "0143105825"] }
40
- @items = @worker.get["ItemLookupResponse"]["Items"]["Item"]
44
+ @items = @worker.get.to_h["ItemLookupResponse"]["Items"]["Item"]
41
45
  end
42
46
 
43
47
  it "returns two items" do
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  module Sucker
4
- describe "Japan" do
4
+ describe "A Japanese request" do
5
5
  before do
6
6
  @worker = Sucker.new(
7
7
  :locale => "jp",
@@ -16,7 +16,7 @@ module Sucker
16
16
  context "single item" do
17
17
  before do
18
18
  @worker << { "ItemId" => "0816614024" }
19
- @item = @worker.get["ItemLookupResponse"]["Items"]["Item"]
19
+ @item = @worker.get.to_h["ItemLookupResponse"]["Items"]["Item"]
20
20
  end
21
21
 
22
22
  it "returns an item" do
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  module Sucker
4
- describe "Seller Listing Search" do
4
+ describe "Seller listing search" do
5
5
  before do
6
6
  @worker = Sucker.new(
7
7
  :locale => "us",
@@ -13,7 +13,7 @@ module Sucker
13
13
  @worker << {
14
14
  "Operation" => "SellerListingSearch",
15
15
  "SellerId" => "A31N271NVIORU3" }
16
- @listings = @worker.get["SellerListingSearchResponse"]["SellerListings"]
16
+ @listings = @worker.get.to_h["SellerListingSearchResponse"]["SellerListings"]
17
17
  end
18
18
 
19
19
  it "returns page count" do
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ module Sucker
4
+ describe "Twenty items in one request" do
5
+ before do
6
+ @worker = Sucker.new(
7
+ :locale => "us",
8
+ :key => amazon["key"],
9
+ :secret => amazon["secret"])
10
+
11
+ # Prep worker
12
+ @worker << {
13
+ "Operation" => "ItemLookup",
14
+ "ItemLookup.Shared.IdType" => "ASIN",
15
+ "ItemLookup.Shared.Condition" => "All",
16
+ "ItemLookup.Shared.MerchantId" => "All",
17
+ "ItemLookup.Shared.ResponseGroup" => "OfferFull" }
18
+
19
+ # Push twenty ASINs to worker
20
+ @asins = %w{
21
+ 0816614024 0143105825 0485113600 0816616779 0942299078
22
+ 0816614008 144006654X 0486400360 0486417670 087220474X
23
+ 0486454398 0268018359 1604246014 184467598X 0312427182
24
+ 1844674282 0745640974 0745646441 0826489540 1844672972 }
25
+ @worker << {
26
+ "ItemLookup.1.ItemId" => @asins[0, 10],
27
+ "ItemLookup.2.ItemId" => @asins[10, 10] }
28
+
29
+ @items = @worker.get.to_h["ItemLookupResponse"]["Items"].map { |items| items["Item"] }.flatten!
30
+ end
31
+
32
+ it "returns 20 items" do
33
+ @items.count.should eql 20
34
+ @items.map { |item| item["ASIN"] }.should eql @asins
35
+ end
36
+ end
37
+ end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  module Sucker
4
- describe "Request" do
4
+ describe Request do
5
5
  before do
6
6
  @worker = Sucker.new
7
7
  end
@@ -43,14 +43,13 @@ module Sucker
43
43
  @worker.locale = "us"
44
44
  @worker.secret = "secret"
45
45
 
46
- # Stub curl
47
46
  curl = @worker.curl
48
47
  curl.stub(:get).and_return(nil)
49
48
  curl.stub!(:body_str).and_return(fixture("single_item_lookup.us"))
50
49
  end
51
50
 
52
- it "returns a hash" do
53
- @worker.get.should be_an_instance_of Hash
51
+ it "returns a Response object" do
52
+ @worker.get.should be_an_instance_of Response
54
53
  end
55
54
  end
56
55
 
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ module Sucker
4
+ describe Response do
5
+ before do
6
+ curl = Sucker.new.curl
7
+ curl.stub(:get).and_return(nil)
8
+ curl.stub!(:body_str).and_return(fixture("multiple_item_lookup.us"))
9
+ curl.stub!(:response_code).and_return(200)
10
+ curl.stub!(:total_time).and_return(1.0)
11
+ @response = Response.new(curl)
12
+ end
13
+
14
+ context ".new" do
15
+ it "sets the response body" do
16
+ @response.body.should be_an_instance_of String
17
+ end
18
+
19
+ it "sets the response code" do
20
+ @response.code.should == 200
21
+ end
22
+
23
+ it "sets the response time" do
24
+ @response.time.should be_an_instance_of Float
25
+ end
26
+ end
27
+
28
+ context "to_h" do
29
+ it "returns a hash" do
30
+ @response.to_h.should be_an_instance_of Hash
31
+ end
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Hakan Ensari
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-07-26 00:00:00 +01:00
19
+ date: 2010-07-29 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -65,14 +65,17 @@ files:
65
65
  - LICENSE
66
66
  - lib/sucker.rb
67
67
  - lib/sucker/request.rb
68
+ - lib/sucker/response.rb
68
69
  - README.md
69
70
  - spec/integration/item_lookup_spec.rb
70
71
  - spec/integration/japan_spec.rb
71
72
  - spec/integration/seller_listing_search_spec.rb
73
+ - spec/integration/twenty_items_in_one_request_spec.rb
72
74
  - spec/spec_helper.rb
73
75
  - spec/support/amazon_credentials.rb
74
76
  - spec/support/curb_stubber.rb
75
77
  - spec/unit/sucker/request_spec.rb
78
+ - spec/unit/sucker/response_spec.rb
76
79
  - spec/unit/sucker_spec.rb
77
80
  has_rdoc: true
78
81
  homepage: http://github.com/papercavalier/sucker
@@ -112,8 +115,10 @@ test_files:
112
115
  - spec/integration/item_lookup_spec.rb
113
116
  - spec/integration/japan_spec.rb
114
117
  - spec/integration/seller_listing_search_spec.rb
118
+ - spec/integration/twenty_items_in_one_request_spec.rb
115
119
  - spec/spec_helper.rb
116
120
  - spec/support/amazon_credentials.rb
117
121
  - spec/support/curb_stubber.rb
118
122
  - spec/unit/sucker/request_spec.rb
123
+ - spec/unit/sucker/response_spec.rb
119
124
  - spec/unit/sucker_spec.rb