agent_cooper 0.0.6 → 0.0.7.pre

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.
Files changed (60) hide show
  1. data/.gitignore +0 -1
  2. data/.rvmrc +1 -0
  3. data/.travis.yml +2 -0
  4. data/README.md +6 -4
  5. data/Rakefile +2 -0
  6. data/agent_cooper.gemspec +16 -14
  7. data/features/finder.feature +1 -1
  8. data/features/support/env.rb +1 -5
  9. data/features/support/vcr.rb +2 -6
  10. data/lib/agent_cooper.rb +13 -15
  11. data/lib/agent_cooper/builder.rb +46 -0
  12. data/lib/agent_cooper/config.rb +6 -1
  13. data/lib/agent_cooper/request.rb +54 -30
  14. data/lib/agent_cooper/requests/finder.rb +12 -15
  15. data/lib/agent_cooper/requests/merchandiser.rb +15 -16
  16. data/lib/agent_cooper/requests/shopper.rb +14 -14
  17. data/lib/agent_cooper/response.rb +9 -10
  18. data/lib/agent_cooper/synchrony.rb +22 -9
  19. data/lib/agent_cooper/version.rb +1 -1
  20. data/spec/agent_cooper/config_spec.rb +36 -0
  21. data/spec/agent_cooper/request_spec.rb +17 -106
  22. data/spec/agent_cooper/requests/finder_spec.rb +11 -8
  23. data/spec/agent_cooper/requests/merchandiser_spec.rb +13 -7
  24. data/spec/agent_cooper/requests/shopper_spec.rb +12 -7
  25. data/spec/agent_cooper/response_spec.rb +21 -53
  26. data/spec/agent_cooper_spec.rb +5 -2
  27. data/spec/fixtures/cassettes/finder/find_items_advanced/0612b88aa6d3bcece0d88b587267b6de.yml +590 -0
  28. data/spec/fixtures/cassettes/finder/find_items_by_category/e782300f74c9c6669d5c0d9719d03853.yml +420 -0
  29. data/spec/fixtures/cassettes/finder/find_items_by_keywords/a6e74b4aff9c701b73db82def8b31bc8.yml +632 -0
  30. data/spec/fixtures/cassettes/finder/find_items_by_product/9dd9b3128cbf3cda22e027207825c4c0.yml +684 -0
  31. data/spec/fixtures/cassettes/finder/find_items_in_ebay_stores/6514aa6b1db3f9fbddcfbb32b58fb67e.yml +464 -0
  32. data/spec/fixtures/cassettes/finder/get_histograms/{80bd04d96cd529af4148866a2d28f079.yml → 1cc52302ce2de278051c3b6be90ccd36.yml} +11 -11
  33. data/spec/fixtures/cassettes/finder/get_search_keywords_recommendation/{ec96bf3af091373de08550ff46f86c87.yml → 85a972f0b32d1e011b2c8b74fff19261.yml} +6 -6
  34. data/spec/fixtures/cassettes/merchandiser/get_most_watched_items/66600b7c4cb52b32e517f3e5563463e1.yml +51 -0
  35. data/spec/fixtures/cassettes/merchandiser/get_related_category_items/{c949e90fb2bd975020df657a4348328d.yml → c74a3e3dff9ffb3e5a84c057d1a88bf3.yml} +6 -6
  36. data/spec/fixtures/cassettes/merchandiser/get_similar_items/{56c2c41a60135db83d359053778a065f.yml → 943af1f951bf437e72a0d6325bc167af.yml} +6 -6
  37. data/spec/fixtures/cassettes/merchandiser/get_top_selling_products/323d900971616df3712c8bc7737a70f1.yml +51 -0
  38. data/spec/fixtures/cassettes/shopper/find_half_products/{77ad8a2ade8e4efadef07346677e43a9.yml → 7bf13f5c5fad44a19d46d68168dc05a3.yml} +15 -15
  39. data/spec/fixtures/cassettes/shopper/find_popular_items/67c26e00c0be5bfa5994b1ae408517f7.yml +258 -0
  40. data/spec/fixtures/cassettes/shopper/find_popular_searches/{6db9b375ea610e870e7a4ca4e20974ec.yml → bcf07f2cb90cae7cb40f2252dde126a6.yml} +8 -8
  41. data/spec/fixtures/cassettes/shopper/find_products/{3fd6d6000037cd409ab1ef18a6075257.yml → b1001bc7a19381f533b36d7b40daedd7.yml} +16 -16
  42. data/spec/fixtures/cassettes/shopper/find_reviews_and_guides/{8586a984e122cb84136d3cc0bb8bec77.yml → ceb14bd40e2371d3e58e9c4645e857f2.yml} +16 -16
  43. data/spec/fixtures/cassettes/shopper/get_item_status/{f34cf10c602f9fec7a388297888b4299.yml → 83876f24642637642979b09a6475b475.yml} +8 -8
  44. data/spec/fixtures/cassettes/shopper/get_multiple_items/{c1183c23add6cad9afbb73f986c2692e.yml → 1f42d0c0fe105f8c0ad79d08f1900738.yml} +8 -8
  45. data/spec/fixtures/cassettes/shopper/get_shipping_costs/{e7de18031f266637414155639797c097.yml → 9ca24c61a943a3f6647e45b0ae30261f.yml} +8 -8
  46. data/spec/fixtures/cassettes/shopper/get_single_item/{d4a895de6b214907c47a0b32ab630b9a.yml → d50997fefbf6099325c3c01ef2a4cb51.yml} +8 -8
  47. data/spec/fixtures/cassettes/shopper/get_user_profile/{dccabbf426bfeba4d898ca546e17dd4b.yml → a5a8fd7e8aeaea9a16dc09c6cb8f3092.yml} +8 -8
  48. data/spec/spec_helper.rb +2 -1
  49. data/spec/support/shared/request.rb +67 -0
  50. metadata +83 -92
  51. data/lib/agent_cooper/nokogiri_decorator.rb +0 -47
  52. data/spec/fixtures/cassettes/finder/find_items_advanced/d49218a9522a4a055e31fc6b88c3a2b5.yml +0 -528
  53. data/spec/fixtures/cassettes/finder/find_items_by_category/85e07de9993e68d09e895f1b01234c5a.yml +0 -463
  54. data/spec/fixtures/cassettes/finder/find_items_by_keywords/91d7d9b4b62b82877604465a20f9ffa9.yml +0 -668
  55. data/spec/fixtures/cassettes/finder/find_items_by_product/59ee1391b6dfc2a93e30c8911942fc71.yml +0 -744
  56. data/spec/fixtures/cassettes/finder/find_items_in_ebay_stores/c7d658804b6353fda03809c21b183af2.yml +0 -472
  57. data/spec/fixtures/cassettes/merchandiser/get_most_watched_items/3c0cb6fb7bb94b384cae15be175e9640.yml +0 -51
  58. data/spec/fixtures/cassettes/merchandiser/get_top_selling_products/a82dcb1f094754e54f2a5de191818c3f.yml +0 -53
  59. data/spec/fixtures/cassettes/shopper/find_popular_items/532fcb32fee187bdc6cc5fb7e4336992.yml +0 -258
  60. data/spec/fixtures/ebay.yml.sample +0 -1
