Class: CardmarketCLI::Entities::Wantslist

Inherits:
Changeable show all
Extended by:
Deletable, Unique
Defined in:
lib/cardmarket_cli/entities/wantslist.rb

Overview

Constant Summary collapse

PARAMS =
i[name].freeze
PATH_BASE =
'wantslist'

Instance Attribute Summary

Attributes inherited from Entity

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deletable

list_attr

Methods included from Unique

uniq_attr

Methods inherited from Changeable

#changed?

Constructor Details

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

Returns a new instance of Wantslist.



21
22
23
24
# File 'lib/cardmarket_cli/entities/wantslist.rb', line 21

def initialize(id, , params = {})
  super(id, , params.slice(*PARAMS))
  Wantslist.send(:add, self)
end

Class Method Details

.create(*args) ⇒ Object



124
125
126
# File 'lib/cardmarket_cli/entities/wantslist.rb', line 124

def create(*args)
  new(*args)
end

.from_hash(account, hash) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/cardmarket_cli/entities/wantslist.rb', line 128

def from_hash(, hash)
  return nil unless hash['game']&.fetch('idGame') == 1

  list = Wantslist.new(hash['idWantsList'], , name: hash['name'])
  hash['item']&.each { |item| list.add_item(WantslistItem.from_hash(, item)) }
  list
end

.read(account, eager: true) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/cardmarket_cli/entities/wantslist.rb', line 108

def read(, eager: true)
  LOGGER.debug('Reading wantslists')
  response = .get(PATH_BASE)
  hash = JSON.parse(response.response_body)
  hash['wantslist']&.each do |item|
    list = from_hash(, item)
    list.read if eager && list
  end
  instances
end

.updateObject



119
120
121
122
# File 'lib/cardmarket_cli/entities/wantslist.rb', line 119

def update
  instances.each(&:update)
  deleted_instances.each(&:delete)
end

Instance Method Details

#deleteObject



40
41
42
43
44
45
# File 'lib/cardmarket_cli/entities/wantslist.rb', line 40

def delete
  LOGGER.debug("Deleting wantslist #{name}(#{id})")
  return unless id

  .delete(path, body: { action: 'deleteWantslist' })
end

#pathObject



26
27
28
# File 'lib/cardmarket_cli/entities/wantslist.rb', line 26

def path
  "#{PATH_BASE}/#{id}"
end

#readObject



30
31
32
33
34
35
36
37
38
# File 'lib/cardmarket_cli/entities/wantslist.rb', line 30

def read
  LOGGER.debug("Reading wantslist #{name}(#{id})")
  return unless id

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

  response
end

#updateObject



47
48
49
50
51
52
53
54
# 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