huginn_bigcommerce_product_agent 1.4.2 → 1.8.0

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: 4212c209c32fafb12fc7746a038bcb4c20af8cde053cb2adc44cff87e8112433
4
- data.tar.gz: 4dd2faafdf9ab8422eacd7c407ccf35549a69ace3df0a9a748c09bc91a038ffc
3
+ metadata.gz: 401d12c4fd8a1bd918e0e0b4d6a20eca3467e7ce288130025c500e7f3a7e7d9c
4
+ data.tar.gz: 501ebd2b48cc23f68671c407ddefdf69d12b666eb608f37d4f149e81eeaecf6d
5
5
  SHA512:
6
- metadata.gz: c871f927452151ecd880b7b68731d8629100262911c9fc7d4566cf7f31a29c626bedba74f5dcb83dbd135413a95a781042d1e6408b060c00e0bf4106c408d2af
7
- data.tar.gz: a275f1a162b1a7ed5633b5e0868766eb9e41b9b1ce0e8a024c5bc241183bf286ddc582f53df9b580e2ea3cb52b7ad6de51bdc1274b13857ecb153a206e924ce3
6
+ metadata.gz: fb699b59217b6c4ee28b5691ffa1f08bd8298e7aa96bdaed61060097e506bb170d189a386b61871bfb761c5eb6c103e9451aa13ced6330843808774e9fd00997
7
+ data.tar.gz: a368c30a438e9c20659a7b819e4cdf691f2f790436ecd588bb6742b65fefa6453ee42a3f8eb4aab0aaf924d6bfd7f002053b60cd946f27b7da13fc11f1470748
@@ -4,8 +4,12 @@ module BigcommerceProductAgent
4
4
  @uri_base = 'catalog/products/:product_id'
5
5
 
6
6
  def update(id, payload, params={})
7
- response = client.put(uri(product_id: id), payload.to_json) do |request|
8
- request.params.update(params) if params
7
+ begin
8
+ response = client.put(uri(product_id: id), payload.to_json) do |request|
9
+ request.params.update(params) if params
10
+ end
11
+ rescue Faraday::Error::ClientError => e
12
+ raise e, "\n#{e.message}\nFailed to update product with payload = #{payload.to_json}\n", e.backtrace
9
13
  end
10
14
 
11
15
  return response.body['data']
@@ -17,23 +21,22 @@ module BigcommerceProductAgent
17
21
  end
18
22
 
19
23
  def create(payload, params={})
20
- response = client.post(uri, payload.to_json) do |request|
21
- request.params.update(params) if params
24
+ begin
25
+ response = client.post(uri, payload.to_json) do |request|
26
+ request.params.update(params) if params
27
+ end
28
+ rescue Faraday::Error::ClientError => e
29
+ raise e, "\n#{e.message}\nFailed to create product with payload = #{payload.to_json}\n", e.backtrace
22
30
  end
23
31
 
24
32
  return response.body['data']
25
33
  end
26
34
 
27
35
  def upsert(payload, params={})
28
- begin
29
- if payload[:id]
30
- return update(payload[:id], payload, params)
31
- else
32
- return create(payload, params)
33
- end
34
- rescue Faraday::Error::ClientError => e
35
- puts e.inspect
36
- raise e
36
+ if payload[:id]
37
+ return update(payload[:id], payload, params)
38
+ else
39
+ return create(payload, params)
37
40
  end
38
41
  end
39
42
 
@@ -29,7 +29,8 @@ module Agents
29
29
  'meta_fields_map' => {},
30
30
  'meta_fields_namespace' => '',
31
31
  'mode' => modes[0],
32
- 'not_purchasable_format_list' => []
32
+ 'not_purchasable_format_list' => [],
33
+ 'should_disambiguate' => false
33
34
  }
34
35
  end
35
36
 
@@ -64,9 +65,14 @@ module Agents
64
65
  errors.add(:base, "mode is a required field and must be one of: #{modes.join(', ')}")
65
66
  end
66
67
 
67
- unless options['not_purchasable_format_list'].present? && !options['not_purchasable_format_list'].is_a?(Array)
68
+ if options['not_purchasable_format_list'].present? && !options['not_purchasable_format_list'].is_a?(Array)
68
69
  errors.add(:base, 'not_purchasable_format_list must be an Array')
69
70
  end
71
+
72
+ if options.has_key?('should_disambiguate') && boolify(options['should_disambiguate']).nil?
73
+ errors.add(:base, 'when provided, `should_disambiguate` must be either true or false')
74
+ end
75
+
70
76
  end
71
77
 
72
78
  def working?
@@ -124,6 +130,16 @@ module Agents
124
130
  wrapper_skus.map {|k,v| v},
125
131
  %w[custom_fields options]
126
132
  )
133
+ #save skus
134
+ digital_skus = []
135
+ physical_skus = []
136
+ if split[:digital] and split[:physical]
137
+ digital_skus.concat([wrapper_skus[:digital]])
138
+ digital_skus.concat(get_mapper(:ProductMapper).get_product_skus(split[:digital])).join(",")
139
+ physical_skus.concat([wrapper_skus[:physical]])
140
+ physical_skus.concat(get_mapper(:ProductMapper).get_product_skus(split[:physical])).join(",")
141
+ end
142
+
127
143
  # upsert wrapper products
128
144
  split.each do |type, product|
129
145
  is_digital = type == :digital ? true : false
@@ -133,15 +149,26 @@ module Agents
133
149
  product['name'] = "#{product['name']} (Digital)"
