tiny_builder 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2bea932227effab5ceaed99f16011cf8937638e7b670a79bedc0e2fff23c0b8
4
- data.tar.gz: 0a8681f8107bea7d2c109aa9686e3e90d214deff47b8dc29b8d14cd83a89a17c
3
+ metadata.gz: a7008ba5de564b4e194e0e062a5fe8b42d2db1389467486916b56593d1bc55cf
4
+ data.tar.gz: 4f618de26362c65b4b7c4613e79c207888982b7a467aeabd6ebcf9e129bd0d7f
5
5
  SHA512:
6
- metadata.gz: 9bd58428ab8ad86b404d10da13b043052c1bc1a6cd1f228399b23b63d6c0659b75f52378fc67cc822e99c8deb00186b2495374088c34ff879efa1c3798412239
7
- data.tar.gz: a1be06a2566eed07c505eff652a4a9f960e1ebda7f34d1ed94e7e416911d9b4a6f173cce3b983ccca80644da067594432d48e0057c9a192b14bc6ac3fcf292ab
6
+ metadata.gz: 463f30d62d92f1d6da463eb800f507268b69b0cb6d27e44833be680f1f98f892abe78ca99e2e4be02c9fda4aa8ab854f1f4b8900cfe9d69d4fc2953d8ba4a9eb
7
+ data.tar.gz: 40f1c096bb3d1fcbe0e3cb355393f845d2938d83decca41e9e002edf130622a54a4504600e6fe47f619bb100a8be07e76552388414e1dd561681f4945150c044
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TinyBuilder
4
+ module ApigatewayHelper
5
+ def rest_client(params, rescued_codes = 200)
6
+ RestClient::Request.execute(params.merge(timeout: 3)) do |response|
7
+ code = response.code
8
+ resp = response.body.to_str
9
+ unless Array.wrap(rescued_codes).include?(code)
10
+ raise "Response Code is #{code}" unless resp.include?('Response code = 404')
11
+ end
12
+
13
+ response
14
+ end
15
+ end
16
+
17
+ def zalora_stock_url
18
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
19
+ url + "/zalora/listing_stock"
20
+ end
21
+
22
+ def headers
23
+ { content_type: :json, accept: :json }
24
+ end
25
+
26
+ def zalora_stock_payload(listing)
27
+ {
28
+ "credential": credential,
29
+ "data": {
30
+ "productId": listing[:local_id]
31
+ }
32
+ }.to_json
33
+ end
34
+ end
35
+ end
@@ -11,10 +11,12 @@ class TinyBuilder
11
11
 
12
12
  attr_reader :listings
13
13
  attr_reader :mwh
14
+ attr_reader :channel_id
14
15
 
15
16
  def initialize(args)
16
17
  @listings = args[:listings]
17
18
  @mwh = args[:mwh]
19
+ @channel_id = args[:channel_id]
18
20
  end
19
21
 
20
22
  def perform
@@ -5,7 +5,8 @@ class TinyBuilder
5
5
  def perform_builder
6
6
  builder_classes[mode].new(
7
7
  listings: listings,
8
- mwh: mwh
8
+ mwh: mwh,
9
+ channel_id: credential['channel_id']
9
10
  ).perform
10
11
  end
11
12
 
@@ -28,5 +29,12 @@ class TinyBuilder
28
29
  end
29
30
  data
30
31
  end
32
+
33
+ def prepare_data
34
+ if credential['channel_id'] == 13
35
+ TinyBuilder::ZaloraQuantity.new(credential, listings).perform
36
+ elsif credential['channel_id'] == 2
37
+ end
38
+ end
31
39
  end
32
40
  end
@@ -74,6 +74,7 @@ class TinyBuilder
74
74
 
75
75
  def search_bundle_variants
76
76
  bundle = MasterProduct.where("bundle_variants.master_variant_id": variant.id).last
77
+ return [] unless bundle
77
78
  bundle.bundle_variants.select { |bv| !bv[:main] }
78
79
  end
79
80
  end
@@ -21,9 +21,11 @@ class TinyBuilder
21
21
  end
22
22
 
23
23
  def quantity(listing, wh_space = nil)