@@ -1,35 +1,34 @@
1
- require 'nokogiri'
2
-
3
1
  module AgentCooper
4
2
  class Response
5
3
 
6
- def initialize(response)
7
- @response = response
8
- end
4
+ include Virtus
5
+
6
+ attribute :response, Object, :accessor => :protected
9
7
 
8
+ # @api public
10
9
  def body
11
10
  response.body
12
11
  end
13
12
 
13
+ # @api public
14
14
  def to_hash
15
- xml.to_hash
15
+ AgentCooper::Builder.from_xml(xml)
16
16
  end
17
17
 
18
+ # @api public
18
19
  def code
19
20
  response.code
20
21
  end
21
22
 
23
+ # @api public
22
24
  def valid?
23
25
  code == 200
24
26
  end
25
27
 
28
+ # @api public
26
29
  def xml
27
30
  @xml ||= Nokogiri::XML(body)
28
31
  end
29
32
 
30
- protected
31
-
32
- attr_reader :response
33
-
34
33
  end
35
34
  end
@@ -1,31 +1,44 @@
1
- require 'agent_cooper'
2
1
  require 'em-synchrony'
3
2
  require 'em-synchrony/em-http'
4
3
 
5
4
  module AgentCooper
6
5
 
7
6
  class Request
8
- def adapter
9
- @adapter ||= EM::HttpRequest
10
- end
11
7
 
