jekyll-spree-client 0.1.6 → 0.1.11

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: ec972940eca59000e0a3ba7bd764b84531c872e39044f85f2e2a4c4ec61c9ca8
4
- data.tar.gz: 3be5a5a3d475b392974c667a2c9b15bad9293e63115b425c7b4f300afd8d1eeb
3
+ metadata.gz: 57f0f9a9615cb8ecd401ff1c4224b1c246b33489809187f1dacd99bc8efdaba0
4
+ data.tar.gz: f9279e9889b35a6e7ef42013ca44590796f1530178973d99da5b30d4bb6adcfd
5
5
  SHA512:
6
- metadata.gz: e02ca9264d8a137c65f5b3e0de3d97171a5432ffe5cab698e8249192ffeb50989993869e6fa9527e042630bb48ed8268f2a3b043158a7e0708483624c4e992ac
7
- data.tar.gz: d4da262c648e4006926073e86cd884783a746091784cf96f5544611c428c9657611398ad9f1f6c56e9063ef5b12e3c301647c37f0e10a434884ac334d774859d
6
+ metadata.gz: 9f77636bc5292b48977e0e7d43de5609d3437568c93aaac1d80f3e9c485b98b87c368d2351770a0c6a047129a1ba2c4a4dc136e6b2ee83ecaf22e0243e37b2ed
7
+ data.tar.gz: 71243959c46fa41db5d1db9e9a3d661e9284688257b6b00f2a278a6c7174562a0cfdfdfd2197500d141571843f1c6af6290877cbcc70e6bb0590b7ded16ebd80
data/README.md CHANGED
@@ -16,16 +16,6 @@ conversion unless you store prices as strings.
16
16
 
17
17
  You can use Integer and Floats now!
18
18
 
19
- ### Update
20
-
21
- The issue is that Spree expects to receive prices in the locale format
22
- (ie, "200,00") but will return them on Ruby's `to_s` conversion
23
- ("200.0") so we need to reconvert them to our current format and juggle
24
- between them.
25
-
26
- We'll deal with `Integer` for now until we find [a better
27
- solution](https://github.com/spree/spree/issues/10556).
28
-
29
19
  ## Installation
30
20
 
31
21
  Add this line to your site's Gemfile:
@@ -77,8 +67,10 @@ Add the required fields in their frontmatter:
77
67
  ---
78
68
  layout: post
79
69
  title: A product
70
+ description: With a description
80
71
  sku: A unique ID
81
72
  price: 100.00
73
+ cost_price: 50.0
82
74
  stock: 100
83
75
  weight: 100
84
76
  height: 100
@@ -90,18 +82,64 @@ depth: 100
90
82
  The layout doesn't matter, but we recommend to use a layout for
91
83
  products.
92
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
+
97
+ ### Hooks
98
+
99
+ If you need to do some data manipulation before and after
100
+ synchronization is done, you can use hooks. In a `_plugins/spree.rb`
101
+ file:
102
+
103
+ ```ruby
104
+ Jekyll::Hooks.register :spree, :pre_render do |spree|
105
+ spree.local_products.each do |product|
106
+ product.data['_description'] = product.data['description']
107
+ product.data['description'] = product.data['productore']&.data&.dig('title')
108
+ end
109
+ end
110
+
111
+ Jekyll::Hooks.register :spree, :post_render do |spree|
112
+ spree.local_products.each do |product|
113
+ product.data['description'] = product.data.delete '_description'
114
+
115
+ product.data.delete 'description' unless product.data['description']
116
+ end
117
+ end
118
+ ```
119
+
120
+ This plugin gets the description value from another document (they're
121
+ linked using
122
+ [jekyll-linked-posts](https://0xacab.org/sutty/jekyll/jekyll-linked-posts/),
123
+ it's a real example!), and then recovers it.
124
+
125
+ * `pre_render` runs just before starting synchronization
126
+ * `post_render` run after synchronization and before writing changes
127
+
93
128
  ### Building the site
94
129
 
95
130
  During site build, the plugin will communicate with the Spree API and
96
- create products when they don't exist or update their price, current
97
- stock and the other values.
131
+ create products when they don't exist or locally update their price,
132
+ current stock and the other values.
98
133
 
99
134
  Some fields will appear on the frontmatter:
100
135
 
101
136
  ```yaml
102
137
  variant_id: 1
103
138
  in_stock: true
104
- sync_error: 2020-10-10 00:00:00 UTC
139
+ errors:
140
+ time: 2020-10-10 00:00:00 UTC
141
+ messages:
142
+ - Something went wrong
105
143
  ```
106
144
 
107
145
  * `variant_id` is the ID in the Spree database.
@@ -109,11 +147,17 @@ sync_error: 2020-10-10 00:00:00 UTC
109
147
  * `in_stock` is true or false according to availability (Spree decides
110
148
  this).
111
149
 
112
- * `sync_error` if the synchronization fails there will be a timestamp
113
- here.
150
+ * `errors` if the synchronization fails there will be a timestamp and
151
+ some messages here.
114
152
 
115
153
  ## Notes
116
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.
157
+
158
+ We want this to change in the near future but we need to think a way
159
+ to prevent conflicts.
160
+
117
161
  * All products are created as "master variants" on Spree. There will be
118
162
  no pictures, taxonomies or other values, only basic product
119
163
  information.
@@ -126,11 +170,12 @@ sync_error: 2020-10-10 00:00:00 UTC
126
170
  * Stock is added to the default stock location, meaning the first active
127
171
  one. The API doesn't show or allows to query the actual default.
128
172
 
129
- * The `price` is in the default currency.
173
+ * `price` and `cost_price` are expressed in Spree's default currency.
130
174
 
131
175
  * If you install the `jekyll-write-and-commit-changes` plugin it will
132
- save the data back to disk when you build with `JEKYLL_ENV=production`
133
- environment variable.
176
+ save the data back to disk when you build. Using the
177
+ `JEKYLL_ENV=production` environment variable will also commit changes
178
+ to git.
134
179
 
135
180
  * Check the shipping category ID on Spree, since there's no API for it,
136
181
  you need to provide it manually, default is `1`.
@@ -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
+ unless ENV['SPREE_API_KEY']
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'])
@@ -33,7 +33,7 @@ module Jekyll
33
33
  #
34
34
  # @return [Array]
35
35
  def variant_fields
36
- @variant_fields ||= %w[price weight height width depth].freeze
36
+ @variant_fields ||= %w[price weight height width depth description cost_price].freeze
37
37
  end
38
38
 
39
39
  # Spree Client
@@ -56,6 +56,8 @@ module Jekyll
56
56
  end
57
57
 
58
58
  def sync!
59
+ Jekyll::Hooks.trigger :spree, :pre_render, self
60
+
59
61
  # The API allows to query several SKUs at the same time, so we send
60
62
  # groups of 10 products.
61
63
  local_products.each_slice(10) do |products|
@@ -68,13 +70,14 @@ module Jekyll
68
70
  products.each do |p|
69
71
  mark_with_error p,
70
72
  i18n.dig('spree', 'errors', 'api') || "Couldn't obtain variants, the store may be down or API key is incorrect"
71
- local_save p.save
72
73
  end
73
74
 
74
75
  next
75
76
  end
76
77
 
77
78
  products.each do |product|
79
+ # Remove previous errors
80
+ product.data.delete 'errors'
78
81
  # Find the corresponding Spree variant in the response
79
82
  variant = response['variants'].find do |v|
80
83
  v[sku_field] == product.data[sku_field]
@@ -88,6 +91,12 @@ module Jekyll
88
91
  end
89
92
  end
90
93
  end
94
+
95
+ Jekyll::Hooks.trigger :spree, :post_render, self
96
+
97
+ local_products.each do |product|
98
+ local_save product
99
+ end
91
100
  end
92
101
 
93
102
  private
@@ -152,19 +161,18 @@ module Jekyll
152
161
  def update(product, variant)
153
162
  # XXX: The API replies with stringified numbers so we convert them
154
163
  # back to the type we're using locally.
155
- product.data.merge! variant.slice(*variant_fields).map do |k, v|
156
- case product.data[k]
157
- when Integer then v.to_i
158
- when Float then v.to_f
159
- else v
160
- end
161
- end.to_h
164
+ variant.slice(*variant_fields).each do |k, v|
165
+ product.data[k] = case product.data[k]
166
+ when Integer then v.to_i
167
+ when Float then v.to_f
168
+ else v
169
+ end
170
+ end
162
171
 
163
172
  product.data['in_stock'] = variant['in_stock']
164
173
  product.data['variant_id'] = variant['id']
165
- product.data.delete 'errors'
166
174
 
167
- local_save product
175
+ product
168
176
  end
169
177
 
170
178
  # If the product doesn't exist, create it.
@@ -179,16 +187,12 @@ module Jekyll
179
187
  unless remote_products.create **new_product
180
188
  Jekyll.logger.error "Couldn't create #{product.data['title']}"
181
189
  mark_with_error product, error_messages
182
- local_save product
183
190
 
184
191
  return false
185
192
  end
186
193
 
187
194
  product.data['variant_id'] = remote_products.response.dig('master', 'id')
188
195
 
189
- # Save the variant ID
190
- local_save product
191
-
192
196
  # Add the initial stock
193
197
  spree.stock_items(id: product.data['variant_id'], stock_location_id: stock_location['id'])
194
198
  .stock_movements.create(quantity: product.data['stock'])
@@ -202,7 +206,7 @@ module Jekyll
202
206
  errors.map do |error|
203
207
  field + ' ' + error
204
208
  end
205
- end.flatten
209
+ end&.flatten
206
210
  end
207
211
  end
208
212
  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.6
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-30 00:00:00.000000000 Z
11
+ date: 2021-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree-api-client