futureshop 0.1.2 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f2fd23ed1fbea603b0fb85215daf907df8452b0e33af3224ff368101f3a69dd
4
- data.tar.gz: 3552f0d56f12cbfc5338b62f673e6c99b4ddda9d81216a9a1afcf9523b8a266e
3
+ metadata.gz: 9c7319b9455fdd1e3d74f600e88bb4412ae18cb9bc44dd21ae9f0b3cf973abf2
4
+ data.tar.gz: 442ee86969ef328888583a4161342d872a17414581a0e7ce2e160c7aa8437f6e
5
5
  SHA512:
6
- metadata.gz: 88c85c2e8a2be55b4f937a4b6052241b740c0c16b3e57b1597cc42afa63e503013aff67111527297c3cb22fe5760cc399d06e6bf2a9b620dccd8e016ec789b2d
7
- data.tar.gz: f0b1c12f981157dafb3ee4fd5e086675ae9de82dc0083cced20742aa881fe67285071e8248fa68fb69067b7f1cdb401d8dabf72dfc2233d973e83f253d73b046
6
+ metadata.gz: 4255adf63d28af1d55988b826d8be1c0f791ef805163fa193a869a3e41d902c4534a94fbcb73f4572e92d2d897f211208c32973ccae2ab4ef159c19850d8e70b
7
+ data.tar.gz: fac3eaece886d91a44021dc688c3e6cd196334fba574f4956dc6403bf53c04677041f96cd71f27abe2de7d39a1f79f31f361422bf59e841562168dd868ad039b
data/.gitlab-ci.yml CHANGED
@@ -4,6 +4,6 @@ before_script:
4
4
  - gem install bundler -v 2.2.21
5
5
  - bundle install
6
6
 
7
- example_job:
7
+ test:
8
8
  script:
9
9
  - bundle exec rake
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.6] - 2021-11-04
4
+
5
+ - Change required Ruby version from 3.0.0 to 2.7.0
6
+ - Complete command options
7
+
8
+ ## [0.1.5] - 2021-10-21
9
+
10
+ - Add `Futureshop::Inventory` class
11
+ - Add `inventories` subcommand
12
+
13
+ ## [0.1.4] - 2021-10-11
14
+
15
+ - Handle order's couponList field, which was introduced on 2021-09-29
16
+
17
+ ## [0.1.3] - 2021-08-25
18
+
19
+ - Fix option passing
20
+ - Fix handling of nextUrl
21
+
3
22
  ## [0.1.2] - 2021-08-06
4
23
 
5
24
  - Fix command-line option name
data/README.md CHANGED
@@ -21,6 +21,7 @@ Or install it yourself as:
21
21
  ## Usage
22
22
 
23
23
  futureshop orders -fcsv --order-date-start=2016-07-05 --order-date-end=2021-07-20 > orders.2021-07-20.csv
24
+ futureshop inventories --type=regular,preorder
24
25
 
25
26
  ## Development
26
27
 
@@ -30,4 +31,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
30
31
 
31
32
  ## Contributing
32
33
 
33
- Bug reports and merge requests are welcome on GitHub at https://gitlab.com/KitaitiMakoto/futuresohp
34
+ Bug reports and merge requests are welcome on GitHub at https://gitlab.com/KitaitiMakoto/futureshop
data/exe/futureshop CHANGED
@@ -4,7 +4,7 @@ require "optparse"
4
4
  require "optparse/time"
5
5
  require "futureshop"
6
6
 
7
- SUPPORTED_COMMANDS = %w[orders]
7
+ SUPPORTED_COMMANDS = %w[orders inventories]
8
8
 
9
9
  def main(argv)
10
10
  global_options = parse_global_options(argv)
@@ -14,6 +14,8 @@ def main(argv)
14
14
  case subcommand
15
15
  when "orders"
16
16
  orders global_options, argv
17
+ when "inventories"
18
+ inventories global_options, argv
17
19
  end
18
20
  end
19
21
 
@@ -27,6 +29,11 @@ end
27
29
 
28
30
  def parse_global_options(argv)
