agent_cooper 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -11,11 +11,13 @@ Currently the following eBay APIs are supported:
11
11
  Usage
12
12
  -----
13
13
  Set up.
14
-
15
- AgentCooper::Request::APP_ID = YOUR_APP_ID
14
+
15
+ AgentCooper::Config.set do |config|
16
+ config.app_id = "SOME_OBSCURE_APP_ID"
17
+ end
16
18
 
17
19
  Initialize a request
18
-
20
+
19
21
  request = AgentCooper::Finder.new
20
22
  request = AgentCooper::Shopper.new
21
23
  request = AgentCooper::Merchandiser.new
@@ -23,15 +25,22 @@ Initialize a request
23
25
  Build request params.
24
26
 
25
27
  request << {
26
- 'OPERATION-NAME' => 'findItemsByKeywords',
27
- 'keywords' => 'harry potter'
28
+ 'OPERATION-NAME' => 'getSearchKeywordsRecommendation',
29
+ 'KEYWORDS' => 'arry potter'
28
30
  }
29
31
 
30
32
  Get a response.
31
33
 
32
34
  response = request.get
33
35
 
34
- Response API allows one to utilize the pure goodness of Nokogiri:
36
+ Return a hash:
37
+
38
+ response.to_hash
39
+
40
+ returns: {'getSearchKeywordsRecommendationResponse' => {'xmnls' => 'http://www.ebay.com/marketplace/search/v1/services', 'ack' => 'Success', 'version' => '1.9.0', 'keywords' => 'harry potter'}}
41
+
42
+
43
+ Or parse a response with Nokogiri:
35
44
 
36
45
  response.xml.css("Item > Title").each do |title|
37
46
  some business value
@@ -39,10 +48,6 @@ Response API allows one to utilize the pure goodness of Nokogiri:
39
48
 
40
49
  response.xml.xpath("//Item")
41
50
 
42
- Specify a particular eBay locale (defaults to US):
43
-
44
- AgentCooper::Shopper.new(:locale => :fr)
45
-
46
51
  ----
47
52
 
48
53
  Based on papercavalier's [sucker](http://github.com/papercavalier/sucker).
@@ -14,12 +14,14 @@ module AgentCooperMethods
14
14
  end
15
15
 
16
16
  def cassette_name
17
- Digest::MD5.hexdigest(@request.parameters.to_json)
17
+ Digest::MD5.hexdigest(@request.options.to_json)
18
18
  end
19
19
  end
20
20
 
21
21
  World(AgentCooperMethods)
22
22
 
23
23
  Before do
24
- Request::APP_ID = ebay_app_id
24
+ AgentCooper::Config.set do |config|
25
+ config.app_id = 'SOME_OBSCURE_APP_ID'
26
+ end
25
27
  end
@@ -0,0 +1,17 @@
1
+ module AgentCooper
2
+ class Config
3
+ class << self
4
+ def set(&block)
5
+ yield self
6
+ end
7
+
8
+ def app_id=(app_id)
9
+ @@app_id = app_id
10
+ end
11
+
12
+ def app_id
13
+ @@app_id ||= nil
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,22 +1,18 @@
1
1
  module AgentCooper
2
2
  class Finder < Request
3
3
 
4
- HOST = 'http://svcs.ebay.com/services/search/FindingService/v1'
5
4
  VERSION = '1.9.0'
6
5
 
7
- protected
6
+ base_uri 'svcs.ebay.com'
7
+ default_params 'SECURITY-APPNAME' => Config.app_id,
8
+ 'SECURITY-VERSION' => VERSION,
9
+ 'RESPONSE-DATA-FORMAT' => ENCODING,
10
+ 'REST-PAYLOAD' => ''
8
11
 
9
- def options
10
- {
11
- 'SECURITY-APPNAME' => APP_ID,
12
- 'SECURITY-VERSION' => VERSION,
13
- 'RESPONSE-DATA-FORMAT' => ENCODING,
14
- 'REST-PAYLOAD' => ''
15
- }.merge(parameters)
16
- end
12
+ protected
17
13
 
18
- def uri
19
- "#{HOST}?#{options.to_params}"
14
+ def path
15
+ '/services/search/FindingService/v1'
20
16
  end
21
17
  end
22
18
  end