8
+ # request attributes
9
+ attribute :request_adapter, Object,
10
+ :default => Proc.new { EM::HttpRequest },
11
+ :accessor => :protected
12
+
13
+ # @api public
12
14
  def aget(&block)
13
- http = adapter.new(url).aget
14
- http.callback { block.call(Response.new(http)) }
15
- http.errback { block.call(Response.new(http)) }
15
+ http = new_adapter.aget
16
+ http.callback { block.call(Response.new(:response => http)) }
17
+ http.errback { block.call(Response.new(:respose => http)) }
16
18
  end
17
19
 
20
+ # @api public
18
21
  def get
19
- http = adapter.new(url).get
20
- Response.new(http)
22
+ http = new_adapter.get
23
+ Response.new(:response => http)
24
+ end
25
+
26
+ private
27
+
28
+ # @api private
29
+ def new_adapter
30
+ request_adapter.new(url)
21
31
  end
32
+
22
33
  end
23
34
 
24
35
  class Response
36
+ # @api public
25
37
  def body
26
38
  response.response
27
39
  end
28
40
 
41
+ # @api public
29
42
  def code
30
43
  response.response_header.status
31
44
  end
@@ -1,3 +1,3 @@
1
1
  module AgentCooper
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7.pre"
3
3
  end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe AgentCooper::Config do
4
+
5
+ subject { described_class }
6
+
7
+ let(:app_id) { "123456ABCD" }
8
+
9
+ describe ".app_id" do
10
+ before do
11
+ subject.instance_variable_set(:@app_id, app_id)
12
+ end
13
+
14
+ its(:app_id) { should eql(app_id) }
15
+ end
16
+
17
+ describe ".app_id=" do
18
+ it "sets app_id" do
19
+ subject.app_id = app_id
20
+ subject.instance_variable_get(:@app_id).should eql(app_id)
21
+ end
22
+ end
23
+
24
+ describe ".configure" do
25
+ it "yields an AgentCooper::Config" do
26
+ subject.configure do |obj|
27
+ obj.should eql(described_class)
28
+ end
29
+ end
30
+
31
+ context "when no block is given" do
32
+ it { should raise_error }
33
+ end
34
+ end
35
+
36
+ end
@@ -1,117 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
- module AgentCooper
4
-
5
- describe Request do
6
-
7
- let(:adapter) { mock(HTTPClient) }
8
- let(:url) { 'http://foo.com' }
9
-
10
- let(:response) { 'bar' }
11
-
12
- let(:host) { 'foo.com' }
13
- let(:path) { '/bar' }
14
- let(:query) { 'foo=bar&bar=foo' }
15
-
16
- let(:default_parameters) { {:foo => 'bar'} }
17
- let(:parameters) { {:bar => 'foo'} }
18
-
19
- let(:config) { mock(Config) }
20
-
21
- subject { Request }
22
-
23
- before(:each) do
24
- @request = subject.new
25
- end
26
-
27
- describe "#app_id" do
28
- before(:each) do
29
- @request.instance_variable_set(:@config, config)
30
- end
31
-
32
- it "calls @config.app_id" do
33
- config.should_receive(:app_id)
34
- @request.app_id
35
- end
36
- end
37
-
38
- describe "#adapter" do
39
- it "defaults to HTTPClient" do
40
- @request.adapter.should be_a(HTTPClient)
41
- end
3
+ describe AgentCooper::Request do
4
+ describe "#parameters" do
5
+ let(:query_parameters) do
6
+ { "SECURITY-APPNAME" => "123456",
7
+ "SECURITY-VERSION" => 781 }
42
8
  end
