jekyll-spree-client 0.1.14 → 0.1.18

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: 231ce6a54824c7e484336d5a80f90eecf82ccff491a0de8f80fcd7e81b8e0d9a
4
- data.tar.gz: 5808e4bf0454d60ead7c62f2e2ecf8828f4417c97f4a0a5f907039dffdc73591
3
+ metadata.gz: 3d065883f0802c7520e29ebaa9fad7b624c22d903c190b06450543351603e389
4
+ data.tar.gz: 1810529172893e3c5feb437a27817e15d51e1b5bcd494d2b80de2dea44ffedea
5
5
  SHA512:
6
- metadata.gz: 17d67e2e407cba9595c4218b01dc03512f75a4b959c1917ab0d95488cf045cd6e9296e11568a2aae01ab21ddd9fbf66d4e8c276d64b202fe67cfd8e834dce2ff
7
- data.tar.gz: 0ef761a1c5fb123635e4e1fd9f6636e5ff2b3ae7297d8ebfc4b937d1337be7d46ccb7a2bd9729ea27f8f56706b3dad3b1688669633f52afc522bab5a0eee61f5
6
+ metadata.gz: a43430f248135eaad2522f0975af3c07cec45e4a4144f226435bfd2b3f097d9d525738d1401450ccbeb34cd3704023588c183a5391ccba1604ea4077d960b773
7
+ data.tar.gz: 16e594d12bbc16c8ae40a6b851065a3024897d66cf6585c461a8c6255ad9cc1590276c79cf9ff2920103259b36615f6a9bd13787342cf1586b4c9cae1d285b04
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].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,20 +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
243
+ product.data['in_stock'] = product.data['stock'].to_i > 0
232
244
 
233
245
  # Add the initial stock
234
- 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'])
235
247
  .stock_movements.create(quantity: product.data['stock'])
236
248
 
237
249
  true
@@ -245,5 +257,14 @@ module Jekyll
245
257
  end
246
258
  end&.flatten
247
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
248
269
  end
249
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.14
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-11 00:00:00.000000000 Z
11
+ date: 2021-09-01 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.3
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.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: fast_blank
29
29
  requirement: !ruby/object:Gem::Requirement