jekyll-spree-client 0.1.10 → 0.1.15

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: 716e8e0f2ab2c09b987a10a760ed2e4bf36ebb142ff09b1331a03caaa824c57a
4
- data.tar.gz: 7f5048e01435bde67988e5d4690e3026aed28ffbe75684cfd3b4526febe0d1af
3
+ metadata.gz: 416bd768f1cdbf0a968e36466b0792a96f3f0ee4178b0a22654b5d82c973d170
4
+ data.tar.gz: e8267b5cf04604fce87b7fab7e26725a83a62cf69d471fb966b3f7897756aead
5
5
  SHA512:
6
- metadata.gz: 2824a7951b2c4e9affb55ba9bee0bc249f4e728db341b2704181a16130ec49274d248110a2c8d072921018ab2f807fc870978fd874891bced7925971560f92aa
7
- data.tar.gz: 61cdaaca089e6fdbeeae9dc3be9d904730e0ba322fd653b36fa9b4d979cccda4c0aa618a7e21e46c7b62157c781e6d37f8035bb05f4f35cbb8a5f39d3ed8b370
6
+ metadata.gz: ce0e083f63dc3cfa20f114e4a020047c0d3c946b99efce126bf39043e11dd589d441da4988bf5d58f3b81e33375e931182340e35143f5cee9af49cb568b0fc05
7
+ data.tar.gz: cbf6e5406e93e6fb7ddd3f42cffb907de583d77101369b66743cb56a01f9d0df736f999e141f9fb2a8d24e4c0e2a5a89447c9f5c4b873a2071671341a405672a
data/README.md CHANGED
@@ -152,8 +152,18 @@ errors:
152
152
 
153
153
  ## Notes
154
154
 
155
- * Synchronization is unidirectional. If the product doesn't exist on
156
- Spree, it's created. If it does, the Jekyll document is updated.
155
+ * Synchronization follows this rules.
156
+
157
+ If the product doesn't exist on Spree, it's created.
158
+
159
+ If it does, the Jekyll document is updated with price, weight,
160
+ dimensions and stock.
161
+
162
+ It means once the product is created on Spree, Spree manages this
163
+ variables.
164
+
165
+ But Product properties like description and metadata are updated from
166
+ Jekyll to Spree.
157
167
 
158
168
  We want this to change in the near future but we need to think a way
159
169
  to prevent conflicts.
@@ -16,7 +16,7 @@ Jekyll::Hooks.register :site, :post_read do |site|
16
16
  end
17
17
  end
18
18
 
19
- unless ENV['SPREE_API_KEY']
19
+ if ENV['SPREE_API_KEY'].nil? || ENV['SPREE_API_KEY'].empty?
20
20
  Jekyll.logger.warn "SPREE_API_KEY environment variable missing, skipping syncrhonization"
21
21
  next
22
22
  end
@@ -33,7 +33,21 @@ module Jekyll
33
33
  #
34
34
  # @return [Array]
35
35
  def variant_fields
36
- @variant_fields ||= %w[price weight height width depth description cost_price].freeze
36
+ @variant_fields ||= %w[price weight height width depth cost_price pay_what_you_can].freeze
37
+ end
38
+
39
+ # Fields that can change locally and we need to sync to Spree
40
+ #
41
+ # @return [Array]
42
+ def product_fields
43
+ @product_fields ||= %w[name description meta_description meta_keywords meta_title].freeze
44
+ end
45
+
46
+ # All fields, using during creation
47
+ #
48
+ # @return [Array]
49
+ def create_fields
50
+ @create_fields ||= product_fields + variant_fields
37
51
  end
38
52
 
39
53
  # Spree Client
@@ -76,6 +90,10 @@ module Jekyll
76
90
  end
77
91
 
78
92
  products.each do |product|
93
+ # Products have names, not titles, so we save them for later
94
+ product.data['_name'] = product.data['name'] if product.data.key? 'name'
95
+ product.data['name'] = product.data['title']
96
+
79
97
  # Remove previous errors
80
98
  product.data.delete 'errors'
81
99
  # Find the corresponding Spree variant in the response
@@ -89,6 +107,8 @@ module Jekyll
89
107
  else
90
108
  create product
91
109
  end
110
+
111
+ product.data['name'] = product.data.delete('_name') if product.data.key? '_name'
92
112
  end
93
113
  end
94
114
 
@@ -159,6 +179,24 @@ module Jekyll
159
179
  # @param [Hash]
160
180
  # @return [true,false]
161
181
  def update(product, variant)
182
+ # Sync local changes to Spree
183
+ remote_product = product.data.slice(*product_fields).transform_keys(&:to_sym)
184
+ variant_comparison = variant.slice(*product_fields).transform_keys(&:to_sym)
185
+
186
+ # Only when they haven't changed, so we don't waste time sending a
187
+ # request.
188
+ unless remote_product.slice(*variant_comparison.keys) == variant_comparison
189
+ # XXX: We don't have the product ID with the variant but we have
190
+ # the slug.
191
+ remote_product[:id] = variant['slug']
192
+ remote_product[:sku] = product.data[sku_field]
193
+
194
+ unless remote_products.update **remote_product
195
+ Jekyll.logger.error "Couldn't update #{product.data['title']}"
196
+ mark_with_error product, error_messages
197
+ end
198
+ end
199
+
162
200
  # XXX: The API replies with stringified numbers so we convert them
163
201
  # back to the type we're using locally.
164
202
  variant.slice(*variant_fields).each do |k, v|
@@ -180,9 +218,8 @@ module Jekyll
180
218
  # @param [Jekyll::Document]
181
219
  # @return [true,false]
182
220
  def create(product)
183
- new_product = product.data.slice(*variant_fields).transform_keys(&:to_sym)
221
+ new_product = product.data.slice(*create_fields).transform_keys(&:to_sym)
184
222
  new_product[:sku] = product.data[sku_field]
185
- new_product[:name] = product.data['title']
186
223
 
187
224
  unless remote_products.create **new_product
188
225
  Jekyll.logger.error "Couldn't create #{product.data['title']}"
@@ -192,6 +229,7 @@ module Jekyll
192
229
  end
193
230
 
194
231
  product.data['variant_id'] = remote_products.response.dig('master', 'id')
232
+ product.data['in_stock'] = product.data['stock'].to_i > 0
195
233
 
196
234
  # Add the initial stock
197
235
  spree.stock_items(id: product.data['variant_id'], stock_location_id: stock_location['id'])
@@ -206,7 +244,7 @@ module Jekyll
206
244
  errors.map do |error|
207
245
  field + ' ' + error
208
246
  end
209
- end.flatten
247
+ end&.flatten
210
248
  end
211
249
  end
212
250
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-spree-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-30 00:00:00.000000000 Z
11
+ date: 2021-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree-api-client