43
9
 
44
- describe "#get" do
45
- before(:each) do
46
- adapter.stub(:get).and_return(response)
47
-
48
- @request.stub(:adapter).and_return(adapter)
49
- @request.stub(:url).and_return(url)
50
- end
51
-
52
- it "calls adapter#get" do
53
- adapter.should_receive(:get).with(url)
54
- @request.get
55
- end
56
-
57
- it "initializes a new AgentCooper::Response" do
58
- Response.should_receive(:new).with(response)
59
- @request.get
60
- end
61
-
62
- it "returns an AgentCooper::Response" do
63
- @request.get.should be_a(Response)
64
- end
10
+ let(:default_parameters) do
11
+ { "REST-PAYLOAD" => "",
12
+ "SECURITY-VERSION" => 582 }
65
13
  end
66
14
 
67
- describe "#url" do
68
- before(:each) do
69
- @request.stub(:host).and_return(host)
70
- @request.stub(:path).and_return(path)
71
- @request.stub(:query).and_return(query)
72
- end
73
-
74
- it "builds the correct url" do
75
- URI::HTTP.should_receive(:build).with(:host => host, :path => path, :query => query)
76
- @request.url
77
- end
78
-
79
- it "returns an instance of URI::HTTP" do
80
- @request.url.should be_a(URI::HTTP)
81
- end
15
+ let(:expected_parameters) do
16
+ { "SECURITY-APPNAME" => "123456",
17
+ "SECURITY-VERSION" => 781,
18
+ "REST-PAYLOAD" => "" }
82
19
  end
83
20
 
84
- describe "#query" do
85
- before(:each) do
86
- @request.stub(:default_parameters).and_return(default_parameters)
87
- @request.stub(:parameters).and_return(parameters)
88
- end
89
-
90
- it "returns the query" do
91
- @request.query.should == "foo=bar&bar=foo"
92
- end
21
+ before do
22
+ subject.stub(:default_parameters).and_return(default_parameters)
23
+ subject.stub(:query_parameters).and_return(query_parameters)
93
24
  end
94
25
 
95
- describe "#parameters" do
96
- it "returns a hash" do
97
- @request.parameters.should be_a(Hash)
98
- end
99
- end
100
-
101
- describe "#<<" do
102
- it "merges a hash into the existing parameters" do
103
- @request << {:foo => "bar"}
104
- @request.parameters[:foo].should == "bar"
105
- end
106
- end
107
-
108
- describe "#reset!" do
109
- it "resets the parameters to a blank hash" do
110
- @request << {:foo => 'bar'}
111
- @request.reset!
112
- @request.parameters.should_not have_key(:foo)
113
- end
114
- end
26
+ its(:parameters) { should eql(expected_parameters) }
115
27
  end
116
-
117
- end
28
+ end
@@ -1,13 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
- module AgentCooper
4
- describe Finder do
5
-
6
- subject { Finder.new }
7
-
8
- it "inherits from AgentCooper::Request" do
9
- subject.should be_a(Request)
3
+ describe AgentCooper::Finder do
4
+ it_behaves_like "Request" do
5
+ let(:expected_default_parameters) do
6
+ {
7
+ "SECURITY-APPNAME" => app_id,
8
+ "SECURITY-VERSION" => version,
9
+ "RESPONSE-DATA-FORMAT" => encoding,
10
+ "REST-PAYLOAD" => ""
11
+ }
10
12
  end
11
13
 
14
+ its(:default_parameters) { should eql(expected_default_parameters) }
12
15
  end
13
- end
16
+ end
@@ -1,13 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
- module AgentCooper
4
- describe Merchandiser do
3
+ describe AgentCooper::Merchandiser do
4
+ it_behaves_like "Request" do
5
+ let(:service_name) { described_class::SERVICE_NAME }
5
6
 
