Class: CardmarketCLI::Entities::Product
- Extended by:
- Unique
- Defined in:
- lib/cardmarket_cli/entities/product.rb
Overview
Constant Summary collapse
- PARAMS =
i[en_name loc_name expansion_name rarity count_articles count_foils price_guide].freeze
- PATH_BASE =
'products'
Instance Attribute Summary
Attributes inherited from Entity
Class Method Summary collapse
- .create(id, account, params = {}) ⇒ Object
- .from_hash(account, hash) ⇒ Object
- .search(account, search_string, array, start, exact: false) ⇒ Object
- .search_all(account, search_string, exact: false) ⇒ Object
Instance Method Summary collapse
-
#initialize(id, account, params = {}) ⇒ Product
constructor
A new instance of Product.
- #meta? ⇒ Boolean
- #price_guide ⇒ Object
- #read ⇒ Object
Methods included from Unique
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, account, params = {}) super(id, account, {}) 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, account, params = {}) self[id]&.send(:merge_params, params) || new(id, account, 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(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 |
.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(account, search_string, array, start, exact: false) response = account.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(account, 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(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 |
Instance Method Details
#meta? ⇒ Boolean
22 23 24 |
# File 'lib/cardmarket_cli/entities/product.rb', line 22 def false end |
#price_guide ⇒ Object
34 35 36 |
# File 'lib/cardmarket_cli/entities/product.rb', line 34 def price_guide params[:price_guide].dup end |
#read ⇒ Object
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 = 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 |