134
150
  end
135
151
 
152
+ # Ignatius Press -- some products have the same name and must be disambiguated.
153
+ # ...by adding a list of the product types (hardback, paperback, etc.) to their names
154
+ if boolify(options['should_disambiguate'])
155
+ product['name'] += " |~ " + product['model'].map { |m|
156
+ m['additionalProperty'].find { |p|
157
+ p['propertyID'] == 'option'
158
+ }['value']
159
+ }.join(", ")
160
+ end
161
+
136
162
  wrapper_sku = wrapper_skus[type]
137
163
  bc_product = bc_products[wrapper_sku]
138
164
  variant_option_name = get_mapper(:OptionMapper).variant_option_name
139
165
  bc_option = !bc_product.nil? ? bc_product['options'].select {|opt| opt['display_name'] === variant_option_name}.first : nil
140
166
 
167
+ search_skus = is_digital ? physical_skus : digital_skus
141
168
  # ##############################
142
169
  # 1. update wrapper product
143
170
  # ##############################
144
- upsert_result = upsert_product(wrapper_sku, product, bc_product, is_digital)
171
+ upsert_result = upsert_product(wrapper_sku, product, bc_product, is_digital, search_skus)
145
172
  bc_product = upsert_result[:product]
146
173
 
147
174
  # clean up custom/meta fields. there are not batch operations so we might as well do them here.
@@ -337,7 +364,7 @@ module Agents
337
364
  return ::BigcommerceProductAgent::Mapper.const_get(class_name.to_sym)
338
365
  end
339
366
 
340
- def upsert_product(sku, product, bc_product = nil, is_digital=false)
367
+ def upsert_product(sku, product, bc_product = nil, is_digital=false, search_skus=[])
341
368
  custom_fields_updates = get_mapper(:CustomFieldMapper).map(
342
369
  interpolated['custom_fields_map'],
343
370
  product,
@@ -350,7 +377,10 @@ module Agents
350
377
  sku,
351
378
  product,
352
379
  product_id,
353
- { custom_fields: custom_fields_updates[:upsert] },
380
+ {
381
+ additional_search_terms: search_skus,
382
+ custom_fields: custom_fields_updates[:upsert]
383
+ },
354
384
  is_digital,
355
385
  )
356
386
 
@@ -19,6 +19,9 @@ module BigcommerceProductAgent
19
19
  "width" => default_variant['width'],
20
20
  "depth" => default_variant['depth'],
21
21
  "height" => default_variant['height'],
22
+ "releaseDate" => default_variant['releaseDate'],
23
+ "datePublished" => default_variant['datePublished'],
24
+ "availability" => self.get_availability_offer(product, default_variant),
22
25
  }.merge(product)
23
26
  end
24
27
  end
@@ -31,15 +34,18 @@ module BigcommerceProductAgent
31
34
  description: product['description'] || '',
32
35
  price: product['offers'] && product['offers'][0] ? product['offers'][0]['price'] : '0',
33
36
  categories: self.get_categories(product),
34
- available: 'available',
37
+ availability: self.get_availability(product),
35
38
  weight: product['weight'] ? product['weight']['value'] : '0',
36
39
  width: product['width'] ? product['width']['value'] : '0',
37
40
  depth: product['depth'] ? product['depth']['value'] : '0',
38
41
  height: product['height'] ? product['height']['value'] : '0',
39
42
  meta_keywords: self.meta_keywords(product),
40
43
  meta_description: self.meta_description(product) || '',
41
- search_keywords: self.meta_keywords(product).join(','),
44
+ search_keywords: self.get_search_keywords(additional_data.delete(:additional_search_terms), product),
42
45
  is_visible: variant ? false : true,
46
+ preorder_release_date: product['releaseDate'] && product['releaseDate'].to_datetime ? product['releaseDate'].to_datetime.strftime("%FT%T%:z") : nil,
47
+ preorder_message: self.get_availability(product) == 'preorder' ? product['availability'] : '',
48
+ is_preorder_only: self.get_availability(product) == 'preorder' ? true : false,
43
49
  }
44
50
  result[:upc] = product['gtin12'] if product['gtin12']
45
51
 
@@ -182,6 +188,38 @@ module BigcommerceProductAgent
182
188
  end
183
189
  end
184
190
  end
191
+
192
+ # get a list of search keywords for the products
193
+ def self.get_search_keywords(additional_search_terms, product)
194
+ return (self.meta_keywords(product) + additional_search_terms.split(",")).join(",")
195
+ end
196
+
197
+ def self.get_availability(product)
198
+ if product['datePublished'] && product['datePublished'].to_datetime && product['datePublished'].to_datetime < DateTime.now
199
+ return 'available'
200
+ else
201
+ if product['releaseDate'] && product['releaseDate'].to_datetime
202
+ if product['releaseDate'].to_datetime > DateTime.now
203
+ return 'preorder'
204
+ else
205
+ return 'available'
206
+ end
207
+ else
208
+ return 'disabled'
209
+ end
210
+ end
211
+ end
212
+
213
+ def self.get_availability_offer(product, variant)
214
+ if product['offers'] && product['offers'][0]
215
+ return product['offers'][0]['availability']
216
+ else
217
+ if variant['offers'] && variant['offers'][0]
218
+ return variant['offers'][0]['availability']
219
+ end
220
+ end
221
+ return ''
222
+ end
185
223
  end
186
224
  end
187
225
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huginn_bigcommerce_product_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Spizziri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-05 00:00:00.000000000 Z
11
+ date: 2020-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler