Class: CardmarketCLI::Entities::MetaProduct

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

Overview

Constant Summary collapse

PARAMS =
i[en_name loc_name products].freeze
PATH_BASE =
'metaproducts'

Instance Attribute Summary collapse

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 = {}) ⇒ MetaProduct

Returns a new instance of MetaProduct.



18
19
20
21
22
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 18

def initialize(id, , params = {})
  super(id, , { products: [] })
  merge_params(params)
  MetaProduct.send(:add, self)
end

Instance Attribute Details

#updated_atObject (readonly)

Returns the value of attribute updated_at.



16
17
18
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 16

def updated_at
  @updated_at
end

Class Method Details

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



65
66
67
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 65

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

.from_hash(account, hash) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 56

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

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



79
80
81
82
83
84
85
86
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 79

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)['metaproduct']&.each do |product|
    array << from_hash(, product)
  end
  array
end

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



69
70
71
72
73
74
75
76
77
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 69

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)


24
25
26
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 24

def meta?
  true
end

#productsObject



28
29
30
31
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 28

def products
  read unless params[:products]
  params[:products].dup
end

#readObject



33
34
35
36
37
38
39
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 33

def read
  LOGGER.debug("Reading Metaproduct #{en_name}(#{id})")
  response = .get("#{PATH_BASE}/#{id}")
  hash = JSON.parse(response.response_body)
  hash['metaproduct']&.store('product', hash['product'])
  MetaProduct.from_hash(, hash['metaproduct'])
end