Class: Jekyll::SpreeClient
- Inherits:
-
Struct
- Object
- Struct
- Jekyll::SpreeClient
- Defined in:
- lib/jekyll/spree_client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#shipping_category_id ⇒ Integer
Default shipping category ID.
-
#site ⇒ Object
Returns the value of attribute site.
-
#sku_field ⇒ String
SKU identifies a unique product.
-
#spree_url ⇒ Object
Returns the value of attribute spree_url.
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
-
#create_fields ⇒ Array
All fields, using during creation.
-
#i18n ⇒ Hash
Localization with jekyll-locales.
-
#local_products ⇒ Array
Products are posts with the SKU field.
-
#locale ⇒ String, Nil
Current locale.
-
#product_fields ⇒ Array
Fields that can change locally and we need to sync to Spree.
-
#spree ⇒ SpreeClient::API::V1
Spree Client.
- #sync! ⇒ Object
-
#variant_fields ⇒ Array
Fields that can change on Spree and we need to sync locally.
-
#variant_only_fields ⇒ Object
Fields which are only updated on the Variant.
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key
8 9 10 |
# File 'lib/jekyll/spree_client.rb', line 8 def api_key @api_key end |
#shipping_category_id ⇒ Integer
Default shipping category ID
28 29 30 |
# File 'lib/jekyll/spree_client.rb', line 28 def shipping_category_id @shipping_category_id end |
#site ⇒ Object
Returns the value of attribute site
8 9 10 |
# File 'lib/jekyll/spree_client.rb', line 8 def site @site end |
#sku_field ⇒ String
SKU identifies a unique product
21 22 23 |
# File 'lib/jekyll/spree_client.rb', line 21 def sku_field @sku_field end |
#spree_url ⇒ Object
Returns the value of attribute spree_url
8 9 10 |
# File 'lib/jekyll/spree_client.rb', line 8 def spree_url @spree_url end |
#store ⇒ Object
Returns the value of attribute store
8 9 10 |
# File 'lib/jekyll/spree_client.rb', line 8 def store @store end |
Instance Method Details
#create_fields ⇒ Array
All fields, using during creation
49 50 51 |
# File 'lib/jekyll/spree_client.rb', line 49 def create_fields @create_fields ||= (product_fields + variant_fields).freeze end |
#i18n ⇒ Hash
Localization with jekyll-locales
67 68 69 |
# File 'lib/jekyll/spree_client.rb', line 67 def i18n @i18n ||= locale ? site.data[locale] : {} end |
#local_products ⇒ Array
Products are posts with the SKU field
12 13 14 15 16 |
# File 'lib/jekyll/spree_client.rb', line 12 def local_products @local_products ||= site.documents.select do |doc| !doc.data.dig(sku_field).to_s.blank? end end |
#locale ⇒ String, Nil
Current locale
73 74 75 |
# File 'lib/jekyll/spree_client.rb', line 73 def locale @locale ||= site.config['locale'] end |
#product_fields ⇒ Array
Fields that can change locally and we need to sync to Spree
42 43 44 |
# File 'lib/jekyll/spree_client.rb', line 42 def product_fields @product_fields ||= %w[name description meta_description meta_keywords meta_title pay_what_you_can extra_attributes].freeze end |
#spree ⇒ SpreeClient::API::V1
Spree Client
61 62 63 |
# File 'lib/jekyll/spree_client.rb', line 61 def spree @spree ||= ::SpreeClient::API::V1.new **to_h.slice(:api_key, :spree_url, :store) end |
#sync! ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/jekyll/spree_client.rb', line 77 def sync! Jekyll::Hooks.trigger :spree, :pre_render, self # The API allows to query several SKUs at the same time, so we send # groups of 10 products. local_products.each_slice(10) do |products| skus = products.map { |p| p.data[sku_field] } # Gather variants by their SKU unless response = (variants skus) Jekyll.logger.error "Couldn't obtain variants" products.each do |p| mark_with_error p, i18n.dig('spree', 'errors', 'api') || "Couldn't obtain variants, the store may be down or API key is incorrect" end next end products.each do |product| # Products have names, not titles, so we save them for later product.data['_name'] = product.data['name'] if product.data.key? 'name' product.data['name'] = product.data['title'] # Remove previous errors product.data.delete 'errors' # Find the corresponding Spree variant in the response variant = response['variants'].find do |v| v[sku_field] == product.data[sku_field] end # If the variant already exists on Spree, update. if variant update product, variant else create product end product.data['name'] = product.data.delete('_name') if product.data.key? '_name' end end Jekyll::Hooks.trigger :spree, :post_render, self local_products.each do |product| local_save product end end |
#variant_fields ⇒ Array
Fields that can change on Spree and we need to sync locally
35 36 37 |
# File 'lib/jekyll/spree_client.rb', line 35 def variant_fields @variant_fields ||= %w[sku price weight height width depth cost_price].freeze end |
#variant_only_fields ⇒ Object
Fields which are only updated on the Variant.
54 55 56 |
# File 'lib/jekyll/spree_client.rb', line 54 def variant_only_fields @variant_only_fields ||= %w[track_inventory].freeze end |