@@ -1,24 +1,20 @@
1
1
  module AgentCooper
2
2
  class Merchandiser < Request
3
3
 
4
- HOST = 'http://svcs.ebay.com/MerchandisingService'
5
4
  SERVICE_NAME = 'MerchandisingService'
6
5
  VERSION = '1.4.0'
7
-
8
- protected
9
6
 
10
- def options
11
- {
12
- 'SERVICE-NAME' => SERVICE_NAME,
13
- 'SERVICE-VERSION' => VERSION,
14
- 'RESPONSE-DATA-FORMAT' => ENCODING,
15
- 'CONSUMER-ID' => APP_ID,
16
- 'REST-PAYLOAD' => ''
17
- }.merge(parameters)
18
- end
7
+ base_uri 'svcs.ebay.com'
8
+ default_params 'CONSUMER-ID' => Config.app_id,
9
+ 'SERVICE-NAME' => SERVICE_NAME,
10
+ 'SERVICE-VERSION' => VERSION,
11
+ 'RESPONSE-DATA-FORMAT' => ENCODING,
12
+ 'REST-PAYLOAD' => ''
13
+
14
+ protected
19
15
 
20
- def uri
21
- "#{HOST}?#{options.to_params}"
16
+ def path
17
+ '/MerchandisingService'
22
18
  end
23
19
  end
24
20
  end
@@ -1,47 +1,30 @@
1
1
  require 'httparty'
2
2
 
3
- class Request
4
- ENCODING = 'XML'
5
- LOCALE_CODES = {
6
- :us => 0,
7
- :fr => 71,
8
- :es => 186,
9
- :gb => 3,
10
- :de => 77,
11
- :nl => 146,
12
- :it => 101,
13
- :au => 15,
14
- :ca => 2
15
- }
16
-
17
- attr_accessor :locale
18
-
19
- def initialize(args={})
20
- args.each { |k, v| send("#{k}=", v) }
21
- end
3
+ module AgentCooper
4
+ class Request
22
5
 
23
- def parameters
24
- @parameters ||= {}
25
- end
6
+ ENCODING = 'XML'
26
7
 
27
- def reset!
28
- @parameters = {}
29
- end
8
+ include HTTParty
9
+ format :xml
30
10
 
31
- def <<(hash)
32
- parameters.merge!(hash)
33
- end
11
+ attr_accessor :locale
34
12
 
35
- def locale
36
- @locale ||= :us
37
- end
13
+ def options
14
+ @options ||= {}
15
+ end
38
16
 
39
- def locale_code
40
- LOCALE_CODES[locale]
41
- end
17
+ def <<(hash)
18
+ options.merge!(hash)
19
+ end
20
+
21
+ def reset!
22
+ @options = {}
23
+ end
42
24
 
43
- def get
44
- response = HTTParty.get(uri)
45
- Response.new(response)
25
+ def get
26
+ r = self.class.get(path, :query => options)
27
+ Response.new(r)
28
+ end
46
29
  end
47
30
  end
@@ -1,30 +1,36 @@
1
1
  require 'nokogiri'
2
2
 
3
- class Response
3
+ module AgentCooper
4
+ class Response
4
5
 
5
- def initialize(response)
6
- @response = response
7
- end
6
+ def initialize(response)
7
+ @response = response
8
+ end
8
9
 
9
- def body
10
- response.body
11
- end
10
+ def body
11
+ response.body
12
+ end
12
13
 
13
- def code
14
- response.code
15
- end
14
+ def to_hash
15
+ response.parsed_response
16
+ end
16
17
 
17
- def valid?
18
- code == 200
19
- end
18
+ def code
19
+ response.code
20
+ end
20
21
 
21
- def xml
22
- @xml ||= Nokogiri::XML(body)
23
- end
22
+ def valid?
23
+ code == 200
24
+ end
25
+
26
+ def xml
27
+ @xml ||= Nokogiri::XML(body)
28
+ end
24
29
 
25
- protected
30
+ protected
26
31
 
27
- def response
28
- @response
32
+ def response
33
+ @response
34
+ end
29
35
  end
30
36
  end
@@ -1,22 +1,18 @@
1
1
  module AgentCooper
2
2
  class Shopper < Request
3
3
 
4
- HOST = 'http://open.api.ebay.com/shopping'
5
4
  VERSION = '717'
