ebay-ruby 0.3.0 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 18523b3ec4ff1f6d4bac8c6dbeb4d2570ece325e963ed4540aded51bcbef8be9
4
- data.tar.gz: 4db3fedd2023ff5c49d0a6e1369188325d1e61b798dc77b63c1cd51ee9b3baa6
3
+ metadata.gz: d6e9d59b1d0ba42b7c825f8a29aac4e7f88765c6095c686739e03688d3e5e831
4
+ data.tar.gz: 2b7d7471e20d57190db24d2e3c192c61ee2acc0918a75e594575050008380608
5
5
  SHA512:
6
- metadata.gz: 5e6cf632c7006f51393878a5af01057e9cec95678ddc7e7565e528f4c015ec2a19a70e55b1ddd8f67e4a5f5e40e00e8e2c858811178faf214084f65f934bdcf3
7
- data.tar.gz: 138a8d6fa3f982ed664cc4340ea9d47802fb4a4c8b3cc9dd5fcbfee050198c4916bda43521947831e78422bee8fd06f7db979e8934b998e7bfd153fb16151995
6
+ metadata.gz: ab9807c80c509e6d276f881c026d9d0a4430e6f57a57ff259250cc5bf30d4fe2e33b08a48c8da356fc37db90d7bc1c3109a6c42d72b584f0a2af8a9be827cb35
7
+ data.tar.gz: 5829d5b8fd4c8aa189d0979be0c030c2109976217e88434afce15b26e123b557bcbb21764a7fbdb2e170984420e0bfeef69c7ff2fe8638ccf5a398243a64db02
data/README.md CHANGED
@@ -2,81 +2,126 @@
2
2
 
