jekyll-spree-client 0.1.4 → 0.1.9

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: 8a40d8af711c0a63af1d4f40c7573a52b33d34dc8c3f4f8c895e14e6e5952f97
4
- data.tar.gz: a8c8e002b61466f6fab8907c75e1243582511a2feb5ca77b1af1f1d2a0a0ae96
3
+ metadata.gz: 345d7f0e45d1c821288d7a4452601c163f2d82304d317dc72ec425d6abd5cb47
4
+ data.tar.gz: b74a132ff5c5592c2989bff62b2403e9a6cb10eacf4af715b5ee287c404f19a6
5
5
  SHA512:
6
- metadata.gz: 69badade74e187bf6926ef4bb2e155c3720416e3411a384489c927a41559849d4e838b14fd28fc82a157a89b377934d088e748e7eebc4ff83d9757bd6951c2d9
7
- data.tar.gz: a7ee374df482289ae15471665c3e784af2383f6b85c3b59444a16a88e17af31a8c21e8e6eda7e641cd3c90627064749ae6531a106a8431e13935fd16c921d84f
6
+ metadata.gz: a92e70c17670182523b32ad5e5f6038f3fa0611c23c731a61b30e9f36cb30df6f65dd406921830b35dda8fd303cb60f8738e2e895c73f180390a482762e6725f
7
+ data.tar.gz: f918174d0812624c71174e3c414160daa90c28539c1ae32859ce4c2046aa94ff9c222b5a551d3174009975c31939c7de75c30172a1a419290763b7249bf2418a
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,52 @@ depth: 100
90
82
  The layout doesn't matter, but we recommend to use a layout for
91
83
  products.
92
84
 
85
+ ### Hooks
86
+
87
+ If you need to do some data manipulation before and after
88
+ synchronization is done, you can use hooks. In a `_plugins/spree.rb`
89
+ file:
90
+
91
+ ```ruby
92
+ Jekyll::Hooks.register :spree, :pre_render do |spree|
93
+ spree.local_products.each do |product|
94
+ product.data['_description'] = product.data['description']
95
+ product.data['description'] = product.data['productore']&.data&.dig('title')
96
+ end
97
+ end
98
+
99
+ Jekyll::Hooks.register :spree, :post_render do |spree|
100
+ spree.local_products.each do |product|
101
+ product.data['description'] = product.data.delete '_description'
102
+
103
+ product.data.delete 'description' unless product.data['description']
104
+ end
105
+ end
106
+ ```
107
+
108
+ This plugin gets the description value from another document (they're
109
+ linked using
110
+ [jekyll-linked-posts](https://0xacab.org/sutty/jekyll/jekyll-linked-posts/),
111
+ it's a real example!), and then recovers it.
112
+
113
+ * `pre_render` runs just before starting synchronization
114
+ * `post_render` run after synchronization and before writing changes
115
+
93
116
  ### Building the site
94
117
 
95
118
  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.
119
+ create products when they don't exist or locally update their price,
120
+ current stock and the other values.
98
121
 
99
122
  Some fields will appear on the frontmatter:
100
123
 
101
124
  ```yaml
102
125
  variant_id: 1
103
126
  in_stock: true
104
- sync_error: 2020-10-10 00:00:00 UTC
127
+ errors:
128
+ time: 2020-10-10 00:00:00 UTC
129
+ messages:
130
+ - Something went wrong
105
131
  ```
106
132
 
107
133
  * `variant_id` is the ID in the Spree database.
@@ -109,11 +135,17 @@ sync_error: 2020-10-10 00:00:00 UTC
109
135
  * `in_stock` is true or false according to availability (Spree decides
110
136
  this).
111
137
 
112
- * `sync_error` if the synchronization fails there will be a timestamp
113
- here.
138
+ * `errors` if the synchronization fails there will be a timestamp and
139
+ some messages here.
114
140
 
115
141
  ## Notes
116
142
 
143
+ * Synchronization is unidirectional. If the product doesn't exist on
144
+ Spree, it's created. If it does, the Jekyll document is updated.
145
+
146
+ We want this to change in the near future but we need to think a way
147
+ to prevent conflicts.
148
+
117
149
  * All products are created as "master variants" on Spree. There will be
118
150
  no pictures, taxonomies or other values, only basic product
119
151
  information.
@@ -126,11 +158,12 @@ sync_error: 2020-10-10 00:00:00 UTC
126
158
  * Stock is added to the default stock location, meaning the first active
127
159
  one. The API doesn't show or allows to query the actual default.
128
160
 
129
- * The `price` is in the default currency.
161
+ * `price` and `cost_price` are expressed in Spree's default currency.
130
162
 
131
163
  * 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.
164
+ save the data back to disk when you build. Using the
165
+ `JEKYLL_ENV=production` environment variable will also commit changes
166
+ to git.
134
167
 
135
168
  * Check the shipping category ID on Spree, since there's no API for it,
136
169
  you need to provide it manually, default is `1`.
@@ -6,7 +6,9 @@ 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
- config = site.config['spree']&.transform_keys(&:to_sym)
9
+ next unless ENV['SPREE_API_KEY']
10
+
11
+ config = site.config['spree']&.transform_keys(&:to_sym) || {}
10
12
  config.merge!(site: site, api_key: ENV['SPREE_API_KEY'], spree_url: ENV['SPREE_URL'])
11
13
 
12
14
  client = Jekyll::SpreeClient.new **config
@@ -16,4 +18,14 @@ Jekyll::Hooks.register :site, :post_read do |site|
16
18
  if ENV['JEKYLL_ENV'] == 'production' && site.respond_to?(:repository)
17
19
  site.repository.commit site.data.dig(site.config['locale'], 'spree', 'commit') || 'Spree synchronization'
18
20
  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
19
31
  end
@@ -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'])
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
4
+ version: 0.1.9
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-11 00:00:00.000000000 Z
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