6
-
7
- protected
8
5
 
9
- def options
10
- {
11
- 'APPID' => APP_ID,
12
- 'RESPONSEENCODING' => ENCODING,
13
- 'VERSION' => VERSION,
14
- 'SITEID' => locale_code
15
- }.merge(parameters)
16
- end
6
+ base_uri 'open.api.ebay.com'
7
+ default_params 'APPID' => Config.app_id,
8
+ 'RESPONSEENCODING' => ENCODING,
9
+ 'VERSION' => VERSION,
10
+ 'SITEID' => 0
11
+
12
+ protected
17
13
 
18
- def uri
19
- "#{HOST}?#{options.to_params}"
14
+ def path
15
+ '/shopping'
20
16
  end
21
17
  end
22
18
  end
@@ -1,3 +1,3 @@
1
1
  module AgentCooper
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/agent_cooper.rb CHANGED
@@ -1,8 +1,9 @@
1
+ require 'agent_cooper/config'
1
2
  require 'agent_cooper/request'
2
3
  require 'agent_cooper/response'
3
4
  require 'agent_cooper/finder'
4
5
  require 'agent_cooper/shopper'
5
6
  require 'agent_cooper/merchandiser'
6
7
 
7
- module AgentCooper
8
+ module AgentCooper
8
9
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ module AgentCooper
4
+ describe Config do
5
+ subject { Config }
6
+
7
+ describe ".app_id=" do
8
+ it "sets a value for @@app_id" do
9
+ subject.app_id = "12345"
10
+ subject.class_variables.should include("@@app_id")
11
+ end
12
+ end
13
+
14
+ describe ".app_id" do
15
+ it "returns @@app_id value" do
16
+ subject.app_id = "6789"
17
+ subject.app_id.should == "6789"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -8,22 +8,16 @@ module AgentCooper
8
8
  @request = subject.new
9
9
  end
10
10
 
11
- describe "#parameters" do
11
+ describe "#options" do
12
12
  it "returns a hash" do
13
- @request.parameters.should be_a(Hash)
13
+ @request.options.should be_a(Hash)
14
14
  end
15
15
  end
16
16
 
17
17
  describe "#<<" do
18
- it "merges a hash into the existing parameters" do
18
+ it "merges a hash into the existing options" do
19
19
  @request << {:foo => "bar"}
20
- @request.parameters[:foo].should == "bar"
21
- end
22
- end
23
-
24
- describe "#locale" do
25
- it "defaults to :us" do
26
- @request.locale.should == :us
20
+ @request.options[:foo].should == "bar"
27
21
  end
28
22
  end
29
23
 
@@ -31,7 +25,7 @@ module AgentCooper
31
25
  it "resets the parameters to a blank hash" do
32
26
  @request << {:foo => 'bar'}
33
27
  @request.reset!
34
- @request.parameters.should_not have_key(:foo)
28
+ @request.options.should_not have_key(:foo)
35
29
  end
36
30
  end
37
31
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agent_cooper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Closner
@@ -204,12 +204,14 @@ files:
204
204
  - features/support/env.rb
205
205
  - features/support/vcr.rb
206
206
  - lib/agent_cooper.rb
207
+ - lib/agent_cooper/config.rb
207
208
  - lib/agent_cooper/finder.rb
208
209
  - lib/agent_cooper/merchandiser.rb
209
210
  - lib/agent_cooper/request.rb
210
211
  - lib/agent_cooper/response.rb
211
212
  - lib/agent_cooper/shopper.rb
212
213
  - lib/agent_cooper/version.rb
214
+ - spec/agent_cooper/config_spec.rb
213
215
  - spec/agent_cooper/finder_spec.rb
214
216
  - spec/agent_cooper/merchandiser_spec.rb
215
217
  - spec/agent_cooper/request_spec.rb
@@ -261,6 +263,7 @@ test_files:
261
263
  - features/step_definitions/shopper_steps.rb
262
264
  - features/support/env.rb
263
265
  - features/support/vcr.rb
266
+ - spec/agent_cooper/config_spec.rb
264
267
  - spec/agent_cooper/finder_spec.rb
265
268
  - spec/agent_cooper/merchandiser_spec.rb
266
269
  - spec/agent_cooper/request_spec.rb