tiny_builder 0.0.5 → 0.0.7

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: a7008ba5de564b4e194e0e062a5fe8b42d2db1389467486916b56593d1bc55cf
4
- data.tar.gz: 4f618de26362c65b4b7c4613e79c207888982b7a467aeabd6ebcf9e129bd0d7f
3
+ metadata.gz: '0697caf27e45cd86fc137e9b8e44043d56f36049f984606bb5dae3b00376a586'
4
+ data.tar.gz: 7331985a4c4aecdeac1f47268c96682972d345990ace314fabc807ad83568f60
5
5
  SHA512:
6
- metadata.gz: 463f30d62d92f1d6da463eb800f507268b69b0cb6d27e44833be680f1f98f892abe78ca99e2e4be02c9fda4aa8ab854f1f4b8900cfe9d69d4fc2953d8ba4a9eb
7
- data.tar.gz: 40f1c096bb3d1fcbe0e3cb355393f845d2938d83decca41e9e002edf130622a54a4504600e6fe47f619bb100a8be07e76552388414e1dd561681f4945150c044
6
+ metadata.gz: f85ab0ac6f474a7b62e96e0f030c4bb0694561f31e66a4c12beadfad4621684e9024d27ffdc9b9c2b2c9015115b6ce72ce915cf27ea70822a5284b971c2417ad
7
+ data.tar.gz: 85e4aa9d32dc4ce06ff105f82a698f0b72b2c8c19a8fe53df03b2f557ab01fc8ebc9d041b0c393d235f74f751f06810bce07510fc54c3beb90b8b8c619dd9537
@@ -14,9 +14,12 @@ class TinyBuilder
14
14
  end
15
15
  end
16
16
 
17
+ def apigateway_url
18
+ ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
19
+ end
20
+
17
21
  def zalora_stock_url
18
- url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
19
- url + "/zalora/listing_stock"
22
+ apigateway_url + "/zalora/listing_stock"
20
23
  end
21
24
 
22
25
  def headers
@@ -31,5 +34,37 @@ class TinyBuilder
31
34
  }
32
35
  }.to_json
33
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
+
59
+ def woocommerce_product_url
60
+ apigateway_url + '/woocommerce/item'
61
+ end
62
+
63
+ def woocommerce_item_payload(listing)
64
+ {
65
+ "credential": credential,
66
+ "data": { "local_item_id": listing.local_item_id, local_id: listing[:local_id] }
67
+ }.to_json
68
+ end
34
69
  end
35
70
  end
@@ -34,6 +34,9 @@ class TinyBuilder
34
34
  if credential['channel_id'] == 13
35
35
  TinyBuilder::ZaloraQuantity.new(credential, listings).perform
36
36
  elsif credential['channel_id'] == 2
37
+ TinyBuilder::ShopifyQuantity.new(credential, listings).perform
38
+ elsif credential['channel_id'] == 19
39
+ TinyBuilder::WoocommerceQuantity.new(credential, listings).perform
37
40
  end
38
41
  end
39
42
  end
@@ -5,7 +5,22 @@ 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
+ elsif channel_id == 19
18
+ {
19
+ meta_location_id: listing[:meta_location_id]
20
+ }
21
+ else
22
+ {}
23
+ end
9
24
  end
10
25
  end
11
26
  end
@@ -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.5'
4
+ VERSION = '0.0.7'
5
5
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TinyBuilder
4
+ class WoocommerceQuantity
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_location_id(listing)
18
+ listing.save
19
+ end
20
+ end
21
+
22
+ def assign_location_id(listing)
23
+ return unless listing[:meta_location_id].blank?
24
+
25
+ begin
26
+ listing[:meta_location_id] = meta_location_id(listing)
27
+ rescue
28
+ listing[:wocommerce_last_log] = "Error when getting woocommerce location_id"
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def meta_location_id(listing)
35
+ args = {
36
+ method: :post,
37
+ url: woocommerce_product_url,
38
+ payload: woocommerce_item_payload(listing),
39
+ headers: headers
40
+ }
41
+
42
+ resp = JSON.parse(rest_client(args, [200, 402]))
43
+ data = Array.wrap(resp['meta_data']).find { |meta_data| meta_data['key'] == 'smifw_location_stocks' }
44
+ data ? data['id'] : nil
45
+ end
46
+ end
47
+ end
data/lib/tiny_builder.rb CHANGED
@@ -11,6 +11,8 @@ require 'tiny_builder/price_builder'
11
11
  require 'tiny_builder/quantity_builder'
12
12
  require 'tiny_builder/apigateway_helper'
13
13
  require 'tiny_builder/zalora_quantity'
14
+ require 'tiny_builder/shopify_quantity'
15
+ require 'tiny_builder/woocommerce_quantity'
14
16
 
15
17
  class TinyBuilder
16
18
  include ModeExecutor
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.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - hayakaza
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-06 00:00:00.000000000 Z
11
+ date: 2023-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,7 +143,9 @@ files:
143
143
  - lib/tiny_builder/quantity_builder.rb
144
144
  - lib/tiny_builder/quantity_counter.rb
145
145
  - lib/tiny_builder/quantity_helper.rb
146
+ - lib/tiny_builder/shopify_quantity.rb
146
147
  - lib/tiny_builder/version.rb
148
+ - lib/tiny_builder/woocommerce_quantity.rb
147
149
  - lib/tiny_builder/zalora_quantity.rb
148
150
  homepage:
149
151
  licenses: []