jekyll-spree-client 0.1.5 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +63 -18
- data/lib/jekyll-spree-client.rb +15 -2
- data/lib/jekyll/spree_client.rb +19 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 716e8e0f2ab2c09b987a10a760ed2e4bf36ebb142ff09b1331a03caaa824c57a
|
4
|
+
data.tar.gz: 7f5048e01435bde67988e5d4690e3026aed28ffbe75684cfd3b4526febe0d1af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2824a7951b2c4e9affb55ba9bee0bc249f4e728db341b2704181a16130ec49274d248110a2c8d072921018ab2f807fc870978fd874891bced7925971560f92aa
|
7
|
+
data.tar.gz: 61cdaaca089e6fdbeeae9dc3be9d904730e0ba322fd653b36fa9b4d979cccda4c0aa618a7e21e46c7b62157c781e6d37f8035bb05f4f35cbb8a5f39d3ed8b370
|
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,
|
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
|
-
|
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
|
-
* `
|
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
|
-
*
|
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
|
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`.
|
data/lib/jekyll-spree-client.rb
CHANGED
@@ -6,9 +6,22 @@ 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
|
-
|
9
|
+
# Ordered articles
|
10
|
+
posts = site.site_payload['site']['posts']
|
10
11
|
|
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
|
23
|
+
|
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'])
|
13
26
|
|
14
27
|
client = Jekyll::SpreeClient.new **config
|
data/lib/jekyll/spree_client.rb
CHANGED
@@ -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
|
-
|
156
|
-
case product.data[k]
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
end
|
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
|
-
|
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'])
|
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.
|
4
|
+
version: 0.1.10
|
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
|
+
date: 2020-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: spree-api-client
|