Class: Jekyll::SpreeClient

Inherits:
Struct
  • Object
show all
Defined in:
lib/jekyll/spree_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key

Returns:

  • (Object)

    the current value of api_key



8
9
10
# File 'lib/jekyll/spree_client.rb', line 8

def api_key
  @api_key
end

#shipping_category_idInteger

Default shipping category ID

Returns:

  • (Integer)


28
29
30
# File 'lib/jekyll/spree_client.rb', line 28

def shipping_category_id
  @shipping_category_id
end

#siteObject

Returns the value of attribute site

Returns:

  • (Object)

    the current value of site



8
9
10
# File 'lib/jekyll/spree_client.rb', line 8

def site
  @site
end

#sku_fieldString

SKU identifies a unique product

Returns:

  • (String)


21
22
23
# File 'lib/jekyll/spree_client.rb', line 21

def sku_field
  @sku_field
end

#spree_urlObject

Returns the value of attribute spree_url

Returns:

  • (Object)

    the current value of spree_url



8
9
10
# File 'lib/jekyll/spree_client.rb', line 8

def spree_url
  @spree_url
end

#storeObject

Returns the value of attribute store

Returns:

  • (Object)

    the current value of store



8
9
10
# File 'lib/jekyll/spree_client.rb', line 8

def store
  @store
end

Instance Method Details

#create_fieldsArray

All fields, using during creation

Returns:

  • (Array)


49
50
51
# File 'lib/jekyll/spree_client.rb', line 49

def create_fields
  @create_fields ||= (product_fields + variant_fields).freeze
end

#i18nHash

Localization with jekyll-locales

Returns:

  • (Hash)


67
68
69
# File 'lib/jekyll/spree_client.rb', line 67

def i18n
  @i18n ||= locale ? site.data[locale] : {}
end

#local_productsArray

Products are posts with the SKU field

Returns:

  • (Array)

    Jekyll::Document



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

#localeString, Nil

Current locale

Returns:

  • (String, Nil)


73
74
75
# File 'lib/jekyll/spree_client.rb', line 73

def locale
  @locale ||= site.config['locale']
end

#product_fieldsArray

Fields that can change locally and we need to sync to Spree

Returns:

  • (Array)


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

#spreeSpreeClient::API::V1

Spree Client

Returns:

  • (SpreeClient::API::V1)


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_fieldsArray

Fields that can change on Spree and we need to sync locally

Returns:

  • (Array)


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_fieldsObject

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