Class: CardmarketCLI::Entities::WantslistItem

Inherits:
Changeable show all
Defined in:
lib/cardmarket_cli/entities/wantslist_item.rb

Overview

Constant Summary collapse

PARAMS =
%i[count from_price min_condition wish_price mail_alert languages is_foil is_altered is_playset is_signed
is_first_ed].freeze

Instance Attribute Summary collapse

Attributes inherited from Entity

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Changeable

#changed?

Constructor Details

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

Returns a new instance of WantslistItem.



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

def initialize(id, product, , params = {})
  params = { count: 1, min_condition: :PO, wish_price: 0.0, mail_alert: false, languages: [1], is_foil: nil,
             is_altered: nil, is_playset: nil, is_signed: nil, is_first_ed: nil, from_price: nil }.merge!(params)
  super(id, , params.slice(*PARAMS))
  @product = product
end

Instance Attribute Details

#productObject (readonly)

Returns the value of attribute product.



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

def product
  @product
end

Class Method Details

.from_hash(account, hash) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/cardmarket_cli/entities/wantslist_item.rb', line 58

def from_hash(, hash)
  product = create_product(, hash)
  hash.transform_keys! { |key| key.underscore.to_sym }
  hash[:min_condition] &&= hash[:min_condition].to_sym
  hash[:languages] = hash[:id_language]
  WantslistItem.new(hash[:id_want], product, , hash)
end

Instance Method Details

#add_product_id(hash) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/cardmarket_cli/entities/wantslist_item.rb', line 35

def add_product_id(hash)
  return if hash['idWant']

  if @product.meta?
    hash[:idMetaproduct] = @product.id
  else
    hash[:idProduct] = @product.id
  end
end

#languagesObject



49
50
51
# File 'lib/cardmarket_cli/entities/wantslist_item.rb', line 49

def languages
  params[:languages]&.uniq!
end

#languages=(value) ⇒ Object



53
54
55
# File 'lib/cardmarket_cli/entities/wantslist_item.rb', line 53

def languages=(value)
  params[:languages] = value.uniq
end

#meta?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/cardmarket_cli/entities/wantslist_item.rb', line 45

def meta?
  product.meta?
end

#to_xml_hashObject



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

def to_xml_hash
  hash = params.compact.transform_keys! { |key| key.to_s.camelize }
  hash['idLanguage'] = hash.delete('languages')&.uniq!
  hash['idWant'] = id
  hash.delete_if { |_, value| value.nil? || (value.respond_to?(:empty?) && value.empty?) }

  add_product_id(hash)
  hash
end