29
31
  parse_options(argv) {|opt|
32
+ opt.banner = "Usage:\n" + SUPPORTED_COMMANDS.collect {|command|
33
+ " #{opt.program_name} [global options] #{command} [options]"
34
+ }.join("\n")
35
+ opt.version = Futureshop::VERSION
36
+ opt.separator "Global options:"
30
37
  opt.on "--verbose" do
31
38
  $VERBOSE = true
32
39
  end
@@ -36,38 +43,38 @@ end
36
43
  def orders(global_options, argv)
37
44
  options = {format: "json"}
38
45
  options.update(parse_options(argv) {|opt|
39
- opt.on "-f", "--format=FORMAT", ["json", "csv"]
40
- opt.on "--order-date-start=DATE", Time
41
- opt.on "--order-date-end=DATE", Time
46
+ formats = %w[json csv]
47
+ opt.on "-f", "--format=FORMAT", ["json", "csv"], "Output format. Available: #{formats.join(', ')}"
48
+ opt.on "--order-date-start=YYYY-MM-DD", Time, "Retrieve orders whose date is newer than or equal to argument."
49
+ opt.on "--order-date-end=YYYY-MM-DD", Time, "Retrieve orders whose date is older than or equal to argument."
42
50
  })
43
- Futureshop.orders(order_date_start: options["order-date-start"], order_date_end: options["order-date-end"], format: options["format"])
51
+ Futureshop.orders(order_date_start: options[:"order-date-start"], order_date_end: options[:"order-date-end"], format: options[:format])
44
52
  end
45
53
 
46
- def client
47
- Futureshop::Client.new(
48
- shop_key: ENV["FUTURESHOP_SHOP_KEY"],
49
- client_id: ENV["FUTURESHOP_CLIENT_ID"],
50
- client_secret: ENV["FUTURESHOP_CLIENT_SECRET"],
51
- api_domain: ENV["FUTURESHOP_API_DOMAIN"]
52
- )
53
- end
54
+ def inventories(global_options, argv)
55
+ require "futureshop/inventory"
54
56
 
55
- def aggregate_headers(obj, headers = [])
56
- obj.each_pair do |key, value|
57
- unless %w[buyerInfo addressInfo shippingInfo shipmentList productList].include? key
58
- headers << key
59
- end
60
- case value
61
- when Hash
62
- headers.concat aggregate_headers(value).collect {|header| "#{key}.#{header}"}
63
- when Array
64
- sample = value[0]
65
- if sample
66
- headers.concat aggregate_headers(sample).collect {|header| "#{key}.#{header}"}
67
- end
57
+ options = {
58
+ type: ["regular"],
59
+ product_no: [],
60
+ jan_code: []
61
+ }
62
+ options = options.update(parse_options(argv) {|opt|
63
+ opt.on "--type=TYPE", Array, "Type. Available: regular, preorder, planned, realstore"
64
+ opt.on "--create-date-start=DATE", Time, "Retrieve products whose registration date is newer than or equal to argument."
65
+ opt.on "--create-date-end=DATE", Time, "Retrieve products whose registration date is older than or equal to argument."
66
+ opt.on "--product-no=NUMBER", Array, "Product number."
67
+ opt.on "--jan-code=CODE", Array, "JAN code."
68
+ })
69
+ if options[:type]
70
+ unknown_types = options[:type].select {|type| ! %w[regular preorder planned realstore].include?(type)}
71
+ unless unknown_types.empty?
72
+ raise "Unknown type(s): #{unknown_types.join(', ')}"
68
73
  end
69
74
  end
70
- headers
75
+ Futureshop::Inventory.each types: options[:type].collect(&:to_sym), create_date_start: options[:"create-date-start"], create_date_end: options[:"create-date-end"], product_no: options[:"product-no"], jan_code: options[:"jan-code"] do |inventory|
76
+ puts inventory.to_json
77
+ end
71
78
  end
72
79
 
73
80
  main ARGV
data/futureshop.gemspec CHANGED
@@ -8,13 +8,13 @@ Gem::Specification.new do |spec|
8
8
 
9
9
  spec.summary = "futureshop APIv2 client and tools"
10
10
  spec.description = "futureshop APIv2 client and tools"
11
- spec.homepage = "https://gitlab.com/KitaitiMakoto/futuresohp"
12
- spec.required_ruby_version = ">= 3.0.0"
11
+ spec.homepage = "https://gitlab.com/KitaitiMakoto/futureshop"
12
+ spec.required_ruby_version = ">= 2.7.0"
13
13
  spec.license = "AGPL-3.0-or-later"
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
16
16
  spec.metadata["source_code_uri"] = spec.homepage
17
- spec.metadata["changelog_uri"] = "https://gitlab.com/KitaitiMakoto/futuresohp/-/blob/main/CHANGELOG.md"
17
+ spec.metadata["changelog_uri"] = "https://gitlab.com/KitaitiMakoto/futureshop/-/blob/main/CHANGELOG.md"
18
18
 
19
19
  # Specify which files should be added to the gem when it is released.
20
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -42,10 +42,7 @@ module Futureshop
42
42
  }
