jekyll-spree-client 0.1.15 → 0.1.19

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: 416bd768f1cdbf0a968e36466b0792a96f3f0ee4178b0a22654b5d82c973d170
4
- data.tar.gz: e8267b5cf04604fce87b7fab7e26725a83a62cf69d471fb966b3f7897756aead
3
+ metadata.gz: 93b1f2e26bbbb3e0759b4705192ada639098ae129c5cab8fadb494bfccd9e096
4
+ data.tar.gz: 225d56e40a98ecfd64aa25c3e85f80424ec478a3613b1746034340061fa3fab0
5
5
  SHA512:
6
- metadata.gz: ce0e083f63dc3cfa20f114e4a020047c0d3c946b99efce126bf39043e11dd589d441da4988bf5d58f3b81e33375e931182340e35143f5cee9af49cb568b0fc05
7
- data.tar.gz: cbf6e5406e93e6fb7ddd3f42cffb907de583d77101369b66743cb56a01f9d0df736f999e141f9fb2a8d24e4c0e2a5a89447c9f5c4b873a2071671341a405672a
6
+ metadata.gz: 9a842907fd0fa5ea1e8d7b3f36c577b8b99651edf960508df7231a524b6c3abbb27ddcd6dd4cddae83cb6059bed97aa84033fa8646f69c100bd99661427bc9fb
7
+ data.tar.gz: fd25f00e71e59a5299b62643a940666066ca69e96e90fe5b538cd47f815a5d6d62064467062f0d54588b864afdcc2a3d4ac0e69e8b854416ca9d012a762a1eef
data/README.md CHANGED
@@ -43,6 +43,7 @@ spree:
43
43
  sku_field: sku
44
44
  api_url: http://localhost:3000
45
45
  shipping_category_id: 1
46
+ store: https://your.store
46
47
  ```
47
48
 
48
49
  You can get the API key on your user profile. Pass it as the
@@ -52,7 +53,7 @@ You can get the API key on your user profile. Pass it as the
52
53
  SPREE_API_KEY="some random value generated by spree" bundle exec jekyll build
53
54
  ```
54
55
 
55
- Since v0.1.4 you can pass the store URL too, and it'll take precedence
56
+ Since v0.1.4 you can pass the API URL too, and it'll take precedence
56
57
  over `_config.yml`:
57
58
 
