tcg-player-sdk 0.1.3 → 0.1.6

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: 444973875234b87717e9ece0dd3c0373d707d2a13bac732330f7ab8d6a740f72
4
- data.tar.gz: cc526ef5f4bc6cadb4404096dfdf9b5a414b5a438d99c4ddf58171b2b6beb214
3
+ metadata.gz: 57bcdd3797fe1ab87b4f352456e185b75063ceb7e1f7bdac1b4274758fd16e39
4
+ data.tar.gz: 9506983271a359e317fd37786d435f306fef9f59e6a19b31a90f0cfe4dd80407
5
5
  SHA512:
6
- metadata.gz: cdede9b22fe30a6f4106af572ca2f0523e6989ec426de0d307fdc37252cfee511ed6275bae255d954eb9cd386465042d650ac78a6096e522d2810cbe26d1d6bb
7
- data.tar.gz: e4649464a2dd938a07b9ee991cd1b9635248d36685f4f301feb31a5dcded44c43446f32e6a5172cf688938d2e36725c4277b1ef2e2e4e716f0aef7d75445b91a
6
+ metadata.gz: 06d79171151bc20f90e6340e6c94b1149f148bf1179bbb9d871997dd7c85f23d5d208698e95af527f5fd4d7413ee0f3df66e9f3bbce5b290cad024f02d906557
7
+ data.tar.gz: eda02b612a049a0c3e5bead6b560e86b30804121b82599f1065b8b013acad605fb6e6cf5f6101f48a3c4303e4f3f6f723842d0db41db31e7d26b8ae14287e0de
@@ -17,4 +17,14 @@ class TCGPlayerSDK::BearerToken
17
17
  def to_s
18
18
  ap self, indent: -2
19
19
  end
20
+
21
+ def to_json
22
+ h = {
23
+ access_token: token,
24
+ token_type: 'bearer',
25
+ expires_in: expires_in,
26
+ '.issued' => issued.to_s,
27
+ '.expires' => expiration.to_s,
28
+ }.to_json
29
+ end
20
30
  end
@@ -0,0 +1,38 @@
1
+ # Add some shortcuts and helpers to parse the manifest
2
+ class TCGPlayerSDK::Manifest < TCGPlayerSDK::ResponseStruct
3
+
4
+ def initialize(hash = nil)
5
+ super
6
+ end
7
+
8
+ ##
9
+ # Pick out the sorting struct from the response
10
+ #
11
+ # @return [Array<TCGPlayerSDK::ResponseStruct>]
12
+ def sorting
13
+ if(self.results && self.results.is_a?(Array))
14
+ @sorting ||= self.results.first.sorting
15
+ end
16
+
17
+ return @sorting
18
+ end
19
+
20
+ ##
21
+ # @return [Array<String>] The actual values that are legitimate inputs to search query's sort field
22
+ def sort_values
23
+ _sorting = sorting || []
24
+ @sort_values ||= _sorting.map(&:value)
25
+ end
26
+
27
+ ##
28
+ # Pick out the filters struct from the response
29
+ #
30
+ # @return [Array<TCGPlayerSDK::ResponseStruct>]
31
+ def filters
32
+ if(self.results && self.results.is_a?(Array))
33
+ @filters ||= self.results.first.filters
34
+ end
35
+
36
+ return @filters
37
+ end
38
+ end
@@ -25,6 +25,13 @@ class TCGPlayerSDK::Pokemon
25
25
  @set_filter ||= @manifest.results.first.filters.select{|f| f.name == 'SetName'}.first
26
26
  end
27
27
 
28
+ ##
29
+ # @return [Array<TCGPlayerSDK::ResponseStruct>] Response struct with keys :value and :text, which correspond to
30
+ # the available ways to sort search data. Set :sort in your search parameters to one of the returned :value
31
+ def sorting
32
+ @sorting ||= @manifest.results.first.sorting
33
+ end
34
+
28
35
  ##
29
36
  # Returns the TCGPlayerSDK filter item corresponding to the input set
30
37
  #
@@ -1,19 +1,7 @@
1
- class TCGPlayerSDK::ProductPriceList
2
- attr_accessor :response
1
+ class TCGPlayerSDK::ProductPriceList < TCGPlayerSDK::ResponseStruct
3
2
 