3
3
  [![Build](https://github.com/hakanensari/ebay-ruby/workflows/build/badge.svg)](https://github.com/hakanensari/ebay-ruby/actions)
4
4
 
5
- A Ruby wrapper to the [eBay APIs]
5
+ This library provides a wrapper to the [eBay APIs].
6
6
 
7
- ## Usage
7
+ ## Getting started
8
+
9
+ You can instantiate API requests using shorthand class methods defined on `Ebay`.
10
+
11
+ ```ruby
12
+ Ebay.finding # returns a new Ebay::Finding instance
13
+ ```
8
14
 
9
- ### Buy APIs
15
+ ### Keys
10
16
 
11
- Retrieve purchasable items, check out, then track orders without visiting the eBay site.
17
+ eBay APIs require [developer keys].
12
18
 
13
- #### [Browse API]
19
+ You can provide these globally with the environment variables `EBAY_APP_ID`, `EBAY_DEV_ID`, and `EBAY_CERT_ID`.
14
20
 
15
- Using the Browse API, you can create a rich selection of items for your buyers to browse with keyword and category searches. You can also provides the ability to eBay members to add items and change the quantity of an item in their eBay shopping cart as well as view the contents of their eBay cart.
21
+ If you prefer not to use environment variables, you can provide your keys globally with:
16
22
 
17
23
  ```ruby
18
- require 'ebay/browse'
19
- require 'ebay/oauth/client_credentials_grant'
24
+ Ebay.configure do |config|
25
+ config.app_id = 'YOUR APP ID'
26
+ config.dev_id = 'YOUR DEV ID'
27
+ config.cert_id = 'YOUR CERT ID'
28
+ end
29
+ ```
20
30
 
21
- access_token = Oauth::ClientCredentialsGrant.new.mint_access_token
22
- request = Ebay::Browse.new(campaign_id: '123',
23
- country: 'US',
24
- zip: '19406',
25
- access_token: access_token)
26
- response = @request.search(q: 'iphone')
31
+ Finally, you can provide your keys individually to the request when instantiating it. This is what you will want to do if you are using multiple sets of credentials.
27
32
 
28
- JSON.parse(response)
33
+ Please note that each API names keys differently.
34
+
35
+ ```ruby
36
+ request = Ebay.finding(security_appname: 'YOUR APP ID')
29
37
  ```
30
38
 
31
- ### Traditional Searching APIs
39
+ In the examples below, we will assume we are providing our keys with environment variables.
32
40
 
33
- Search eBay; build search and browse experiences.
41
+ ### Parsing a response
34
42
 
35
- #### [Finding API]
43
+ The eBay APIs return responses in XML or JSON format.
36
44
 
37
- The Finding API provides programmatic access to the next generation search capabilities on the eBay platform. It lets you search and browse for items listed on eBay and provides useful metadata to refine searches and enhance the search experience.
45
+ You can override the default format of an API when instantiating the request.
38
46
 
39
47
  ```ruby
40
- require 'ebay/finding'
48
+ require 'json'
41
49
 
42
- request = Ebay::Finding.new
50
+ ### the Finding API returns XML by default
51
+ request = Ebay.finding(response_data_format: 'JSON')
43
52
  response = request.find_items_by_keywords('iphone')
44
53
 
45
54
  JSON.parse(response)
46
55
  ```
47
56
 
48
- ### Traditional Shopping/Buying APIs
49
-
50
- Retrieve public items and user data to create shopping and marketing applications.
57
+ ## Usage
58
+ ### [Browse API]
51
59
 
52
- #### [Shopping API]
60
+ The Browse API allows your buyers to search eBay items by keyword and category. It also allows them to view and add items to their eBay shopping cart. The Browse API defaults to the eBay US marketplace but may be set during initialisation. The list of available marketplaces is [here](https://developer.ebay.com/api-docs/static/rest-request-components.html#marketpl).
53
61
 
54
- The eBay Shopping API makes it easy to search for things on eBay.
62
+ **Note** The marketplace value needs to use an underscore between EBAY and the country code. The Finding and Merchandising APIs use a hyphen.
55
63
 
56
64
  ```ruby
57
- require 'ebay/shopping'
65
+ require 'ebay/browse'
66
+ require 'ebay/oauth/client_credentials_grant'
58
67
 
59
- request = Ebay::Finding.new
60
- response = request.find_products('QueryKeywords' => 'tolkien')
68
+ access_token = Oauth::ClientCredentialsGrant.new.mint_access_token
69
+ request = Ebay.browse(campaign_id: '123', country: 'US', zip: '19406',
70
+ access_token: access_token, market_id: 'EBAY_US')
71
+ response = request.search(q: 'iphone')
72
+ ```
61
73
 
62
- JSON.parse(response)
74
+ ### [Finding API]
75
+
76
+ The Finding API lets you search and browse for items listed on eBay and provides useful metadata to refine searches.
77
+
78
+ ```ruby
79
+ require 'ebay/finding'
80
+
81
+ request = Ebay.finding
82
+ response = request.find_items_by_keywords('iphone')
63
83
  ```
64
84
 
65
- #### [Merchandising API]
85
+ ### [Merchandising API]
66
86
 
67
- The Merchandising API provides item and product recommendations that can be used to cross-sell and up-sell eBay items to buyers.
87
+ The Merchandising API retrieves information about products or item listings on eBay to help you sell more merchandise to eBay buyers.
68
88
 
69
89
  ```ruby
70
90
  require 'ebay/merchandising'
71
91
 
72
- request = Ebay::Finding.new
92
+ request = Ebay::Merchandising.new
73
93
  response = request.get_most_watched_items
94
+ ```
74
95
 
75
- JSON.parse(response)
96
+ ### [Shopping API]
97
+
98
+ The eBay Shopping API makes it easy to search for things on eBay.
99
+
100
+ ```ruby
101
+ require 'ebay/shopping'
102
+
103
+ request = Ebay::Shopping.new
104
+ response = request.find_products('QueryKeywords' => 'tolkien')
105
+ ```
106
+
107
+ ### Market Place
108
+ eBay has country bsaed marketplaces ( listed [here](https://developer.ebay.com/api-docs/static/rest-request-components.html#marketpl) ). By default, the eBay gem queries the US Marketplace. To change the marketplace, set the marketplace on the request object.
109
+
110
+ **Note** For the Browse API, the marketplace value needs to use an underscore between EBAY and the country code (EBAY_AU). The Finding and Merchandising APIs require a hyphen between EBAY and the country code ( EBAY-AU )
111
+
112
+ ## Development
113
+
114
+ To write requests and responses to a logger, use the logging feature:
115
+
116
+ ```ruby
117
+ require 'logger'
118
+
119
+ request = request.use(logging: {logger: Logger.new(STDOUT)})
76
120
  ```
77
121
 
78
122
  [eBay APIs]: https://developer.ebay.com/docs
123
+ [developer keys]: https://developer.ebay.com/my/keys
79
124
  [Browse API]: https://developer.ebay.com/api-docs/buy/browse/static/overview.html
80
125
  [Finding API]: https://developer.ebay.com/Devzone/finding/Concepts/FindingAPIGuide.html
81
- [Shopping API]: https://developer.ebay.com/Devzone/shopping/docs/Concepts/ShoppingAPIGuide.html
82
126
  [Merchandising API]: https://developer.ebay.com/Devzone/merchandising/docs/Concepts/merchandisingAPIGuide.html
127
+ [Shopping API]: https://developer.ebay.com/Devzone/shopping/docs/Concepts/ShoppingAPIGuide.html
data/lib/ebay/browse.rb CHANGED
@@ -1,22 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'http'
3
+ require 'base64'
4
4
 
5
5
  require 'ebay/config'
6
- require 'ebay/sandboxable'
6
+ require 'ebay/requestable'
7
7
 
8
+ # Ruby wrapper to the eBay APIs
8
9
  module Ebay
9
- # Using the Browse API, you can create a rich selection of items for your
10
- # buyers to browse with keyword and category searches. It also provides the
11
- # ability to eBay members to add items and change the quantity of an item in
12
- # their eBay shopping cart as well as view the contents of their eBay cart.
10
+ # Returns a {Ebay::Browse#initialize Browse API} instance
11
+ def self.browse(**params)
12
+ Browse.new(**params)
13
+ end
14
+
15
+ # The Browse API allows your buyers to search eBay items by keyword and category. It also allows them to view and add
16
+ # items to their eBay shopping cart.
13
17
  #
14
18
  # @see https://developer.ebay.com/api-docs/buy/browse/overview.html
15
19
  class Browse
16
- include Sandboxable
20
+ include Requestable
17
21
 
18
- SANDBOX_ENDPOINT = 'https://api.sandbox.ebay.com/buy/browse/v1'
19
- PRODUCTION_ENDPOINT = 'https://api.ebay.com/buy/browse/v1'
22
+ self.endpoint = 'https://api.ebay.com/buy/browse/v1'
20
23
 
21
24
  # @return [String]
22
25
  attr_reader :campaign_id
@@ -30,6 +33,9 @@ module Ebay
30
33
  # @return [String,nil]
31
34
  attr_reader :zip
32
35
 
36
+ # @return [String,nil]
37
+ attr_reader :market_id
38
+
33
39
  # @return [String] the application access token
34
40
  def access_token
35
41
  @access_token ||= mint_access_token
@@ -40,36 +46,36 @@ module Ebay
40
46
  # @param [String] campaign_id
41
47
  # @param [String] reference_id
42
48
  # @param [String] access_token
43
- def initialize(campaign_id:, reference_id: nil, country: nil, zip: nil,
44
- access_token: nil)
49
+ def initialize(campaign_id:, reference_id: nil, country: nil, zip: nil, access_token: nil, market_id: 'EBAY_US')
45
50
  @campaign_id = campaign_id
46
51
  @reference_id = reference_id
47
52
  @country = country
48
53
  @zip = zip
49
54
  @access_token = access_token
55
+ @market_id = market_id
50
56
  end
51
57
 
52
- # Searches for eBay items by various query parameters and retrieves
53
- # summaries of the item
58
+ # Searches for eBay items by various query parameters and retrieves summaries of the item
54
59
  #
55
60
  # @param [Hash] params
56
61
  # @return [HTTP::Response]
57
- def search(**params)
62
+ def search(params = {})
58
63
  url = build_url('item_summary', 'search')
59
- HTTP.headers(build_headers).get(url, params: params)
64
+ http.headers(build_headers).get(url, params: params)
60
65
  end
61
66
 
62
67
  # Searches for eBay items based on a image and retrieves their summaries
63
68
  #
64
- # @param [String] image the Base64 string of the image
69
+ # @param [File] image
65
70
  # @param [Hash] params
66
71
  # @return [HTTP::Response]
67
- def search_by_image(image, **params)
72
+ def search_by_image(image, params = {})
68
73
  url = build_url('item_summary', 'search_by_image')
69
74
  headers = build_headers.update('CONTENT-TYPE' => 'application/json')
70
- body = JSON.dump(image: image)
75
+ encoded_string = Base64.encode64(image.read)
76
+ body = JSON.dump(image: encoded_string)
71
77
 
72
- HTTP.headers(headers).post(url, params: params, body: body)
78
+ http.headers(headers).post(url, params: params, body: body)
73
79
  end
74
80
 
75
81
  # Retrieves the details of a specific item
@@ -77,11 +83,11 @@ module Ebay
77
83
  # @param [String] item_id
78
84
  # @param [Hash] params
79
85
  # @return [HTTP::Response]
80
- def get_item(item_id, **params)
86
+ def get_item(item_id, params = {})
81
87
  url = build_url('item', item_id)
82
- params.update(item_id: item_id)
88
+ params = params.merge(item_id: item_id)
83
89
 
84
- HTTP.headers(build_headers).get(url, params: params)
90
+ http.headers(build_headers).get(url, params: params)
85
91
  end
86
92
 
87
93
  # Retrieves the details of a specific item using its legacy item ID
@@ -89,11 +95,11 @@ module Ebay
89
95
  # @param [String] legacy_item_id
90
96
  # @param [Hash] params
91
97
  # @return [HTTP::Response]
92
- def get_item_by_legacy_id(legacy_item_id, **params)
98
+ def get_item_by_legacy_id(legacy_item_id, params = {})
93
99
  url = build_url('item', 'get_item_by_legacy_id')
94
- params.update(legacy_item_id: legacy_item_id)
100
+ params = params.merge(legacy_item_id: legacy_item_id)
95
101
 
96
- HTTP.headers(build_headers).get(url, params: params)
102
+ http.headers(build_headers).get(url, params: params)
97
103
  end
98
104
 
99
105
  # Retrieves the details of the individual items in an item group
@@ -104,7 +110,7 @@ module Ebay
104
110
  url = build_url('item', 'get_items_by_item_group')
105
111
  params = { item_group_id: item_group_id }
106
112
 
107
- HTTP.headers(build_headers).get(url, params: params)
113
+ http.headers(build_headers).get(url, params: params)
108
114
  end
109
115
 
110
116
  # Retrieves the details of the individual items in an item group
@@ -115,11 +121,10 @@ module Ebay
115
121
  def check_compatibility(item_id, marketplace_id, compatibility_properties)
116
122
  url = build_url('item', item_id, 'check_compatibility')
117
123
  headers = build_headers
118
- headers.update('X-EBAY-C-MARKETPLACE-ID' => marketplace_id,
119
- 'CONTENT-TYPE' => 'application/json')
124
+ headers.update('X-EBAY-C-MARKETPLACE-ID' => marketplace_id, 'CONTENT-TYPE' => 'application/json')
120
125
  body = JSON.dump('compatibilityProperties' => compatibility_properties)
121
126
 
122
- HTTP.headers(headers).post(url, body: body)
127
+ http.headers(headers).post(url, body: body)
123
128
  end
124
129
 
125
130
  def add_item
@@ -141,26 +146,23 @@ module Ebay
141
146
  private
142
147
 
143
148
  def build_url(*resources, operation)
144
- endpoint = sandbox? ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT
145
149
  [endpoint, *resources, operation].join('/')
146
150
  end
147
151
 
148
152
  def build_headers
149
153
  { 'AUTHORIZATION' => "Bearer #{access_token}",
154
+ 'X-EBAY-C-MARKETPLACE-ID' => market_id,
150
155
  'X-EBAY-C-ENDUSERCTX' => build_ebay_enduser_context }
151
156
  end
152
157
 
153
158
  def build_ebay_enduser_context
154
159
  { 'affiliateCampaignId' => campaign_id,
155
160
  'affiliateReferenceId' => reference_id,
156
- 'contextualLocation' => build_contextual_location }
157
- .compact.map { |kv| kv.join('=') }.join(',')
161
+ 'contextualLocation' => build_contextual_location }.compact.map { |kv| kv.join('=') }.join(',')
158
162
  end
159
163
 
160
164
  def build_contextual_location
161
- string = { 'country' => country, 'zip' => zip }
162
- .compact.map { |kv| kv.join('=') }.join(',')
163
-
165
+ string = { 'country' => country, 'zip' => zip }.compact.map { |kv| kv.join('=') }.join(',')
164
166
  CGI.escape(string) if string
165
167
  end
166
168
  end
data/lib/ebay/config.rb CHANGED
@@ -8,7 +8,7 @@ module Ebay
8
8
  yield Config
9
9
  end
10
10
 
11
- # Stores credentials for accessing the eBay APIs
11
+ # Configures credentials for accessing the eBay APIs
12
12
  module Config
13
13
  class << self
14
14
  # @!attribute [rw] app_id
data/lib/ebay/finding.rb CHANGED
@@ -1,35 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'http'
4
-
5
3
  require 'ebay/config'
6
- require 'ebay/sandboxable'
4
+ require 'ebay/requestable'
7
5
 
6
+ # Ruby wrapper to the eBay APIs
8
7
  module Ebay
9
- # The Finding API lets you search for and browse items listed on eBay and
8
+ # Returns a {Ebay::Finding#initialize Finding API} instance
9
+ def self.finding(**params)
10
+ Finding.new(**params)
11
+ end
12
+
13
+ # The Finding API lets you search and browse for items listed on eBay and
10
14
  # provides useful metadata to refine searches.
11
15
  #
12
16
  # @see https://developer.ebay.com/Devzone/finding/Concepts/MakingACall.html
13
17
  # @see https://developer.ebay.com/Devzone/finding/CallRef/index.html
14
18
  class Finding
15
- include Sandboxable
19
+ include Requestable
16
20
 
17
- SANDBOX_ENDPOINT = 'https://svcs.sandbox.ebay.com/services/search/FindingService/v1'
18
- PRODUCTION_ENDPOINT = 'https://svcs.ebay.com/services/search/FindingService/v1'
21
+ self.endpoint = 'https://svcs.ebay.com/services/search/FindingService/v1'
19
22
 
20
- # @return [String, nil]
23
+ # @return [String]
21
24
  attr_reader :global_id
22
25
 
23
- # @return [String, nil]
26
+ # @return [String]
24
27
  attr_reader :message_encoding
25
28
 
26
- # @return [String, nil]
29
+ # @return [String]
27
30
  attr_reader :response_data_format
28
31
 
29
32
  # @return [String]
30
33
  attr_reader :security_appname
31
34
 
32
- # @return [String, nil]
35
+ # @return [String]
33
36
  attr_reader :service_version
34
37
 
35
38
  # Returns a Finding API request instance
@@ -41,7 +44,7 @@ module Ebay
41
44
  # @param [String] security_appname
42
45
  # @param [String] service_version
43
46
  def initialize(global_id: nil, message_encoding: nil,
44
- response_data_format: 'JSON',
47
+ response_data_format: nil,
45
48
  security_appname: Config.app_id, service_version: nil)
46
49
  @global_id = global_id
47
50
  @message_encoding = message_encoding
@@ -80,7 +83,7 @@ module Ebay
80
83
  # @param [Hash] payload
81
84
  # @return [HTTP::Response]
82
85
  def find_items_by_keywords(keywords, payload = {})
83
- payload.update('keywords' => keywords)
86
+ payload = payload.merge('keywords' => keywords)
84
87
  request('findItemsByKeywords', payload)
85
88
  end
86
89
 
@@ -91,8 +94,8 @@ module Ebay
91
94
  # @param [Hash] payload
92
95
  # @return [HTTP::Response]
93
96
  def find_items_by_product(product_id, product_id_type, payload = {})
94
- payload.update('productId' => product_id,
95
- 'productId.@type' => product_id_type)
97
+ payload = payload.merge('productId' => product_id,
98
+ 'productId.@type' => product_id_type)
96
99
 
97
100
  request('findItemsByProduct', payload)
98
101
  end
@@ -133,16 +136,14 @@ module Ebay
133
136
  private
134
137
 
135
138
  def request(operation, payload = {})
136
- url = sandbox? ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT
137
139
  params = { 'GLOBAL-ID' => global_id,
138
140
  'MESSAGE-ENCODING' => message_encoding,
139
141
  'OPERATION-NAME' => operation,
140
- 'REQUEST-DATA-FORMAT' => 'JSON',
141
142
  'RESPONSE-DATA-FORMAT' => response_data_format,
142
143
  'SECURITY-APPNAME' => security_appname,
143
- 'SERVICE-VERSION' => service_version }.compact
144
+ 'SERVICE-VERSION' => service_version }.update(payload).compact
144
145
 
145
- HTTP.post(url, params: params, body: JSON.dump(payload))
146
+ http.headers(headers).get(endpoint, params: params)
146
147
  end
147
148
  end
148
149
  end
@@ -1,32 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'http'
4
-
5
3
  require 'ebay/config'
6
- require 'ebay/sandboxable'
4
+ require 'ebay/requestable'
7
5
 
6
+ # Ruby wrapper to the eBay APIs
8
7
  module Ebay
8
+ # Returns a {Ebay::Merchandising#initialize Merchandising API} instance
9
+ def self.merchandising(**params)
10
+ Merchandising.new(**params)
11
+ end
12
+
9
13
  # Retrieves information about products or item listings on eBay to help you
10
14
  # sell more merchandise to eBay buyers
11
15
  #
12
16
  # @see https://developer.ebay.com/Devzone/merchandising/docs/Concepts/MerchandisingAPI_FormatOverview.html
13
17
  # @see https://developer.ebay.com/Devzone/merchandising/docs/CallRef/index.html
14
18
  class Merchandising
15
- include Sandboxable
19
+ include Requestable
16
20
 
17
- SANDBOX_ENDPOINT = 'https://svcs.sandbox.ebay.com/MerchandisingService'
18
- PRODUCTION_ENDPOINT = 'https://svcs.ebay.com/MerchandisingService'
21
+ self.endpoint = 'https://svcs.ebay.com/MerchandisingService'
19
22
 
20
23
  # @return [String]
21
24
  attr_reader :consumer_id
22
25
 
23
- # @return [String, nil]
26
+ # @return [String]
24
27
  attr_reader :global_id
25
28
 
26
- # @return [String, nil]
29
+ # @return [String]
27
30
  attr_reader :response_data_format
28
31
 
29
- # @return [String, nil]
32
+ # @return [String]
30
33
  attr_reader :service_version
31
34
 
32
35
  # Returns a Finding API request instance
@@ -36,7 +39,7 @@ module Ebay
36
39
  # @param [String] response_data_format
37
40
  # @param [String] service_version
38
41
  def initialize(consumer_id: Config.app_id, global_id: nil,
39
- response_data_format: 'JSON', service_version: nil)
42
+ response_data_format: nil, service_version: nil)
40
43
  @consumer_id = consumer_id
41
44
  @global_id = global_id
42
45
  @response_data_format = response_data_format
@@ -66,7 +69,7 @@ module Ebay
66
69
  # @param [Hash] payload
67
70
  # @return [HTTP::Response]
68
71
  def get_similar_items(item_id, payload = {})
69
- payload.update('itemId' => item_id)
72
+ payload = payload.merge('itemId' => item_id)
70
73
  request('getSimilarItems', payload)
71
74
  end
72
75
 
@@ -80,7 +83,6 @@ module Ebay
80
83
  private
81
84
 
82
85
  def request(operation, payload = {})
83
- endpoint = sandbox? ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT
84
86
  params = { 'CONSUMER-ID' => consumer_id,
85
87
  'GLOBAL-ID' => global_id,
86
88
  'OPERATION-NAME' => operation,
@@ -88,7 +90,7 @@ module Ebay
88
90
  'RESPONSE-DATA-FORMAT' => response_data_format,
89
91
  'SERVICE-VERSION' => service_version }.compact
90
92
 
91
- HTTP.post(endpoint, params: params, body: JSON.dump(payload))
93
+ http.headers(headers).post(endpoint, params: params, body: JSON.dump(payload))
92
94
  end
93
95
  end
94
96
  end
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'http'
4
-
5
3
  require 'ebay/config'
6
- require 'ebay/sandboxable'
4
+ require 'ebay/requestable'
7
5
 
8
6
  module Ebay
9
7
  module Oauth
@@ -11,10 +9,9 @@ module Ebay
11
9
  #
12
10
  # @see https://developer.ebay.com/api-docs/static/oauth-client-credentials-grant.html
13
11
  class ClientCredentialsGrant
14
- include Sandboxable
12
+ include Requestable
15
13
 
16
- SANDBOX_ENDPOINT = 'https://api.sandbox.ebay.com/identity/v1/oauth2/token'
17
- PRODUCTION_ENDPOINT = 'https://api.ebay.com/identity/v1/oauth2/token'
14
+ self.endpoint = 'https://api.ebay.com/identity/v1/oauth2/token'
18
15
 
19
16
  # @return [String]
20
17
  attr_reader :app_id
@@ -40,16 +37,12 @@ module Ebay
40
37
  #
41
38
  # @return [HTTP::Response]
42
39
  def request
43
- HTTP.basic_auth(user: app_id, pass: cert_id)
44
- .post(url, form: payload)
40
+ http.basic_auth(user: app_id, pass: cert_id)
41
+ .post(endpoint, form: payload)
45
42
  end
46
43
 
47
44
  private
48
45
 
49
- def url
50
- sandbox? ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT
51
- end
52
-
53
46
  def payload
54
47
  { grant_type: 'client_credentials',
55
48
  scope: 'https://api.ebay.com/oauth/api_scope' }
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+
5
+ module Ebay
6
+ # Adds an HTTP client and ability to switch to the eBay Sandbox environment
7
+ module Requestable
8
+ class << self
9
+ private
10
+
11
+ def included(base)
12
+ class << base
13
+ attr_accessor :endpoint
14
+ end
15
+ end
16
+ end
17
+
18
+ # @return [HTTP::Client]
19
+ attr_writer :http
20
+
21
+ # @!attribute [r] headers
22
+ # @return [Hash]
23
+ attr_accessor :headers
24
+
25
+ # Sets the eBay Market
26
+ #
27
+ # @param [String]
28
+ def market_id=(market_id)
29
+ @headers ||= {}
30
+ @headers['X-EBAY-SOA-GLOBAL-ID'] = market_id
31
+ end
32
+
33
+ # @!attribute [r] http
34
+ # @return [HTTP::Client]
35
+ def http
36
+ @http ||= HTTP::Client.new
37
+ end
38
+
39
+ # @!attribute [r] endpoint
40
+ # @return [String]
41
+ def endpoint
42
+ @endpoint ||= self.class.endpoint
43
+ end
44
+
45
+ # Switches to the eBay Sandbox environment
46
+ #
47
+ # @return [self]
48
+ def sandbox
49
+ @endpoint = endpoint.sub('ebay', 'sandbox.ebay')
50
+ self
51
+ end
52
+
53
+ # Flags request as persistent
54
+ #
55
+ # @param [Integer] timeout
56
+ # @return [self]
57
+ def persistent(timeout: 5)
58
+ self.http = http.persistent(endpoint, timeout: timeout)
59
+ self
60
+ end
61
+
62
+ # @!method use(*features)
63
+ # Turns on {https://github.com/httprb/http HTTP} features
64
+ #
65
+ # @param features
66
+ # @return [self]
67
+ #
68
+ # @!method via(*proxy)
69
+ # Makes a request through an HTTP proxy
70
+ #
71
+ # @param [Array] proxy
72
+ # @raise [HTTP::Request::Error] if HTTP proxy is invalid
73
+ # @return [self]
74
+ %i[timeout via through use].each do |method_name|
75
+ define_method(method_name) do |*args, &block|
76
+ self.http = http.send(method_name, *args, &block)
77
+ self
78
+ end
79
+ end
80
+ end
81
+ end
data/lib/ebay/shopping.rb CHANGED
@@ -1,43 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'http'
4
-
5
3
  require 'ebay/config'
6
- require 'ebay/sandboxable'
4
+ require 'ebay/requestable'
7
5
 
6
+ # Ruby wrapper to the eBay APIs
8
7
  module Ebay
8
+ # Returns a {Ebay::Shopping#initialize Shopping API} instance
9
+ def self.shopping(**params)
10
+ Shopping.new(**params)
11
+ end
12
+
9
13
  # The eBay Shopping API makes it easy to search for things on eBay.
10
14
  #
11
15
  # @see https://developer.ebay.com/Devzone/shopping/docs/Concepts/ShoppingAPI_FormatOverview.html
12
16
  # @see https://developer.ebay.com/Devzone/shopping/docs/CallRef/index.html
13
17
  class Shopping
14
- include Sandboxable
18
+ include Requestable
15
19
 
16
- SANDBOX_ENDPOINT = 'https://open.api.sandbox.ebay.com/shopping'
17
- PRODUCTION_ENDPOINT = 'https://open.api.ebay.com/shopping'
20
+ self.endpoint = 'https://open.api.ebay.com/shopping'
18
21
 
19
22
  # @return [String]
20
23
  attr_reader :app_id
21
24
 
22
- # @return [String, nil]
25
+ # @return [String]
23
26
  attr_reader :response_encoding
24
27
 
25
- # @return [String, nil]
28
+ # @return [String]
26
29
  attr_reader :site_id
27
30
 
28
31
  # @return [String]
29
32
  attr_reader :version
30
33
 
31
- # @return [String, nil]
34
+ # @return [String]
32
35
  attr_reader :version_handling
33
36
 
34
- # @return [String, nil]
37
+ # @return [String]
35
38
  attr_reader :tracking_id
36
39
 
37
- # @return [String, nil]
40
+ # @return [String]
38
41
  attr_reader :tracking_partner_code
39
42
 
40
- # @return [String, nil]
43
+ # @return [String]
41
44
  attr_reader :affiliate_user_id
42
45
 
43
46
  # Returns a Finding API request instance
@@ -50,7 +53,7 @@ module Ebay
50
53
  # @param [String] tracking_id
51
54
  # @param [String] tracking_partner_code
52
55
  # @param [String] affiliate_user_id
53
- def initialize(app_id: Config.app_id, response_encoding: 'JSON',
56
+ def initialize(app_id: Config.app_id, response_encoding: nil,
54
57
  site_id: nil, version: '1119',
55
58
  version_handling: nil, tracking_id: nil,
56
59
  tracking_partner_code: nil, affiliate_user_id: nil)
@@ -79,7 +82,7 @@ module Ebay
79
82
  # @param [Hash] payload
80
83
  # @return [HTTP::Response]
81
84
  def get_category_info(category_id, payload = {})
82
- payload.update('CategoryID' => category_id)
85
+ payload = payload.merge('CategoryID' => category_id)
83
86
  request('GetCategoryInfo', payload)
84
87
  end
85
88
 
@@ -99,7 +102,7 @@ module Ebay
99
102
  # @return [HTTP::Response]
100
103
  def get_item_status(*item_ids)
101
104
  payload = item_ids.last.is_a?(Hash) ? item_ids.pop : {}
102
- payload.update('ItemID' => item_ids.join(','))
105
+ payload = payload.merge('ItemID' => item_ids.map(&:to_s))
103
106
  request('GetItemStatus', payload)
104
107
  end
105
108
 
@@ -111,7 +114,7 @@ module Ebay
111
114
  # @return [HTTP::Response]
112
115
  def get_multiple_items(*item_ids)
113
116
  payload = item_ids.last.is_a?(Hash) ? item_ids.pop : {}
114
- payload.update('ItemID' => item_ids.join(','))
117
+ payload = payload.merge('ItemID' => item_ids.map(&:to_s))
115
118
 
116
119
  request('GetMultipleItems', payload)
117
120
  end
@@ -122,7 +125,7 @@ module Ebay
122
125
  # @param [Hash] payload
123
126
  # @return [HTTP::Response]
124
127
  def get_shipping_costs(item_id, payload = {})
125
- payload.update('ItemID' => item_id)
128
+ payload = payload.merge('ItemID' => item_id)
126
129
  request('GetShippingCosts', payload)
127
130
  end
128
131
 
@@ -132,7 +135,7 @@ module Ebay
132
135
  # @param [Hash] payload
133
136
  # @return [HTTP::Response]
134
137
  def get_single_item(item_id, payload = {})
135
- payload.update('ItemID' => item_id)
138
+ payload = payload.merge('ItemID' => item_id)
136
139
  request('GetSingleItem', payload)
137
140
  end
138
141
 
@@ -142,14 +145,13 @@ module Ebay
142
145
  # @param [Hash] payload
143
146
  # @return [HTTP::Response]
144
147
  def get_user_profile(user_id, payload = {})
145
- payload.update('UserID' => user_id)
148
+ payload = payload.merge('UserID' => user_id)
146
149
  request('GetUserProfile', payload)
147
150
  end
148
151
 
149
152
  private
150
153
 
151
154
  def request(operation, payload = {})
152
- endpoint = sandbox? ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT
153
155
  params = { 'appid' => app_id,
154
156
  'callname' => operation,
155
157
  'requestencoding' => 'JSON',
@@ -161,7 +163,7 @@ module Ebay
161
163
  'trackingid' => tracking_id,
162
164
  'trackingpartnercode' => tracking_partner_code }.compact
163
165
 
164
- HTTP.post(endpoint, params: params, body: JSON.dump(payload))
166
+ http.headers(headers).post(endpoint, params: params, body: JSON.dump(payload))
165
167
  end
166
168
  end
167
169
  end
data/lib/ebay/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ebay
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ebay-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-17 00:00:00.000000000 Z
11
+ date: 2021-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: vcr
85
113
  requirement: !ruby/object:Gem::Requirement
@@ -122,7 +150,7 @@ dependencies:
122
150
  - - ">="
123
151
  - !ruby/object:Gem::Version
124
152
  version: '0'
125
- description:
153
+ description:
126
154
  email:
127
155
  - me@hakanensari.com
128
156
  executables: []
@@ -138,14 +166,14 @@ files:
138
166
  - lib/ebay/finding.rb
139
167
  - lib/ebay/merchandising.rb
140
168
  - lib/ebay/oauth/client_credentials_grant.rb
141
- - lib/ebay/sandboxable.rb
169
+ - lib/ebay/requestable.rb
142
170
  - lib/ebay/shopping.rb
143
171
  - lib/ebay/version.rb
144
172
  homepage: https://github.com/hakanensari/ebay-ruby
145
173
  licenses:
146
174
  - MIT
147
175
  metadata: {}
148
- post_install_message:
176
+ post_install_message:
149
177
  rdoc_options: []
150
178
  require_paths:
151
179
  - lib
@@ -153,15 +181,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
181
  requirements:
154
182
  - - ">="
155
183
  - !ruby/object:Gem::Version
156
- version: '2.4'
184
+ version: '2.5'
157
185
  required_rubygems_version: !ruby/object:Gem::Requirement
158
186
  requirements:
159
187
  - - ">="
160
188
  - !ruby/object:Gem::Version
161
189
  version: '0'
162
190
  requirements: []
163
- rubygems_version: 3.1.2
164
- signing_key:
191
+ rubygems_version: 3.2.3
192
+ signing_key:
165
193
  specification_version: 4
166
194
  summary: Ruby wrapper to the eBay APIs
167
195
  test_files: []
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'ebay/config'
4
-
5
- module Ebay
6
- # Allows running requests in the eBay Sandbox
7
- module Sandboxable
8
- # Runs requests in the eBay Sandbox
9
- #
10
- # @return [self]
11
- def sandbox
12
- @sandbox = true
13
- self
14
- end
15
-
16
- # @!attribute [r] sandbox?
17
- # Returns whether requests run in the eBay Sandbox
18
- #
19
- # @return [Boolean]
20
- def sandbox?
21
- @sandbox ||= false
22
- end
23
- end
24
- end