huginn_bigcommerce_product_agent 1.4.3 → 1.9.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: 4d9046f3fa0d6c9e3f0c58720148502c622ec9662cecc03488d381f5c7730440
4
- data.tar.gz: 15652aee1e058bffc5a15920574bd93c7eb89ba0391c267cb6912c270a1f0810
3
+ metadata.gz: 8bbd3a133d224fea3726d9b0f8b86803957e919e225e983be8ac3a1c7c027312
4
+ data.tar.gz: 202e2165d11044d4f44e722aa39e61224b819b344304e73e3c9e888c0a20153a
5
5
  SHA512:
6
- metadata.gz: b75f6cc9fbfabb4c0143b33146d1c736fd4680657642c29e755621e12a5d1d5efb6aa1aadc4d3ee6494dea17d1f6dab20d34a15aacf8ec992be8c79430d89fc3
7
- data.tar.gz: 3fe2de4e3330ae759ef8e43b402a6a5ca7ac9e1ce21dac24c8ff2bbdb0e8680ca2fcde53f129d6ae5358a07d3f9e237eae08ae8b1c0fc1f378b3c880082d5239
6
+ metadata.gz: 87204345ec8502a89ac61b5de9bf21f8daf8ab34b3b22cb2563e6a55b90da6239e2dc92d096caf4e9b8e08b2591286ac3f4b8b36a6ba07efecafdbafdf3d93bb
7
+ data.tar.gz: abe689235f182acf5c2eb977d9179af9b7431a1537435093a140e54e16b1981bb901fa02ec2bdc8e3469c96fab3d314474f63714caa835d878edcb375223f781
@@ -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
 
@@ -15,10 +15,9 @@ module BigcommerceProductAgent
15
15
  begin
16
16
  response = client.put(uri, payload.to_json)
17
17
  return response.body['data']
18
- rescue Faraday::Error::ClientError => e
19
- puts e.inspect
20
- raise e
21
- end
18
+ rescue Faraday::Error::ClientError => e
19
+ raise e, "\n#{e.message}\nFailed to upsert variant with payload = #{payload.to_json}\n", e.backtrace
20
+ end
22
21
  end
23
22
 
24
23
  def get_by_skus(skus, include = %w[custom_fields modifiers])
@@ -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
 
@@ -67,6 +68,11 @@ module Agents
67
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,27 @@ 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['page_title'] = product['name']
156
+ product['name'] += " |~ " + product['model'].map { |m|
157
+ m['additionalProperty'].find { |p|
158
+ p['propertyID'] == 'option'
159
+ }['value']
160
+ }.join(", ")
161
+ end
162
+
136
163
  wrapper_sku = wrapper_skus[type]
137
164
  bc_product = bc_products[wrapper_sku]
138
165
  variant_option_name = get_mapper(:OptionMapper).variant_option_name
139
166
  bc_option = !bc_product.nil? ? bc_product['options'].select {|opt| opt['display_name'] === variant_option_name}.first : nil
140
167
 
168
+ search_skus = is_digital ? physical_skus : digital_skus
141
169
  # ##############################
142
170
  # 1. update wrapper product
143
171
  # ##############################
144
- upsert_result = upsert_product(wrapper_sku, product, bc_product, is_digital)
172
+ upsert_result = upsert_product(wrapper_sku, product, bc_product, is_digital, search_skus)
145
173
  bc_product = upsert_result[:product]
146
174
 
147
175
  # clean up custom/meta fields. there are not batch operations so we might as well do them here.
@@ -337,7 +365,7 @@ module Agents
337
365
  return ::BigcommerceProductAgent::Mapper.const_get(class_name.to_sym)
338
366
  end
339
367
 
340
- def upsert_product(sku, product, bc_product = nil, is_digital=false)
368
+ def upsert_product(sku, product, bc_product = nil, is_digital=false, search_skus=[])
341
369
  custom_fields_updates = get_mapper(:CustomFieldMapper).map(
342
370
  interpolated['custom_fields_map'],
343
371
  product,
@@ -350,7 +378,10 @@ module Agents
350
378
  sku,
351
379
  product,
352
380
  product_id,
353
- { custom_fields: custom_fields_updates[:upsert] },
381
+ {
382
+ additional_search_terms: search_skus,
383
+ custom_fields: custom_fields_updates[:upsert]
384
+ },
354
385
  is_digital,
355
386
  )
356
387
 
@@ -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,19 @@ 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,
49
+ page_title: product['page_title'] || '',
43
50
  }
44
51
  result[:upc] = product['gtin12'] if product['gtin12']
45
52
 
@@ -182,6 +189,38 @@ module BigcommerceProductAgent
182
189
  end
183
190
  end
184
191
  end
192
+
193
+ # get a list of search keywords for the products
194
+ def self.get_search_keywords(additional_search_terms, product)
195
+ return (self.meta_keywords(product) + additional_search_terms.split(",")).join(",")
196
+ end
197
+
198
+ def self.get_availability(product)
199
+ if product['datePublished'] && product['datePublished'].to_datetime && product['datePublished'].to_datetime < DateTime.now
200
+ return 'available'
201
+ else
202
+ if product['releaseDate'] && product['releaseDate'].to_datetime
203
+ if product['releaseDate'].to_datetime > DateTime.now
204
+ return 'preorder'
205
+ else
206
+ return 'available'
207
+ end
208
+ else
209
+ return 'disabled'
210
+ end
211
+ end
212
+ end
213
+
214
+ def self.get_availability_offer(product, variant)
215
+ if product['offers'] && product['offers'][0]
216
+ return product['offers'][0]['availability']
217
+ else
218
+ if variant['offers'] && variant['offers'][0]
219
+ return variant['offers'][0]['availability']
220
+ end
221
+ end
222
+ return ''
223
+ end
185
224
  end
186
225
  end
187
226
  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.3
4
+ version: 1.9.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-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler