jekyll-spree-client 0.1.13 → 0.1.17

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: c3313a5acca299870b21648a253999a7bda4fae1c50a8ada64ba08bf710ee677
4
- data.tar.gz: 15ce97b96ab22eb484960cea96a98549bc6ba972ba82599442f67d5ed6c32925
3
+ metadata.gz: cb1e9f91b6b5df295223e22a1a2323ddae55d62df040577a780b58d48ba5138f
4
+ data.tar.gz: 5d470993e67857cc5dbbba06c55dc29ae685c557b07b524a86899f6de7fe69dc
5
5
  SHA512:
6
- metadata.gz: 061be8dc0a119fd8b106e08dd5edc7506aa2d297fd213012f1442744349bcb8d0e955c1887597154510368e9c866e59e597d484f33ede18cf74e76caeb28c27e
7
- data.tar.gz: 2a6e0c0e080ba05a8d94d7b383d445fe7e7aa96b113f053ece6b9528d9e80ad1bfc886f41cad37e97c5bfbb533ab44d4e4099600ef7e6501cc3d4a6ea05d4db3
6
+ metadata.gz: 906fe73618cfaebffd6d61cf49d74dd031ed22df64710bbc6c0e9fb51feeef9fd651457c40543f78170e5a8a8843eaebd22c010fdce7cf804cec0375a473b86b
7
+ data.tar.gz: 5ffb592af45112839f81145fa60720d0a27583690b65e3b6d7a6cb4bfbdec4fbec1dca23e832e633b31e019b53548439ac8b69814112c9f23f62beb4ff7822e8
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
@@ -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
 
@@ -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[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,16 +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)
188
+ remote_product = attributes_from product, product_fields
189
+ variant_comparison = attributes_from variant, product_fields
190
+
184
191
  # Only when they haven't changed, so we don't waste time sending a
185
192
  # request.
186
- unless remote_product == variant.slice(*product_fields).transform_keys(&:to_sym)
193
+ unless remote_product.slice(*variant_comparison.keys) == variant_comparison
187
194
  # XXX: We don't have the product ID with the variant but we have
188
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
+
189
202
  remote_product[:id] = variant['slug']
190
- remote_product[:sku] = product.data[sku_field]
191
203
 
192
- unless remote_products.update **remote_product
204
+ unless remote_products.update(**remote_product) && spree.variants.update(**remote_variant)
193
205
  Jekyll.logger.error "Couldn't update #{product.data['title']}"
194
206
  mark_with_error product, error_messages
195
207
  end
@@ -216,20 +228,22 @@ module Jekyll
216
228
  # @param [Jekyll::Document]
217
229
  # @return [true,false]
218
230
  def create(product)
219
- new_product = product.data.slice(*create_fields).transform_keys(&:to_sym)
220
- new_product[:sku] = product.data[sku_field]
231
+ new_product = attributes_from(product, create_fields)
221
232
 
222
- unless remote_products.create **new_product
233
+ unless remote_products.create(**new_product)
223
234
  Jekyll.logger.error "Couldn't create #{product.data['title']}"
224
235
  mark_with_error product, error_messages
225
236
 
226
237
  return false
227
238
  end
228
239
 
229
- 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
230
244
 
231
245
  # Add the initial stock
232
- 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'])
233
247
  .stock_movements.create(quantity: product.data['stock'])
234
248
 
235
249
  true
@@ -243,5 +257,14 @@ module Jekyll
243
257
  end
244
258
  end&.flatten
245
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
246
269
  end
247
270
  end
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.13
4
+ version: 0.1.17
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-08-04 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