cardmarket_cli 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardmarketCLI
4
+ module Entities
5
+ ##
6
+ # Makes a class unique
7
+ module Unique
8
+ def uniq_attr(symbol, options = {})
9
+ options = { hash: true, plural: "#{symbol}s", index: :id }.merge! options
10
+ def_brackets(options[:plural], options[:hash])
11
+ def_add(options[:plural], options[:hash], options[:index])
12
+ def_reader(options[:plural], options[:hash])
13
+ end
14
+
15
+ private
16
+
17
+ def def_reader(plural, hash)
18
+ return if respond_to? plural.to_s.to_sym
19
+
20
+ define_method plural.to_s do
21
+ instance_variable_set("@#{plural}", hash ? {} : []) unless instance_variable_defined? "@#{plural}"
22
+ instance_variable_get("@#{plural}").dup
23
+ end
24
+ end
25
+
26
+ def def_brackets(plural, hash)
27
+ return if respond_to? :[]
28
+
29
+ define_method :[] do |id|
30
+ instance_variable_set("@#{plural}", hash ? {} : []) unless instance_variable_defined? "@#{plural}"
31
+ instance_variable_get("@#{plural}")[id]
32
+ end
33
+ end
34
+
35
+ def def_add(plural, hash, index)
36
+ define_method :add do |item|
37
+ instance_variable_set("@#{plural}", hash ? {} : []) unless instance_variable_defined? "@#{plural}"
38
+ if hash
39
+ instance_variable_get("@#{plural}")[item.send(index)] = item
40
+ else
41
+ instance_variable_get("@#{plural}") << item
42
+ end
43
+ end
44
+ private :add
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cardmarket_cli/entities/deletable'
4
+ require 'cardmarket_cli/entities/changeable'
5
+ require 'cardmarket_cli/entities/wantslist_item'
6
+ require 'cardmarket_cli/entities/unique'
7
+ require 'cardmarket_cli/logger'
8
+
9
+ module CardmarketCLI
10
+ module Entities
11
+ ##
12
+ # see https://api.cardmarket.com/ws/documentation/API_2.0:Entities:Wantslist
13
+ class Wantslist < Changeable
14
+ extend Deletable
15
+
16
+ PARAMS = %i[name].freeze
17
+ PATH_BASE = 'wantslist'
18
+ attr_(*PARAMS)
19
+ list_attr :item
20
+
21
+ def initialize(id, account, params = {})
22
+ super(id, account, params.slice(*PARAMS))
23
+ Wantslist.send(:add, self)
24
+ end
25
+
26
+ def path
27
+ "#{PATH_BASE}/#{id}"
28
+ end
29
+
30
+ def read
31
+ LOGGER.debug("Reading wantslist #{name}(#{id})")
32
+ return unless id
33
+
34
+ response = account.get(path)
35
+ override_params(JSON.parse(response.response_body)['wantslist'])
36
+
37
+ response
38
+ end
39
+
40
+ def delete
41
+ LOGGER.debug("Deleting wantslist #{name}(#{id})")
42
+ return unless id
43
+
44
+ account.delete(path, body: { action: 'deleteWantslist' })
45
+ end
46
+
47
+ def update
48
+ LOGGER.debug("Updating wantslist #{name}(#{id})")
49
+ responses = {}
50
+ responses[:create] = create unless id
51
+ responses[:update] = patch if changed?
52
+ patch_items(responses)
53
+ responses.compact!
54
+ end
55
+
56
+ private
57
+
58
+ def override_params(hash)
59
+ params[:name] = hash['name']
60
+ clear
61
+ self.changed = false
62
+ hash['item']&.each { |item| add_item(WantslistItem.from_hash(account, item)) }
63
+ end
64
+
65
+ def patch_items(responses)
66
+ %i[create_items update_items delete_items].each { |sym| responses[sym] = send(sym) }
67
+ responses
68
+ end
69
+
70
+ def create
71
+ response = account.post(PATH_BASE, body: { wantslist: { name: name, idGame: 1 } })
72
+ self.id = JSON.parse(response)['wantslist']&.fetch(0)&.fetch('idWantsList')
73
+ response
74
+ end
75
+
76
+ def patch
77
+ account.put(path, body: { action: 'editWantslist', name: name })
78
+ end
79
+
80
+ def create_items
81
+ new_items = @items.reject(&:id)
82
+ return nil if new_items.empty?
83
+
84
+ account.put(path, body: { action: 'addItem',
85
+ product: new_items.reject(&:meta?).map!(&:to_xml_hash),
86
+ metaproduct: new_items.select(&:meta?).map!(&:to_xml_hash) })
87
+ end
88
+
89
+ def update_items
90
+ changed_items = @items.select(&:changed?)
91
+ return nil if changed_items.empty?
92
+
93
+ account.put(path, body: { action: 'editItem', want: changed_items.map!(&:to_xml_hash) })
94
+ end
95
+
96
+ def delete_items
97
+ return nil if deleted_items.empty?
98
+
99
+ account.put(path, body: { action: 'deleteItem', want: @deleted_items.map { |item| { idWant: item.id } } })
100
+ end
101
+
102
+ class << self
103
+ extend Deletable
104
+ extend Unique
105
+ list_attr :instance, default: false, suffix: false
106
+ uniq_attr :instance, hash: false
107
+
108
+ def read(account, eager: true)
109
+ LOGGER.debug('Reading wantslists')
110
+ response = account.get(PATH_BASE)
111
+ hash = JSON.parse(response.response_body)
112
+ hash['wantslist']&.each do |item|
113
+ list = from_hash(account, item)
114
+ list.read if eager && list
115
+ end
116
+ instances
117
+ end
118
+
119
+ def update
120
+ instances.each(&:update)
121
+ deleted_instances.each(&:delete)
122
+ end
123
+
124
+ def create(*args)
125
+ new(*args)
126
+ end
127
+
128
+ def from_hash(account, hash)
129
+ return nil unless hash['game']&.fetch('idGame') == 1
130
+
131
+ list = Wantslist.new(hash['idWantsList'], account, name: hash['name'])
132
+ hash['item']&.each { |item| list.add_item(WantslistItem.from_hash(account, item)) }
133
+ list
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cardmarket_cli/entities/changeable'
4
+ require 'cardmarket_cli/entities/product'
5
+ require 'cardmarket_cli/entities/meta_product'
6
+ require_relative '../util/string'
7
+
8
+ module CardmarketCLI
9
+ module Entities
10
+ ##
11
+ # see https://api.cardmarket.com/ws/documentation/API_2.0:Wantslist_Item
12
+ class WantslistItem < Changeable
13
+ PARAMS = %i[count from_price min_condition wish_price mail_alert languages is_foil is_altered is_playset is_signed
14
+ is_first_ed].freeze
15
+ attr_(*(PARAMS - [:languages]))
16
+ attr_reader :product
17
+
18
+ def initialize(id, product, account, params = {})
19
+ params = { count: 1, min_condition: :PO, wish_price: 0.0, mail_alert: false, languages: [1], is_foil: nil,
20
+ is_altered: nil, is_playset: nil, is_signed: nil, is_first_ed: nil, from_price: nil }.merge!(params)
21
+ super(id, account, params.slice(*PARAMS))
22
+ @product = product
23
+ end
24
+
25
+ def to_xml_hash
26
+ hash = params.compact.transform_keys! { |key| key.to_s.camelize }
27
+ hash['idLanguage'] = hash.delete('languages')&.uniq!
28
+ hash['idWant'] = id
29
+ hash.delete_if { |_, value| value.nil? || (value.respond_to?(:empty?) && value.empty?) }
30
+
31
+ add_product_id(hash)
32
+ hash
33
+ end
34
+
35
+ def add_product_id(hash)
36
+ return if hash['idWant']
37
+
38
+ if @product.meta?
39
+ hash[:idMetaproduct] = @product.id
40
+ else
41
+ hash[:idProduct] = @product.id
42
+ end
43
+ end
44
+
45
+ def meta?
46
+ product.meta?
47
+ end
48
+
49
+ def languages
50
+ params[:languages]&.uniq!
51
+ end
52
+
53
+ def languages=(value)
54
+ params[:languages] = value.uniq
55
+ end
56
+
57
+ class << self
58
+ def from_hash(account, hash)
59
+ product = create_product(account, hash)
60
+ hash.transform_keys! { |key| key.underscore.to_sym }
61
+ hash[:min_condition] &&= hash[:min_condition].to_sym
62
+ hash[:languages] = hash[:id_language]
63
+ WantslistItem.new(hash[:id_want], product, account, hash)
64
+ end
65
+
66
+ private
67
+
68
+ def create_product(account, hash)
69
+ if hash['type'] == 'metaproduct'
70
+ MetaProduct.from_hash(account, hash['metaproduct'])
71
+ else
72
+ Product.from_hash(account, hash.slice(*%w[rarity expansionName countArticles countFoils])
73
+ .merge!(hash['product']))
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module CardmarketCLI
6
+ LOGGER = Logger.new($stdout)
7
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'yaml'
5
+ require 'cardmarket_cli/account'
6
+ require 'cardmarket_cli/logger'
7
+ require 'cardmarket_cli/entities/wantslist'
8
+ require 'cardmarket_cli/entities/product'
9
+
10
+ config = JSON.parse(File.read('config.json'))
11
+ config['TEST'] = false if config['TEST'].nil?
12
+ CardmarketCLI::LOGGER.level = config['LOGGING_LEVEL'] || :info
13
+ CardmarketCLI::LOGGER.level = :info
14
+ account = CardmarketCLI::Account.new(config['APP_TOKEN'], config['APP_SECRET'], config['ACCESS_TOKEN'],
15
+ config['ACCESS_TOKEN_SECRET'], test: config['TEST'])
16
+ # puts list.to_yaml
17
+ puts account.get("#{CardmarketCLI::Entities::Product::PATH_BASE}/265535").to_yaml
18
+ #
19
+ # item = list.items[0]
20
+ # item.count = 4
21
+ # item.min_condition = :NM
22
+ # item.is_playset = true
23
+ # item.mail_alert = true
24
+ # item.from_price = 5
25
+ # item.languages << 1
26
+ # puts list.update.to_yaml
27
+ # puts MetaProduct.search(account, 'tarmogoyf').to_yaml
28
+ # product.read
29
+ # puts product.to_yaml
30
+ # puts list.update.to_yaml
31
+ # puts JSON.parse(request.response_body).to_yaml
32
+ # puts account.get(Wantslist::PATH_BASE).to_yaml
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # adds camelize and underscore to String
5
+ # Taken from ruby on rails
6
+ # See https://apidock.com/rails/String/underscore
7
+ # See https://apidock.com/rails/String/camelize
8
+ class String
9
+ def camelize(uppercase_first: false)
10
+ string = self
11
+ string = if uppercase_first
12
+ string.sub(/^[a-z\d]*/, &:capitalize)
13
+ else
14
+ string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase)
15
+ end
16
+ string.gsub(%r{(?:_|(/))([a-z\d]*)}) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }.gsub('/', '::')
17
+ end
18
+
19
+ def underscore
20
+ return dup unless /[A-Z-]|::/.match?(self)
21
+
22
+ word = gsub('::', '/').dup
23
+ word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)((?=a)b)(?=\b|[^a-z])/) do
24
+ "#{Regexp.last_match(1) && '_'}#{Regexp.last_match(2).downcase}"
25
+ end
26
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
27
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
28
+ word.tr!('-', '_')
29
+ word.downcase!
30
+ word
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardmarketCLI
4
+ VERSION = '0.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,225 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cardmarket_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - lisstem
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: typhoeus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: xml-simple
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.16.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.16.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.4.6
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.4.6
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 5.13.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 5.13.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-reporters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.4.3
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.4.3
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '13.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '13.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.7'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.7'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-minitest
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.10.3
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.10.3
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rake
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 0.5.1
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 0.5.1
167
+ description:
168
+ email:
169
+ - kontakt@knuddelkrabbe.de
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rubocop.yml"
176
+ - CODE_OF_CONDUCT.md
177
+ - Gemfile
178
+ - Gemfile.lock
179
+ - Guardfile
180
+ - LICENSE.md
181
+ - README.md
182
+ - Rakefile
183
+ - bin/console
184
+ - bin/setup
185
+ - cardmarket_cli.gemspec
186
+ - lib/cardmarket_cli.rb
187
+ - lib/cardmarket_cli/account.rb
188
+ - lib/cardmarket_cli/entities/changeable.rb
189
+ - lib/cardmarket_cli/entities/deletable.rb
190
+ - lib/cardmarket_cli/entities/entity.rb
191
+ - lib/cardmarket_cli/entities/meta_product.rb
192
+ - lib/cardmarket_cli/entities/product.rb
193
+ - lib/cardmarket_cli/entities/unique.rb
194
+ - lib/cardmarket_cli/entities/wantslist.rb
195
+ - lib/cardmarket_cli/entities/wantslist_item.rb
196
+ - lib/cardmarket_cli/logger.rb
197
+ - lib/cardmarket_cli/test.rb
198
+ - lib/cardmarket_cli/util/string.rb
199
+ - lib/cardmarket_cli/version.rb
200
+ homepage: https://github.com/Lisstem/cardmarket-cli
201
+ licenses:
202
+ - Nonstandard
203
+ metadata:
204
+ homepage_uri: https://github.com/Lisstem/cardmarket-cli
205
+ source_code_uri: https://github.com/Lisstem/cardmarket-cli
206
+ post_install_message:
207
+ rdoc_options: []
208
+ require_paths:
209
+ - lib
210
+ required_ruby_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: 2.7.1
215
+ required_rubygems_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ requirements: []
221
+ rubygems_version: 3.1.2
222
+ signing_key:
223
+ specification_version: 4
224
+ summary: CLI for cardmarket.com
225
+ test_files: []