jekyll-spree-client 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -2
  3. data/lib/jekyll/spree_client.rb +38 -3
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57f0f9a9615cb8ecd401ff1c4224b1c246b33489809187f1dacd99bc8efdaba0
4
- data.tar.gz: f9279e9889b35a6e7ef42013ca44590796f1530178973d99da5b30d4bb6adcfd
3
+ metadata.gz: 29f4a2f251e21575d7f5b4ef34af064f8fb80a7a2e494267d19336f201288c3d
4
+ data.tar.gz: 77521c26bb015d395fa515adf4270385c28de024a57725d08123c8c7d0daebd5
5
5
  SHA512:
6
- metadata.gz: 9f77636bc5292b48977e0e7d43de5609d3437568c93aaac1d80f3e9c485b98b87c368d2351770a0c6a047129a1ba2c4a4dc136e6b2ee83ecaf22e0243e37b2ed
7
- data.tar.gz: 71243959c46fa41db5d1db9e9a3d661e9284688257b6b00f2a278a6c7174562a0cfdfdfd2197500d141571843f1c6af6290877cbcc70e6bb0590b7ded16ebd80
6
+ metadata.gz: 71480aa397d2fe3e0191833bc55194d6d287ac1825c5ebe01ecbb341a13d1de79f16d1686a2b6a4ebd879cc882584d9bab940ded155208988386110104d00a8b
7
+ data.tar.gz: f917c8e9cf3675801c8240ab88bc05f19368fd1597065ec7d789f4ec82ed5acab22733e5dc65ba7dfc63cc703ed197de4c09d1207f9e79f0d1663253a647b2dc
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.
@@ -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].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,22 @@ 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
+ # Only when they haven't changed, so we don't waste time sending a
185
+ # request.
186
+ unless remote_product == variant.slice(*product_fields).transform_keys(&:to_sym)
187
+ # XXX: We don't have the product ID with the variant but we have
188
+ # the slug.
189
+ remote_product[:id] = variant['slug']
190
+ remote_product[:sku] = product.data[sku_field]
191
+
192
+ unless remote_products.update **remote_product
193
+ Jekyll.logger.error "Couldn't update #{product.data['title']}"
194
+ mark_with_error product, error_messages
195
+ end
196
+ end
197
+
162
198
  # XXX: The API replies with stringified numbers so we convert them
163
199
  # back to the type we're using locally.
164
200
  variant.slice(*variant_fields).each do |k, v|
@@ -180,9 +216,8 @@ module Jekyll
180
216
  # @param [Jekyll::Document]
181
217
  # @return [true,false]
182
218
  def create(product)
183
- new_product = product.data.slice(*variant_fields).transform_keys(&:to_sym)
219
+ new_product = product.data.slice(*create_fields).transform_keys(&:to_sym)
184
220
  new_product[:sku] = product.data[sku_field]
185
- new_product[:name] = product.data['title']
186
221
 
187
222
  unless remote_products.create **new_product
188
223
  Jekyll.logger.error "Couldn't create #{product.data['title']}"
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.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-03 00:00:00.000000000 Z
11
+ date: 2021-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree-api-client