jekyll-spree-client 0.1.9 → 0.1.14

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: 345d7f0e45d1c821288d7a4452601c163f2d82304d317dc72ec425d6abd5cb47
4
- data.tar.gz: b74a132ff5c5592c2989bff62b2403e9a6cb10eacf4af715b5ee287c404f19a6
3
+ metadata.gz: 231ce6a54824c7e484336d5a80f90eecf82ccff491a0de8f80fcd7e81b8e0d9a
4
+ data.tar.gz: 5808e4bf0454d60ead7c62f2e2ecf8828f4417c97f4a0a5f907039dffdc73591
5
5
  SHA512:
6
- metadata.gz: a92e70c17670182523b32ad5e5f6038f3fa0611c23c731a61b30e9f36cb30df6f65dd406921830b35dda8fd303cb60f8738e2e895c73f180390a482762e6725f
7
- data.tar.gz: f918174d0812624c71174e3c414160daa90c28539c1ae32859ce4c2046aa94ff9c222b5a551d3174009975c31939c7de75c30172a1a419290763b7249bf2418a
6
+ metadata.gz: 17d67e2e407cba9595c4218b01dc03512f75a4b959c1917ab0d95488cf045cd6e9296e11568a2aae01ab21ddd9fbf66d4e8c276d64b202fe67cfd8e834dce2ff
7
+ data.tar.gz: 0ef761a1c5fb123635e4e1fd9f6636e5ff2b3ae7297d8ebfc4b937d1337be7d46ccb7a2bd9729ea27f8f56706b3dad3b1688669633f52afc522bab5a0eee61f5
data/README.md CHANGED
@@ -82,6 +82,18 @@ depth: 100
82
82
  The layout doesn't matter, but we recommend to use a layout for
83
83
  products.
84
84
 
85
+ ### Layouts
86
+
87
+ You'll need four layouts and at least a corresponding post, one for each
88
+ step of the buying process.
89
+
90
+ * `cart` the cart, shows the order details
91
+ * `shipment` shipping and billing addresses, shipping rates apply here
92
+ * `payment` select payment methods
93
+ * `confirmation` order confirmation page
94
+
95
+ They'll be available on Liquid under the `site.STEP` keys.
96
+
85
97
  ### Hooks
86
98
 
87
99
  If you need to do some data manipulation before and after
@@ -140,8 +152,18 @@ errors:
140
152
 
141
153
  ## Notes
142
154
 
143
- * Synchronization is unidirectional. If the product doesn't exist on
144
- 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.
145
167
 
146
168
  We want this to change in the near future but we need to think a way
147
169
  to prevent conflicts.
@@ -6,7 +6,20 @@ require_relative 'jekyll/spree_client'
6
6
  # matter. The SKU needs to be unique, but this plugin doesn't ensure
7
7
  # that. Duplicated SKUs will be silently ignored for now.
8
8
  Jekyll::Hooks.register :site, :post_read do |site|
9
- next unless ENV['SPREE_API_KEY']
9
+ # Ordered articles
10
+ posts = site.site_payload['site']['posts']
11
+
12
+ # Make steps available to other articles
13
+ %w[cart shipment payment confirmation].each do |step|
14
+ site.config[step] = posts.find do |doc|
15
+ doc.data['layout'] == step
16
+ end
17
+ end
18
+
19
+ if ENV['SPREE_API_KEY'].nil? || ENV['SPREE_API_KEY'].empty?
20
+ Jekyll.logger.warn "SPREE_API_KEY environment variable missing, skipping syncrhonization"
21
+ next
22
+ end
10
23
 
11
24
  config = site.config['spree']&.transform_keys(&:to_sym) || {}
12
25
  config.merge!(site: site, api_key: ENV['SPREE_API_KEY'], spree_url: ENV['SPREE_URL'])
@@ -18,14 +31,4 @@ Jekyll::Hooks.register :site, :post_read do |site|
18
31
  if ENV['JEKYLL_ENV'] == 'production' && site.respond_to?(:repository)
19
32
  site.repository.commit site.data.dig(site.config['locale'], 'spree', 'commit') || 'Spree synchronization'
20
33
  end
21
-
22
- # Ordered articles
23
- posts = site.site_payload['site']['posts']
24
-
25
- # Make steps available to other articles
26
- %w[cart shipment payment confirmation].each do |step|
27
- site.config[step] = posts.find do |doc|
28
- doc.data['layout'] == step
29
- end
30
- end
31
34
  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']}"
@@ -206,7 +243,7 @@ module Jekyll
206
243
  errors.map do |error|
207
244
  field + ' ' + error
208
245
  end
209
- end.flatten
246
+ end&.flatten
210
247
  end
211
248
  end
212
249
  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.9
4
+ version: 0.1.14
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-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree-api-client