class CardmarketCLI::Entities::Wantslist

see api.cardmarket.com/ws/documentation/API_2.0:Entities:Wantslist

Constants

PARAMS
PATH_BASE

Public Class Methods

new(id, account, params = {}) click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 21
def initialize(id, account, params = {})
  super(id, account, params.slice(*PARAMS))
  Wantslist.send(:add, self)
end

Private Class Methods

create(*args) click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 124
def create(*args)
  new(*args)
end
from_hash(account, hash) click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 128
def from_hash(account, hash)
  return nil unless hash['game']&.fetch('idGame') == 1

  list = Wantslist.new(hash['idWantsList'], account, name: hash['name'])
  hash['item']&.each { |item| list.add_item(WantslistItem.from_hash(account, item)) }
  list
end
read(account, eager: true) click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 108
def read(account, eager: true)
  LOGGER.debug('Reading wantslists')
  response = account.get(PATH_BASE)
  hash = JSON.parse(response.response_body)
  hash['wantslist']&.each do |item|
    list = from_hash(account, item)
    list.read if eager && list
  end
  instances
end
update() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 119
def update
  instances.each(&:update)
  deleted_instances.each(&:delete)
end

Public Instance Methods

delete() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 40
def delete
  LOGGER.debug("Deleting wantslist #{name}(#{id})")
  return unless id

  account.delete(path, body: { action: 'deleteWantslist' })
end
path() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 26
def path
  "#{PATH_BASE}/#{id}"
end
read() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 30
def read
  LOGGER.debug("Reading wantslist #{name}(#{id})")
  return unless id

  response = account.get(path)
  override_params(JSON.parse(response.response_body)['wantslist'])

  response
end
update() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 47
def update
  LOGGER.debug("Updating wantslist #{name}(#{id})")
  responses = {}
  responses[:create] = create unless id
  responses[:update] = patch if changed?
  patch_items(responses)
  responses.compact!
end

Private Instance Methods

create() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 70
def create
  response = account.post(PATH_BASE, body: { wantslist: { name: name, idGame: 1 } })
  self.id = JSON.parse(response)['wantslist']&.fetch(0)&.fetch('idWantsList')
  response
end
create_items() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 80
def create_items
  new_items = @items.reject(&:id)
  return nil if new_items.empty?

  account.put(path, body: { action: 'addItem',
                            product: new_items.reject(&:meta?).map!(&:to_xml_hash),
                            metaproduct: new_items.select(&:meta?).map!(&:to_xml_hash) })
end
delete_items() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 96
def delete_items
  return nil if deleted_items.empty?

  account.put(path, body: { action: 'deleteItem', want: @deleted_items.map { |item| { idWant: item.id } } })
end
override_params(hash) click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 58
def override_params(hash)
  params[:name] = hash['name']
  clear
  self.changed = false
  hash['item']&.each { |item| add_item(WantslistItem.from_hash(account, item)) }
end
patch() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 76
def patch
  account.put(path, body: { action: 'editWantslist', name: name })
end
patch_items(responses) click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 65
def patch_items(responses)
  %i[create_items update_items delete_items].each { |sym| responses[sym] = send(sym) }
  responses
end
update_items() click to toggle source
# File lib/cardmarket_cli/entities/wantslist.rb, line 89
def update_items
  changed_items = @items.select(&:changed?)
  return nil if changed_items.empty?

  account.put(path, body: { action: 'editItem', want: changed_items.map!(&:to_xml_hash) })
end