24
- QuantityCounter.new(
24
+ qty = QuantityCounter.new(
25
25
  counter_params(listing, wh_space)
26
26
  ).perform
27
+ qty += listing[:zalora_reserved_stock].to_i if channel_id == 13
28
+ qty
27
29
  end
28
30
 
29
31
  def counter_params(listing, wh_space = nil)
@@ -49,6 +51,7 @@ class TinyBuilder
49
51
 
50
52
  def bundle_warehouse_spaces(variant)
51
53
  bundle = MasterProduct.find_by("bundle_variants.master_variant_id": variant.id)
54
+ return [] unless bundle
52
55
  variant_id = bundle.bundle_variants.find { |bv| !bv[:main] }[:master_variant_id]
53
56
  WarehouseSpace.where(
54
57
  item_variant_id: variant_id
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TinyBuilder
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TinyBuilder
4
+ class ZaloraQuantity
5
+ include ApigatewayHelper
6
+
7
+ attr_reader :credential
8
+ attr_reader :listings
9
+
10
+ def initialize(credential, listings)
11
+ @credential = credential
12
+ @listings = listings
13
+ end
14
+
15
+ def perform
16
+ listings.each do |listing|
17
+ update_reserved_stock(listing)
18
+ end
19
+ end
20
+
21
+ def update_reserved_stock(listing)
22
+ begin
23
+ listing[:zalora_reserved_stock] = reserved_stock(listing)
24
+ rescue
25
+ listing[:zalora_last_log] = "Error when getting reseverd stock"
26
+ listing[:zalora_reserved_stock] = 0
27
+ end
28
+
29
+ listing[:last_time_get_reserved_stock] = Time.now
30
+ listing.save
31
+ end
32
+
33
+ private
34
+
35
+ def reserved_stock(listing)
36
+ args = {
37
+ method: :post,
38
+ url: zalora_stock_url,
39
+ payload: zalora_stock_payload(listing),
40
+ headers: headers
41
+ }
42
+
43
+ resp = JSON.parse(rest_client(args, [200, 402]))
44
+ resp["reservedStock"]
45
+ end
46
+ end
47
+ end
data/lib/tiny_builder.rb CHANGED
@@ -9,23 +9,27 @@ require 'tiny_builder/mode_executor'
9
9
  require 'tiny_builder/active_builder'
10
10
  require 'tiny_builder/price_builder'
11
11
  require 'tiny_builder/quantity_builder'
12
+ require 'tiny_builder/apigateway_helper'
13
+ require 'tiny_builder/zalora_quantity'
12
14
 
13
15
  class TinyBuilder
14
16
  include ModeExecutor
15
17
 
16
- attr_reader :listing_ids, :mode, :mwh
18
+ attr_reader :listing_ids, :mode, :mwh, :credential
17
19
 
18
- def self.build(listing_ids, mode, mwh)
19
- new(listing_ids, mode, mwh).perform
20
+ def self.build(listing_ids, mode, credential = {})
21
+ new(listing_ids, mode, credential).perform
20
22
  end
21
23
 
22
- def initialize(listing_ids, mode, mwh)
24
+ def initialize(listing_ids, mode, credential)
23
25
  @listing_ids = listing_ids
24
26
  @mode = mode
25
- @mwh = mwh
27
+ @mwh = credential['multiwarehouse']
28
+ @credential = credential
26
29
  end
27
30
 
28
31
  def perform
32
+ prepare_data
29
33
  perform_builder
30
34
  end
31
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - hayakaza
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-24 00:00:00.000000000 Z
11
+ date: 2023-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -135,6 +135,7 @@ files:
135
135
  - lib/tiny_builder.rb
136
136
  - lib/tiny_builder/active_builder.rb
137
137
  - lib/tiny_builder/allocated_stock.rb
138
+ - lib/tiny_builder/apigateway_helper.rb
138
139
  - lib/tiny_builder/base_builder.rb
139
140
  - lib/tiny_builder/data_loader.rb
140
141
  - lib/tiny_builder/mode_executor.rb
@@ -143,6 +144,7 @@ files:
143
144
  - lib/tiny_builder/quantity_counter.rb
144
145
  - lib/tiny_builder/quantity_helper.rb
145
146
  - lib/tiny_builder/version.rb
147
+ - lib/tiny_builder/zalora_quantity.rb
146
148
  homepage:
147
149
  licenses: []
148
150
  metadata: {}