4
- ##
5
- # Group prices by product ID. This will set
6
- # prices to an empty hash if there was an error. Check response.errors for details about any errors.
7
- #
8
- # @param _response[TCGPlayerSDK::ResponseStruct] the result of calling TCGPlayerAPI#product_pricing
9
- def initialize(_response)
10
- @response = _response
11
-
12
- if(response.success && response.results)
13
- @prices = response.results.map{|r| TCGPlayerSDK::ProductPrice.new(r)}.group_by{|r| r.productId}
14
- else
15
- @prices = {}
16
- end
3
+ def initialize(hash = {})
4
+ super
17
5
  end
18
6
 
19
7
  ##
@@ -43,6 +31,12 @@ class TCGPlayerSDK::ProductPriceList
43
31
  #
44
32
  # @return [Hash<Integer, Array<TCGPlayerSDK::ProductPrice>>]
45
33
  def prices
34
+ if(self.success && self.results)
35
+ @prices ||= self.results.map{|r| TCGPlayerSDK::ProductPrice.new(r)}.group_by{|r| r.productId}
36
+ else
37
+ @prices = {}
38
+ end
39
+
46
40
  @prices
47
41
  end
48
42
 
@@ -51,13 +45,15 @@ class TCGPlayerSDK::ProductPriceList
51
45
  #
52
46
  # @return [Hash<Integer, Array<TCGPlayerSDK::ProductPrice>>]
53
47
  def valid_prices
54
- valid_prices = {}
55
- @prices.each do |id, prl|
56
- valid_prices[id] ||= []
57
- valid_prices[id] = prl.select{|pr| pr.has_valid_prices?}
48
+ if(@valid_prices.nil?)
49
+ @valid_prices = {}
50
+ prices.each do |id, prl|
51
+ @valid_prices[id] ||= []
52
+ @valid_prices[id] = prl.select{|pr| pr.has_valid_prices?}
53
+ end
58
54
  end
59
55
 
60
- return valid_prices
56
+ return @valid_prices
61
57
  end
62
58
  end
63
59
 
@@ -81,4 +77,14 @@ class TCGPlayerSDK::ProductPrice < TCGPlayerSDK::ResponseStruct
81
77
  def has_valid_prices?
82
78
  return !(lowPrice.nil? && midPrice.nil? && highPrice.nil? && directLowPrice.nil? && marketPrice.nil?)
83
79
  end
80
+
81
+ ##
82
+ # Returns "price points" described by this price object. i.e. "midPrice" and "highPrice". These are also accessors,
83
+ # so access away
84
+ # price.points.each{|pp| puts price.send(pp)}
85
+ #
86
+ # @return Array<Symbol>
87
+ def points
88
+ self.keys.select{|k| k.to_s =~ /Price/i}
89
+ end
84
90
  end
@@ -115,28 +115,90 @@ class TCGPlayerSDK
115
115
  #
116
116
  # @return [TCGPlayerSDK::ResponseStruct]
117
117
  def category_search_manifest(id)
118
- query("#{CATEGORIES_URL}/#{id}/search/manifest")
118
+ Manifest.new(query("#{CATEGORIES_URL}/#{id}/search/manifest"))
119
119
  end
120
120
 
121
121
  # https://docs.tcgplayer.com/reference/catalog_searchcategory
122
122
  #
123
+ # @param id[String] The category ID to search through
124
+ # @param params[Hash] Put your additional search terms here:
125
+ # - sort: One of the available sort filters see manifest.results.first.sorting
126
+ # - limit: Cap the number of results to this limit
127
+ # - offset: Used with :limit to return a limited number of results in an arbitrary location. i.e. for pagination
128
+ # - filters[Array<Hash>] An array of filters as described by manifest.results.first.filters. Use the following format for Hash in the filters array
129
+ # - name[String]: Name of one of the filters from the manifest
130
+ # - values[Array<String>]: Specify which values to filter on
131
+ #
132
+ # params = {
133
+ # sort: 'ProductName ASC',
134
+ # limit: 10,
135
+ # offset: 0,
136
+ # filters: [ {
137
+ # name: 'ProductName',
138
+ # values: ['Alakazam']
139
+ # }]
140
+ # }
141
+ #
142
+ # results = tcg.category_search_products(3, params)
143
+ # results.results #=> [42444, 42346, 83496, 106996, 83501, 83499, 83497, 83500, 83498, 180715]
144
+ #
145
+ # # Do something with each product ID... will automatically fetch new results
146
+ # all_ids = results.map{|r| r} #=> [42444, 42346, 83496, 106996, 83501, 83499, 83497, 83500, 83498, 180715, 83503, 83504, 117512, 117889, 117896, 117863, 83502, 226606, 228981, 228980, 228979, 251560, 84559, 84560, 118332, 117515, 117890, 88866]
147
+ #
123
148
  # @return [TCGPlayerSDK::ResponseStruct]
