class CardmarketCLI::Entities::Product

See api.cardmarket.com/ws/documentation/API_2.0:Entities:Product

Constants

PARAMS
PATH_BASE

Private Class Methods

create(id, account, params = {}) click to toggle source
# File lib/cardmarket_cli/entities/product.rb, line 62
def create(id, account, params = {})
  self[id]&.send(:merge_params, params) || new(id, account, params)
end
from_hash(account, hash) click to toggle source
# File lib/cardmarket_cli/entities/product.rb, line 54
def from_hash(account, hash)
  hash.transform_keys! { |key| key.underscore.to_sym }
  hash[:meta_product] = MetaProduct.create(hash.delete(:id_metaproduct), account)
  product = Product.create(hash[:id_product], account, hash)
  hash[:meta_product]&.send(:merge_params, { products: [product] })
  product
end
new(id, account, params = {}) click to toggle source
Calls superclass method CardmarketCLI::Entities::Entity::new
# File lib/cardmarket_cli/entities/product.rb, line 16
def initialize(id, account, params = {})
  super(id, account, {})
  merge_params(params)
  Product.send(:add, self)
end
search_all(account, search_string, exact: false) click to toggle source
# File lib/cardmarket_cli/entities/product.rb, line 66
def search_all(account, search_string, exact: false)
  start = 0
  products = []
  loop do
    search(account, search_string, products, start, exact: exact)
    break unless products.count == start += 100
  end
  products
end

Public Instance Methods

meta?() click to toggle source
# File lib/cardmarket_cli/entities/product.rb, line 22
def meta?
  false
end
price_guide() click to toggle source
# File lib/cardmarket_cli/entities/product.rb, line 34
def price_guide
  params[:price_guide].dup
end
read() click to toggle source
# File lib/cardmarket_cli/entities/product.rb, line 26
def read
  LOGGER.debug("Reading Product #{en_name}(#{id})")
  response = account.get("#{PATH_BASE}/#{id}")
  hash = JSON.parse(response.response_body)['product']
  hash['expansion_name'] ||= hash['expansion']&.fetch('enName')
  Product.from_hash(account, hash)
end

Private Instance Methods

merge_params(params) click to toggle source
# File lib/cardmarket_cli/entities/product.rb, line 40
def merge_params(params)
  params[:price_guide]&.transform_keys! { |key| key.to_s.underscore.to_sym }&.delete(nil)
  self.params.merge!(params.slice(*PARAMS))
  self.params[:rarity] = self.params[:rarity]&.to_s&.downcase!&.to_sym
  @updated_at = Time.now
  self
end