tiny_builder 0.0.4 → 0.0.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: d2bea932227effab5ceaed99f16011cf8937638e7b670a79bedc0e2fff23c0b8
4
- data.tar.gz: 0a8681f8107bea7d2c109aa9686e3e90d214deff47b8dc29b8d14cd83a89a17c
3
+ metadata.gz: 0e74aed48d7a6bde96ec9e6dbaf479044bd2d551b59f47992e72e4169ce58da3
4
+ data.tar.gz: 94ec01f9675a3d3b54dceb4ec3ebc749c1b2485a425156b6c5017b5eefb0a798
5
5
  SHA512:
6
- metadata.gz: 9bd58428ab8ad86b404d10da13b043052c1bc1a6cd1f228399b23b63d6c0659b75f52378fc67cc822e99c8deb00186b2495374088c34ff879efa1c3798412239
7
- data.tar.gz: a1be06a2566eed07c505eff652a4a9f960e1ebda7f34d1ed94e7e416911d9b4a6f173cce3b983ccca80644da067594432d48e0057c9a192b14bc6ac3fcf292ab
6
+ metadata.gz: 6ca8521dac8c51243a9372cb63fa67b934a675af3127dcc01aa79494c9e2ac3ff9507cde3207bf579b5d006769fee0b44c2f2a5827a89aa696f749223dcd073d
7
+ data.tar.gz: c04d6de7140861ed1ab84e752d03595d0eafa7b0f184f6c269b5fe326591b3f6f8036c30ab5cd8b35e0d77726e647d1406852146295ee83e624ece9b3d63c4bd
@@ -0,0 +1,59 @@
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 apigateway_url
18
+ ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
19
+ end
20
+
21
+ def zalora_stock_url
22
+ apigateway_url + "/zalora/listing_stock"
23
+ end
24
+
25
+ def headers
26
+ { content_type: :json, accept: :json }
27
+ end
28
+
29
+ def zalora_stock_payload(listing)
30
+ {
31
+ "credential": credential,
32
+ "data": {
33
+ "productId": listing[:local_id]
34
+ }
35
+ }.to_json
36
+ end
37
+
38
+ def inventory_item_payload(listing)
39
+ {
40
+ "credential": credential,
41
+ "data": { "local_id": listing[:local_id] }
42
+ }.to_json
43
+ end
44
+
45
+ def location_payload
46
+ {
47
+ "credential": credential
48
+ }.to_json
49
+ end
50
+
51
+ def shopify_inventory_item_url
52
+ apigateway_url + '/shopify/data_variant'
53
+ end
54
+
55
+ def shopify_location_url
56
+ apigateway_url + '/shopify/data_location'
57
+ end
58
+ end
59
+ 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,13 @@ 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
+ TinyBuilder::ShopifyQuantity.new(credential, listings).perform
38
+ end
39
+ end
31
40
  end
32
41
  end
@@ -5,7 +5,18 @@ require 'tiny_builder/base_builder'
5
5
  class TinyBuilder
6
6
  class QuantityBuilder < BaseBuilder
7
7
  def to_h(listing)
8
- quantity_hash(listing)
8
+ quantity_hash(listing).merge(channel_data(listing))
9
+ end
10
+
11
+ def channel_data(listing)
12
+ if channel_id = 2
13
+ {
14
+ inventory_item_id: listing[:inventory_item_id],
15
+ location_id: listing[:location_id]
16
+ }
17
+ else
18
+ {}
19
+ end
9
20
  end
10
21
  end
11
22
  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
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TinyBuilder
4
+ class ShopifyQuantity
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
+ assign_inventory_item_id(listing)
18
+ assign_location_id(listing)
19
+ listing.save
20
+ end
21
+ end
22
+
23
+ def assign_inventory_item_id(listing)
24
+ return unless listing[:inventory_item_id].blank?
25
+
26
+ begin
27
+ listing[:inventory_item_id] = inventory_item_id(listing)
28
+ rescue
29
+ listing[:shopify_last_log] = "Error when getting inventory_item_id"
30
+ end
31
+ end
32
+
33
+ def assign_location_id(listing)
34
+ return unless listing[:location_id].blank?
35
+
36
+ begin
37
+ listing[:location_id] = credential['primary_warehouse_id'] || location_id
38
+ rescue
39
+ listing[:shopify_last_log] = "Error when getting location_id"
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def inventory_item_id(listing)
46
+ args = {
47
+ method: :post,
48
+ url: shopify_inventory_item_url,
49
+ payload: inventory_item_payload(listing),
50
+ headers: headers
51
+ }
52
+
53
+ resp = JSON.parse(rest_client(args, [200, 402]))
54
+ resp['inventory_item_id']
55
+ end
56
+
57
+ def location_id
58
+ @location_id ||= get_location_id
59
+ end
60
+
61
+ def get_location_id
62
+ args = {
63
+ method: :post,
64
+ url: shopify_location_url,
65
+ payload: location_payload,
66
+ headers: headers
67
+ }
68
+
69
+ resp = JSON.parse(rest_client(args, [200, 402]))
70
+ resp['id']
71
+ end
72
+ end
73
+ end
@@ -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.6'
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,28 @@ 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'
14
+ require 'tiny_builder/shopify_quantity'
12
15
 
13
16
  class TinyBuilder
14
17
  include ModeExecutor
15
18
 
16
- attr_reader :listing_ids, :mode, :mwh
19
+ attr_reader :listing_ids, :mode, :mwh, :credential
17
20
 
18
- def self.build(listing_ids, mode, mwh)
19
- new(listing_ids, mode, mwh).perform
21
+ def self.build(listing_ids, mode, credential = {})
22
+ new(listing_ids, mode, credential).perform
20
23
  end
21
24
 
22
- def initialize(listing_ids, mode, mwh)
25
+ def initialize(listing_ids, mode, credential)
23
26
  @listing_ids = listing_ids
24
27
  @mode = mode
25
- @mwh = mwh
28
+ @mwh = credential['multiwarehouse']
29
+ @credential = credential
26
30
  end
27
31
 
28
32
  def perform
33
+ prepare_data
29
34
  perform_builder
30
35
  end
31
36
  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.6
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-08 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
@@ -142,7 +143,9 @@ files:
142
143
  - lib/tiny_builder/quantity_builder.rb
143
144
  - lib/tiny_builder/quantity_counter.rb
144
145
  - lib/tiny_builder/quantity_helper.rb
146
+ - lib/tiny_builder/shopify_quantity.rb
145
147
  - lib/tiny_builder/version.rb
148
+ - lib/tiny_builder/zalora_quantity.rb
146
149
  homepage:
147
150
  licenses: []
148
151
  metadata: {}