43
43
  end
44
44
 
45
- def request(method, path = "/", params: {}, data: {})
46
- raise "Unsupported method: #{method}" unless [:get, :post].include?(method)
47
- query = params.empty? ? nil : build_query(params)
48
- uri = URI::HTTPS.build(host: @api_domain, port: @port, path: path, query: query)
45
+ def request_by_uri(method, uri, data: {})
49
46
  request = Net::HTTP.const_get(method.capitalize).new(uri)
50
47
  headers.each_pair do |field, value|
51
48
  request[field] = value
@@ -56,6 +53,13 @@ module Futureshop
56
53
  JSON.parse(response.body)
57
54
  end
58
55
 
56
+ def request(method, path = "/", params: {}, data: {})
57
+ raise "Unsupported method: #{method}" unless [:get, :post].include?(method)
58
+ query = params.empty? ? nil : build_query(params)
59
+ uri = URI::HTTPS.build(host: @api_domain, port: @port, path: path, query: query)
60
+ request_by_uri(method, uri, data: data)
61
+ end
62
+
59
63
  def get(path = "/", params: {})
60
64
  request(:get, path, params: params)
61
65
  end
@@ -78,12 +82,13 @@ module Futureshop
78
82
  res = get("/admin-api/v1/shipping", params: params)
79
83
  yield res["orderList"]
80
84
 
81
- nextUrl = res["nextUrl"]
82
- while nextUrl
85
+ next_url = res["nextUrl"]
86
+ while next_url
83
87
  sleep INTERVAL
84
- res = get(nextUrl, params: params) # TODO: check spec to see whether it's okay to pass params in this form
88
+ url = URI.parse(next_url)
89
+ res = request_by_uri(:get, url)
85
90
  yield res["orderList"]
86
- nextUrl = res["nextUrl"]
91
+ next_url = res["nextUrl"]
87
92
  end
88
93
  end
89
94
 
@@ -0,0 +1,51 @@
1
+ module Futureshop
2
+ class Inventory
3
+ class << self
4
+ def each_batch(types: [:regular], create_date_start: nil, create_date_end: nil, product_no: [], jan_code: [])
5
+ params = {
6
+ types: types,
7
+ product_no: product_no,
8
+ jan_code: jan_code
9
+ }
10
+ params[:create_date_start] = create_date_start.strftime("%FT%T") if create_date_start
11
+ params[:create_date_end] = create_date_end.strftime("%FT%T") if create_date_end
12
+ res = client.get("/admin-api/v1/inventory", params: params)
13
+ yield res["productList"]
14
+
15
+ next_url = res["nextUrl"]
16
+ while next_url
17
+ sleep Client::INTERVAL
18
+ url = URI.parse(next_url)
19
+ res = client.request_by_uri(:get, url)
20
+ yield res["productList"]
21
+ next_url = res["nextUrl"]
22
+ end
23
+ end
24
+
25
+ def each(**args)
26
+ return enum_for(__method__, **args) unless block_given?
27
+
28
+ each_batch **args do |inventories|
29
+ inventories.each do |inventory|
30
+ yield inventory
31
+ end
32
+ end
33
+ end
34
+
35
+ def all(**args)
36
+ return each.to_a
37
+ end
38
+
39
+ private
40
+
41
+ def client
42
+ @client ||= Futureshop::Client.new(
43
+ shop_key: ENV["FUTURESHOP_SHOP_KEY"],
44
+ client_id: ENV["FUTURESHOP_CLIENT_ID"],
45
+ client_secret: ENV["FUTURESHOP_CLIENT_SECRET"],
46
+ api_domain: ENV["FUTURESHOP_API_DOMAIN"]
47
+ )
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module Futureshop
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.6"
3
3
  end
