Class: CardmarketCLI::Entities::Product

Inherits:
Entity
  • Object
show all
Extended by:
Unique
Defined in:
lib/cardmarket_cli/entities/product.rb

Overview

Constant Summary collapse

PARAMS =
i[en_name loc_name meta_product expansion_name rarity count_articles count_foils price_guide].freeze
PATH_BASE =
'products'

Instance Attribute Summary

Attributes inherited from Entity

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Unique

uniq_attr

Constructor Details

#initialize(id, account, params = {}) ⇒ Product

Returns a new instance of Product.



16
17
18
19
20
# File 'lib/cardmarket_cli/entities/product.rb', line 16

def initialize(id, , params = {})
  super(id, , {})
  merge_params(params)
  Product.send(:add, self)
end

Class Method Details

.create(id, account, params = {}) ⇒ Object



62
63
64
# File 'lib/cardmarket_cli/entities/product.rb', line 62

def create(id, , params = {})
  self[id]&.send(:merge_params, params) || new(id, , params)
end

.from_hash(account, hash) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/cardmarket_cli/entities/product.rb', line 54

def from_hash(, hash)
  hash.transform_keys! { |key| key.underscore.to_sym }
  hash[:meta_product] = MetaProduct.create(hash.delete(:id_metaproduct), )
  product = Product.create(hash[:id_product], , hash)
  hash[:meta_product]&.send(:merge_params, { products: [product] })
  product
end

.search(account, search_string, array, start, exact: false) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/cardmarket_cli/entities/product.rb', line 76

def search(, search_string, array, start, exact: false)
  response = .get("#{PATH_BASE}/find", params: { search_all: search_string, exact: exact, start: start,
                                                        maxResults: 100, idGame: 1, idLanguage: 1 })
  JSON.parse(response.response_body)['product']&.each do |product|
    array << from_hash(, product)
  end
  array
end

.search_all(account, search_string, exact: false) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/cardmarket_cli/entities/product.rb', line 66

def search_all(, search_string, exact: false)
  start = 0
  products = []
  loop do
    search(, search_string, products, start, exact: exact)
    break unless products.count == start += 100
  end
  products
end

Instance Method Details

#meta?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/cardmarket_cli/entities/product.rb', line 22

def meta?
  false
end

#price_guideObject



34
35
36
# File 'lib/cardmarket_cli/entities/product.rb', line 34

def price_guide
  params[:price_guide].dup
end

#readObject



26
27
28
29
30
31
32
# File 'lib/cardmarket_cli/entities/product.rb', line 26

def read
  LOGGER.debug("Reading Product #{en_name}(#{id})")
  response = .get("#{PATH_BASE}/#{id}")
  hash = JSON.parse(response.response_body)['product']
  hash['expansion_name'] ||= hash['expansion']&.fetch('enName')
  Product.from_hash(, hash)
end