Class: CardmarketCLI::Entities::MetaProduct
- 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
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
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 = {}) ⇒ MetaProduct
constructor
A new instance of MetaProduct.
- #meta? ⇒ Boolean
- #products ⇒ Object
- #read ⇒ Object
Methods included from Unique
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, account, params = {}) super(id, account, { products: [] }) merge_params(params) MetaProduct.send(:add, self) end |
Instance Attribute Details
#updated_at ⇒ Object (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, account, params = {}) self[id]&.send(:merge_params, params) || new(id, account, 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(account, hash) hash.transform_keys! { |key| key.underscore.to_sym } hash[:products] = [] hash.delete(:product)&.each do |product| hash[:products] << Product.from_hash(account, product) end MetaProduct.create(hash[:id_metaproduct], account, 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(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)['metaproduct']&.each do |product| array << from_hash(account, 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(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
24 25 26 |
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 24 def true end |
#products ⇒ Object
28 29 30 31 |
# File 'lib/cardmarket_cli/entities/meta_product.rb', line 28 def products read unless params[:products] params[:products].dup end |
#read ⇒ Object
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 = account.get("#{PATH_BASE}/#{id}") hash = JSON.parse(response.response_body) hash['metaproduct']&.store('product', hash['product']) MetaProduct.from_hash(account, hash['metaproduct']) end |