data/lib/futureshop.rb CHANGED
@@ -25,33 +25,40 @@ module Futureshop
25
25
  raise "Multiple shipmentList. orderNo: #{order["orderNo"]}" if order["shipmentList"].length > 1
26
26
  order["shipmentList"].each do |shipment|
27
27
  shipment["productList"].each do |product|
28
- csv << order.each_value.with_object([]) {|value, values|
28
+ csv << order.each_pair.with_object([]) {|(key, value), values|
29
29
  case value
30
30
  when Hash
31
31
  value.each_value do |v|
32
32
  values << v
33
33
  end
34
34
  when Array
35
- # only shipmentList is an Array and its length is 0
36
- shipment.each_value do |v|
37
- case v
38
- when Hash
39
- v.each_value do |ov|
40
- values << ov
41
- end
42
- when Array
43
- product.each_pair do |k, v|
44
- case k
45
- when "optionPriceList"
46
- v = v.collect {|optionPrice| "#{optionPrice['name']}:#{optionPrice['selectionName']}(#{optionPrice['price']})"}.join("/")
47
- when "optionList"
48
- v = v.collect {|option| [option["name"], option["selectionItem"]].join(":")}.join(",")
35
+ case key
36
+ when "shipmentList"
37
+ # shipmentList's length is 1
38
+ shipment.each_value do |v|
39
+ case v
40
+ when Hash
41
+ v.each_value do |ov|
42
+ values << ov
43
+ end
44
+ when Array
45
+ product.each_pair do |k, v|
46
+ case k
47
+ when "optionPriceList"
48
+ v = v.collect {|optionPrice| "#{optionPrice['name']}:#{optionPrice['selectionName']}(#{optionPrice['price']})"}.join("/")
49
+ when "optionList"
50
+ v = v.collect {|option| [option["name"], option["selectionItem"]].join(":")}.join(",")
51
+ end
52
+ values << v
49
53
  end
54
+ else
50
55
  values << v
51
56
  end
52
- else
53
- values << v
54
57
  end
58
+ when "couponList"
59
+ values << value.collect {|coupon| [coupon["id"], coupon["name"]].join(":")}.join("/")
60
+ else
61
+ raise "Unknown array field: #{key}"
55
62
  end
56
63
  else
57
64
  values << value
@@ -60,6 +67,8 @@ module Futureshop
60
67
  end
61
68
  end
62
69
  end
70
+ else
71
+ raise RuntimeError("unsupported format: #{format}")
63
72
  end
64
73
  end
65
74
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: futureshop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kitaiti Makoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-06 00:00:00.000000000 Z
11
+ date: 2021-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -73,14 +73,15 @@ files:
73
73
  - futureshop.gemspec
74
74
  - lib/futureshop.rb
75
75
  - lib/futureshop/client.rb
76
+ - lib/futureshop/inventory.rb
76
77
  - lib/futureshop/version.rb
77
- homepage: https://gitlab.com/KitaitiMakoto/futuresohp
78
+ homepage: https://gitlab.com/KitaitiMakoto/futureshop
78
79
  licenses:
79
80
  - AGPL-3.0-or-later
80
81
  metadata:
81
- homepage_uri: https://gitlab.com/KitaitiMakoto/futuresohp
82
- source_code_uri: https://gitlab.com/KitaitiMakoto/futuresohp
83
- changelog_uri: https://gitlab.com/KitaitiMakoto/futuresohp/-/blob/main/CHANGELOG.md
82
+ homepage_uri: https://gitlab.com/KitaitiMakoto/futureshop
83
+ source_code_uri: https://gitlab.com/KitaitiMakoto/futureshop
84
+ changelog_uri: https://gitlab.com/KitaitiMakoto/futureshop/-/blob/main/CHANGELOG.md
84
85
  post_install_message:
85
86
  rdoc_options: []
86
87
  require_paths:
@@ -89,14 +90,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
90
  requirements:
90
91
  - - ">="
91
92
  - !ruby/object:Gem::Version
92
- version: 3.0.0
93
+ version: 2.7.0
93
94
  required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  requirements:
95
96
  - - ">="
96
97
  - !ruby/object:Gem::Version
97
98
  version: '0'
98
99
  requirements: []
99
- rubygems_version: 3.2.22
100
+ rubygems_version: 3.1.6
100
101
  signing_key:
101
102
  specification_version: 4
102
103
  summary: futureshop APIv2 client and tools