124
149
  def category_search_products(id, params = {})
125
150
  search_params = {post: true}.merge(params)
126
151
  query("#{CATEGORIES_URL}/#{id}/search", search_params)
127
152
  end
128
153
 
129
- #def product_details(pids, params = {})
130
- # query("#{CATEGORIES_URL}/#{id}/search", search_params)
131
- #end
154
+ ##
155
+ # https://docs.tcgplayer.com/reference/catalog_getproduct-1
156
+ #
157
+ def product_details(_ids, params = {})
158
+ slice_ids(_ids) do |slice|
159
+ query("#{CATALOG_URL}/products/#{id_list(slice)}", params)
160
+ end
161
+ end
132
162
 
133
163
  ##
134
164
  # Accessor to https://docs.tcgplayer.com/reference/pricing_getproductprices-1
165
+ # Automatically handles arbitrarily large number of _ids and provides one merged response
135
166
  #
136
- # @param ids An array of product IDs to query
167
+ # @param _ids An array of product IDs to query
137
168
  # @return [TCGPlayerSDK::ProductPriceList]
138
169
  def product_pricing(_ids)
139
- ids = _ids.is_a?(Array) ? _ids.join(',') : _ids
140
- TCGPlayerSDK::ProductPriceList.new(query("#{PRICING_URL}/product/#{ids}"))
170
+ slice_ids(_ids) do |slice|
171
+ TCGPlayerSDK::ProductPriceList.new(query("#{PRICING_URL}/product/#{id_list(slice)}"))
172
+ end
173
+ end
174
+
175
+ private
176
+
177
+ ##
178
+ # Sanitize an array or string of ids to be compatible with the comma-separated values that the API expects
179
+ #
180
+ # @param value Accepts either an Array or String to convert to the API-friendly formatting
181
+ # @return [String] ids separated by comma
182
+ def id_list(value)
183
+ value.is_a?(Array) ? value.join(',') : value
184
+ end
185
+
186
+ ##
187
+ # Slice the input ids into smaller pieces, and execute the given block with each slice. Merge the results
188
+ def slice_ids(_ids, slice_size=200)
189
+ ids = _ids.is_a?(Array) ? _ids : [_ids]
190
+ resp = nil
191
+
192
+ ids.each_slice(slice_size) do |slice|
193
+ response = yield(slice)
194
+ if(resp.nil?)
195
+ resp = response
196
+ else
197
+ resp.results += response.results if(response.results && resp.results)
198
+ end
199
+ end
200
+
201
+ return resp
141
202
  end
203
+
142
204
  end
@@ -8,4 +8,5 @@ require 'tcg-player-sdk/bearer_token'
8
8
  require 'tcg-player-sdk/response_struct'
9
9
  require 'tcg-player-sdk/product_price_list'
10
10
  require 'tcg-player-sdk/pokemon'
11
+ require 'tcg-player-sdk/manifest'
11
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tcg-player-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Svensson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-24 00:00:00.000000000 Z
11
+ date: 2022-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -62,6 +62,7 @@ extra_rdoc_files: []
62
62
  files:
63
63
  - lib/tcg-player-sdk.rb
64
64
  - lib/tcg-player-sdk/bearer_token.rb
65
+ - lib/tcg-player-sdk/manifest.rb
65
66
  - lib/tcg-player-sdk/pokemon.rb
66
67
  - lib/tcg-player-sdk/product_price_list.rb
67
68
  - lib/tcg-player-sdk/response_struct.rb
@@ -85,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  - !ruby/object:Gem::Version
86
87
  version: '0'
87
88
  requirements: []
88
- rubygems_version: 3.1.2
89
+ rubygems_version: 3.1.4
89
90
  signing_key:
90
91
  specification_version: 4
91
92
  summary: A Ruby interface to the TCGPlayer.com API for trading card prices.