6
- subject { Merchandiser.new }
7
-
8
- it "inherits from AgentCooper::Request" do
9
- subject.should be_a(Request)
7
+ let(:expected_default_parameters) do
8
+ {
9
+ "CONSUMER-ID" => app_id,
10
+ "SERVICE-NAME" => service_name,
11
+ "SERVICE-VERSION" => version,
12
+ "RESPONSE-DATA-FORMAT" => encoding,
13
+ "REST-PAYLOAD" => ""
14
+ }
10
15
  end
11
16
 
17
+ its(:default_parameters) { should eql(expected_default_parameters) }
12
18
  end
13
- end
19
+ end
@@ -1,13 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
- module AgentCooper
4
- describe Shopper do
3
+ describe AgentCooper::Shopper do
4
+ it_behaves_like "Request" do
5
+ let(:site_id) { described_class::SITE_ID }
5
6
 
6
- subject { Shopper.new }
7
-
8
- it "inherits from AgentCooper::Request" do
9
- subject.should be_a(Request)
7
+ let(:expected_default_parameters) do
8
+ {
9
+ "APPID" => app_id,
10
+ "RESPONSEENCODING" => encoding,
11
+ "VERSION" => version,
12
+ "SITEID" => site_id
13
+ }
10
14
  end
11
15
 
16
+ its(:default_parameters) { should eql(expected_default_parameters) }
12
17
  end
13
- end
18
+ end
@@ -1,57 +1,25 @@
1
1
  require 'spec_helper'
2
2
 
3
- module AgentCooper
4
- describe Response do
5
-
6
- let(:response) { '<root><item></item></root>' }
7
-
8
- subject { Response.new(response) }
9
-
10
- before(:each) do
11
- response.stub(:body).and_return(response)
12
- end
13
-
14
- describe "#body" do
15
- it "delegates to response#code" do
16
- response.should_receive(:body)
17
- subject.body
18
- end
19
- end
20
-
21
- describe "#to_hash" do
22
- it "converts the xml to a hash" do
23
- subject.to_hash.should == { "item"=> {} }
24
- end
25
- end
26
-
27
- describe "#code" do
28
- it "delegate to response#code" do
29
- response.should_receive(:code)
30
- subject.code
31
- end
32
- end
33
-
34
- describe "#valid?" do
35
- context "#code returns '200'" do
36
- before(:each) do
37
- subject.stub(:code).and_return(200)
38
- end
39
-
40
- it "returns true" do
41
- subject.valid?.should be_true
42
- end
43
- end
44
-
45
- context "#code does not return '200'" do
46
- before(:each) do
47
- subject.stub(:code).and_return(500)
48
- end
49
-
50
- it "returns false" do
51
- subject.valid?.should be_false
52
- end
53
- end
54
- end
3
+ describe AgentCooper::Response do
55
4
 
5
+ subject { described_class.new(:response => response) }
6
+
7
+ let(:body) { "<root><foo>bar</foo></root>" }
8
+ let(:code) { 200 }
9
+
10
+ let(:response) { mock(:response, :body => body, :code => code) }
11
+
12
+ its(:body) { should eql(body) }
13
+ its(:code) { should eql(code) }
14
+
15
+ its(:to_hash) { should eql({"foo" => "bar"}) }
16
+
17
+ its(:xml) { should be_a(Nokogiri::XML::Document) }
18
+ its(:valid?) { should be_true }
19
+
20
+ context "when code != 200" do
21
+ let(:code) { 400 }
22
+ its(:valid?) { should be_false }
56
23
  end
57
- end
24
+
25
+ end
@@ -2,11 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe AgentCooper do
4
4
 
5
- subject { AgentCooper }
5
+ subject { described_class }
6
6
 
7
7
  describe ".configure" do
8
8
  it "yields a Config object" do
9
- subject.configure {|c| c.should == AgentCooper::Config }
9
+ subject.configure do |c|
10
+ c.should == AgentCooper::Config
11
+ end
10
12
  end
11
13
  end
14
+
12
15
  end