58
59
  ```bash
@@ -5,7 +5,7 @@ require 'spree_client'
5
5
 
6
6
  #
7
7
  module Jekyll
8
- SpreeClient = Struct.new :site, :sku_field, :api_key, :spree_url, :shipping_category_id, keyword_init: true do
8
+ SpreeClient = Struct.new :site, :sku_field, :api_key, :spree_url, :shipping_category_id, :store, keyword_init: true do
9
9
  # Products are posts with the SKU field
10
10
  #
11
11
  # @return [Array] Jekyll::Document
@@ -33,28 +33,33 @@ module Jekyll
33
33
  #
34
34
  # @return [Array]
35
35
  def variant_fields
36
- @variant_fields ||= %w[price weight height width depth cost_price pay_what_you_can].freeze
36
+ @variant_fields ||= %w[sku price weight height width depth cost_price].freeze
37
37
  end
38
38
 
39
39
  # Fields that can change locally and we need to sync to Spree
40
40
  #
41
41
  # @return [Array]
42
42
  def product_fields
43
- @product_fields ||= %w[name description meta_description meta_keywords meta_title].freeze
43
+ @product_fields ||= %w[name description meta_description meta_keywords meta_title pay_what_you_can extra_attributes].freeze
44
44
  end
45
45
 
46
46
  # All fields, using during creation
47
47
  #
48
48
  # @return [Array]
49
49
  def create_fields
50
- @create_fields ||= product_fields + variant_fields
50
+ @create_fields ||= (product_fields + variant_fields).freeze
51
+ end
52
+
53
+ # Fields which are only updated on the Variant.
54
+ def variant_only_fields
55
+ @variant_only_fields ||= %w[track_inventory].freeze
51
56
  end
52
57
 
53
58
  # Spree Client
54
59
  #
55
60
  # @return [SpreeClient::API::V1]
56
61
  def spree
57
- @spree ||= ::SpreeClient::API::V1.new **to_h.slice(:api_key, :spree_url)
62
+ @spree ||= ::SpreeClient::API::V1.new **to_h.slice(:api_key, :spree_url, :store)
58
63
  end
59
64
 
60
65
  # Localization with jekyll-locales
@@ -180,18 +185,23 @@ module Jekyll
180
185
  # @return [true,false]
181
186
  def update(product, variant)
182
187
  # 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)
188
+ remote_product = attributes_from product, product_fields
189
+ variant_comparison = attributes_from variant, product_fields
185
190
 
186
191
  # Only when they haven't changed, so we don't waste time sending a
187
192
  # request.
188
193
  unless remote_product.slice(*variant_comparison.keys) == variant_comparison
189
194
  # XXX: We don't have the product ID with the variant but we have
190
195
  # the slug.
196
+ remote_variant = { id: variant['id'] }.merge!(attributes_from product, variant_only_fields)
197
+
198
+ remote_product.delete_if do |k, v|
199
+ remote_variant.key? k
200
+ end
201
+
191
202
  remote_product[:id] = variant['slug']
192
- remote_product[:sku] = product.data[sku_field]
193
203
 
194
- unless remote_products.update **remote_product
204
+ unless remote_products.update(**remote_product) && spree.variants.update(**remote_variant)
195
205
  Jekyll.logger.error "Couldn't update #{product.data['title']}"
196
206
  mark_with_error product, error_messages
197
207
  end
@@ -218,21 +228,22 @@ module Jekyll
218
228
  # @param [Jekyll::Document]
219
229
  # @return [true,false]
220
230
  def create(product)
221
- new_product = product.data.slice(*create_fields).transform_keys(&:to_sym)
222
- new_product[:sku] = product.data[sku_field]
231
+ new_product = attributes_from(product, create_fields)
223
232
 
224
- unless remote_products.create **new_product
233
+ unless remote_products.create(**new_product)
225
234
  Jekyll.logger.error "Couldn't create #{product.data['title']}"
226
235
  mark_with_error product, error_messages
227
236
 
228
237
  return false
229
238
  end
230
239
 
231
- product.data['variant_id'] = remote_products.response.dig('master', 'id')
240
+ variant_id = remote_products.response.dig('master', 'id')
241
+ spree.variants(id: variant_id).update(attributes_from(product, variant_only_fields))
242
+ product.data['variant_id'] = variant_id
232
243
  product.data['in_stock'] = product.data['stock'].to_i > 0
233
244
 
234
245
  # Add the initial stock
235
- spree.stock_items(id: product.data['variant_id'], stock_location_id: stock_location['id'])
246
+ spree.stock_items(id: variant_id, stock_location_id: stock_location['id'])
236
247
  .stock_movements.create(quantity: product.data['stock'])
237
248
 
238
249
  true
@@ -246,5 +257,14 @@ module Jekyll
246
257
  end
247
258
  end&.flatten
248
259
  end
260
+
261
+ # Converts fields from a Jekyll::Document into an attribute Hash.
262
+ #
263
+ # @param [Jekyll::Document,Hash] Document or Hash to extract data from
264
+ # @param [Array] An Array of field names
265
+ # @return [Hash] A symbolized Hash of fields and values
266
+ def attributes_from(document_or_hash, fields)
267
+ (document_or_hash.respond_to?(:data) ? document_or_hash.data : document_or_hash).slice(*fields).transform_keys(&:to_sym)
268
+ end
249
269
  end
250
270
  end
@@ -22,7 +22,7 @@ Jekyll::Hooks.register :site, :post_read do |site|
22
22
  end
23
23
 
24
24
  config = site.config['spree']&.transform_keys(&:to_sym) || {}
25
- config.merge!(site: site, api_key: ENV['SPREE_API_KEY'], spree_url: ENV['SPREE_URL'])
25
+ config.merge!(site: site, api_key: ENV['SPREE_API_KEY'], spree_url: ENV['SPREE_URL'], store: site.config['url'])
26
26
 
27
27
  client = Jekyll::SpreeClient.new **config
28
28
 
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-spree-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-02 00:00:00.000000000 Z
11
+ date: 2021-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree-api-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0.2'
19
+ version: 0.2.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0.2'
26
+ version: 0.2.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: fast_blank
29
29
  requirement: !ruby/object:Gem::Requirement