Class: TCGPlayerSDK

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ TCGPlayerSDK

Returns a new instance of TCGPlayerSDK.

Parameters:

  • params (Hash) (defaults to: {})
    • user_agent: An identifying user agent string to be passed along with each request

    • bearer_token: Optionally pass in a previously authenticated bearer token

    • noretry: Set this to true to disable retrying queries when the bearer token is invalid

    • logger: Optionally pass a custom logging object

    • debug: Set output verbosity to DEBUG. Default verbosity is WARN



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_tokenObject

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

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 4

def logger
  @logger
end

#noretryObject

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_keyObject

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_keyObject

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_agentObject

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

Parameters:

  • params (Hash) (defaults to: {})
    • public_key: your TCP Player API pubic key

    • private_key: your TCP Player API pubic key

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tcg-player-sdk/tcg_player_sdk.rb', line 45

def authorize(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

Parameters:

  • params (Hash) (defaults to: {})
    • limit: max to return (default 10)

    • offset: number of categories to skip (for paging)

    • sort_order: property to sort by (defaults to name)

    • sort_desc: descending sort order

Returns:



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

Parameters:

Returns:



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

Parameters:

  • _ids

    An array of product IDs to query

Returns:



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

Parameters:

  • url

    The API endpoint url, without arguments

  • _params (Hash) (defaults to: {})
    • post: When true, use a post request instead of a get request

    • noretry: Override any other retry settings and skip retry if true

    • *: Additional entries in _params hash are passed through to API as arguments

Returns:



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
    authorize
    ret = query(url, _params.merge({noretry: true}))
  end

  return ret
end