Class: TCGPlayerSDK
- Inherits:
-
Object
- Object
- TCGPlayerSDK
- Defined in:
- lib/tcg-player-sdk/tcg_player_sdk.rb
Overview
Wrap up request handling and common functions for TCGPlayer price API
Defined Under Namespace
Classes: BearerToken, Manifest, Pokemon, ProductPrice, ProductPriceList, ResponseStruct
Constant Summary collapse
- API_VERSION =
'1.39'
- BASE_URL =
'https://api.tcgplayer.com'
- TOKEN_URL =
"#{BASE_URL}/token"
- CATALOG_URL =
"#{BASE_URL}/catalog"
- CATEGORIES_URL =
"#{CATALOG_URL}/categories"
- PRICING_URL =
"#{BASE_URL}/pricing"
Instance Attribute Summary collapse
-
#bearer_token ⇒ Object
Returns the value of attribute bearer_token.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#noretry ⇒ Object
Returns the value of attribute noretry.
-
#private_key ⇒ Object
Returns the value of attribute private_key.
-
#public_key ⇒ Object
Returns the value of attribute public_key.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#authorize(params = {}) ⇒ TCGPlayerSDK::BearerToken
Get a new bearer token.
- #categories(params = {}) ⇒ TCGPlayerSDK::ResponseStruct
- #category_details(ids) ⇒ TCGPlayerSDK::ResponseStruct
- #category_search_manifest(id) ⇒ TCGPlayerSDK::ResponseStruct
- #category_search_products(id, params = {}) ⇒ TCGPlayerSDK::ResponseStruct
-
#initialize(params = {}) ⇒ TCGPlayerSDK
constructor
A new instance of TCGPlayerSDK.
- #product_details(_ids, params = {}) ⇒ Object
-
#product_pricing(_ids) ⇒ TCGPlayerSDK::ProductPriceList
Accessor to docs.tcgplayer.com/reference/pricing_getproductprices-1 Automatically handles arbitrarily large number of _ids and provides one merged response.
-
#query(url, _params = {}) ⇒ TCGPlayerSDK::ResponseStruct
Perform a query on the TCGPlayer API.
Constructor Details
#initialize(params = {}) ⇒ TCGPlayerSDK
Returns a new instance of TCGPlayerSDK.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 20 def initialize(params = {}) self.user_agent = params[:user_agent] || 'Unknown' self.bearer_token = params[:bearer_token] self.noretry = params[:noretry] self.public_key = params[:public_key] self.private_key = params[:private_key] # Setup logging self.logger = params[:logger] || Logger.new(STDOUT) if(params[:debug]) self.logger.level = Logger::DEBUG else self.logger.level = Logger::WARN end end |
Instance Attribute Details
#bearer_token ⇒ Object
Returns the value of attribute bearer_token.
4 5 6 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 4 def bearer_token @bearer_token end |
#logger ⇒ Object
Returns the value of attribute logger.
4 5 6 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 4 def logger @logger end |
#noretry ⇒ Object
Returns the value of attribute noretry.
4 5 6 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 4 def noretry @noretry end |
#private_key ⇒ Object
Returns the value of attribute private_key.
4 5 6 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 4 def private_key @private_key end |
#public_key ⇒ Object
Returns the value of attribute public_key.
4 5 6 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 4 def public_key @public_key end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
4 5 6 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 4 def user_agent @user_agent end |
Instance Method Details
#authorize(params = {}) ⇒ TCGPlayerSDK::BearerToken
Get a new bearer token. Specify your app's public and private key as parameters or via Environment variables (or via .env) TCG_PLAYER_API_PUBLIC_KEY and TCG_PLAYER_API_PRIVATE_KEY
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 45 def (params = {}) pub_key = params[:public_key] || public_key || ENV['TCG_PLAYER_API_PUBLIC_KEY'] pri_key = params[:private_key] || private_key || ENV['TCG_PLAYER_API_PRIVATE_KEY'] #"grant_type=client_credentials&client_id=PUBLIC_KEY&client_secret=PRIVATE_KEY" query_params = {grant_type: 'client_credentials', client_id: pub_key, client_secret: pri_key} response = HTTP.post(TOKEN_URL, form: query_params) resp_hash = response.parse logger.info resp_hash self.bearer_token = BearerToken.new resp_hash end |
#categories(params = {}) ⇒ TCGPlayerSDK::ResponseStruct
102 103 104 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 102 def categories(params = {}) query(CATEGORIES_URL, params) end |
#category_details(ids) ⇒ TCGPlayerSDK::ResponseStruct
110 111 112 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 110 def category_details(ids) query("#{CATEGORIES_URL}/#{ids.join(',')}") end |
#category_search_manifest(id) ⇒ TCGPlayerSDK::ResponseStruct
117 118 119 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 117 def category_search_manifest(id) Manifest.new(query("#{CATEGORIES_URL}/#{id}/search/manifest")) end |
#category_search_products(id, params = {}) ⇒ TCGPlayerSDK::ResponseStruct
149 150 151 152 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 149 def category_search_products(id, params = {}) search_params = {post: true}.merge(params) query("#{CATEGORIES_URL}/#{id}/search", search_params) end |
#product_details(_ids, params = {}) ⇒ Object
157 158 159 160 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 157 def product_details(_ids, params = {}) ids = id_list(_ids) query("#{CATALOG_URL}/products/#{ids}", params) end |
#product_pricing(_ids) ⇒ TCGPlayerSDK::ProductPriceList
Accessor to docs.tcgplayer.com/reference/pricing_getproductprices-1 Automatically handles arbitrarily large number of _ids and provides one merged response
168 169 170 171 172 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 168 def product_pricing(_ids) slice_ids(_ids) do |slice| TCGPlayerSDK::ProductPriceList.new(query("#{PRICING_URL}/product/#{id_list(slice)}")) end end |
#query(url, _params = {}) ⇒ TCGPlayerSDK::ResponseStruct
Perform a query on the TCGPlayer API
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 68 def query(url, _params = {}) params = _params.dup post = params.delete :post method = post ? 'post' : 'get' pkey = post ? :json : :params skip_retry = params.delete(:noretry) || noretry logger.debug "Query: #{url} params: " logger.ap params # Check for expiration of bearer token response = HTTP.auth("bearer #{bearer_token ? bearer_token.token : 'none'}").headers('User-Agent' => user_agent).send(method, url, pkey => params) ret = ResponseStruct.new response.parse.merge({base_query: {url: url, params: _params}, http_response: response, tcg_object: self}) # Detect an invalid bearer token and attempt to retry if(!skip_retry && ret.errors && ret.errors.size > 0 && ret.errors.reduce(false){|sum, err| sum = (sum || (err =~ /bearer token/))}) # Reauthorize and try again ret = query(url, _params.merge({noretry: true})) end return ret end |