jekyll-spree-client 0.1.16 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/jekyll/spree_client.rb +32 -12
 - 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: cb1e9f91b6b5df295223e22a1a2323ddae55d62df040577a780b58d48ba5138f
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 5d470993e67857cc5dbbba06c55dc29ae685c557b07b524a86899f6de7fe69dc
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 906fe73618cfaebffd6d61cf49d74dd031ed22df64710bbc6c0e9fb51feeef9fd651457c40543f78170e5a8a8843eaebd22c010fdce7cf804cec0375a473b86b
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 5ffb592af45112839f81145fa60720d0a27583690b65e3b6d7a6cb4bfbdec4fbec1dca23e832e633b31e019b53548439ac8b69814112c9f23f62beb4ff7822e8
         
     | 
    
        data/lib/jekyll/spree_client.rb
    CHANGED
    
    | 
         @@ -33,21 +33,26 @@ module Jekyll 
     | 
|
| 
       33 
33 
     | 
    
         
             
                #
         
     | 
| 
       34 
34 
     | 
    
         
             
                # @return [Array]
         
     | 
| 
       35 
35 
     | 
    
         
             
                def variant_fields
         
     | 
| 
       36 
     | 
    
         
            -
                  @variant_fields ||= %w[price weight height width depth cost_price 
     | 
| 
      
 36 
     | 
    
         
            +
                  @variant_fields ||= %w[price weight height width depth cost_price].freeze
         
     | 
| 
       37 
37 
     | 
    
         
             
                end
         
     | 
| 
       38 
38 
     | 
    
         | 
| 
       39 
39 
     | 
    
         
             
                # Fields that can change locally and we need to sync to Spree
         
     | 
| 
       40 
40 
     | 
    
         
             
                #
         
     | 
| 
       41 
41 
     | 
    
         
             
                # @return [Array]
         
     | 
| 
       42 
42 
     | 
    
         
             
                def product_fields
         
     | 
| 
       43 
     | 
    
         
            -
                  @product_fields ||= %w[name description meta_description meta_keywords meta_title].freeze
         
     | 
| 
      
 43 
     | 
    
         
            +
                  @product_fields ||= %w[name description meta_description meta_keywords meta_title pay_what_you_can].freeze
         
     | 
| 
       44 
44 
     | 
    
         
             
                end
         
     | 
| 
       45 
45 
     | 
    
         | 
| 
       46 
46 
     | 
    
         
             
                # All fields, using during creation
         
     | 
| 
       47 
47 
     | 
    
         
             
                #
         
     | 
| 
       48 
48 
     | 
    
         
             
                # @return [Array]
         
     | 
| 
       49 
49 
     | 
    
         
             
                def create_fields
         
     | 
| 
       50 
     | 
    
         
            -
                  @create_fields ||= product_fields + variant_fields
         
     | 
| 
      
 50 
     | 
    
         
            +
                  @create_fields ||= (product_fields + variant_fields).freeze
         
     | 
| 
      
 51 
     | 
    
         
            +
                end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                # Fields which are only updated on the Variant.
         
     | 
| 
      
 54 
     | 
    
         
            +
                def variant_only_fields
         
     | 
| 
      
 55 
     | 
    
         
            +
                  @variant_only_fields ||= %w[track_inventory].freeze
         
     | 
| 
       51 
56 
     | 
    
         
             
                end
         
     | 
| 
       52 
57 
     | 
    
         | 
| 
       53 
58 
     | 
    
         
             
                # Spree Client
         
     | 
| 
         @@ -180,18 +185,23 @@ module Jekyll 
     | 
|
| 
       180 
185 
     | 
    
         
             
                # @return [true,false]
         
     | 
| 
       181 
186 
     | 
    
         
             
                def update(product, variant)
         
     | 
| 
       182 
187 
     | 
    
         
             
                  # Sync local changes to Spree
         
     | 
| 
       183 
     | 
    
         
            -
                  remote_product = product 
     | 
| 
       184 
     | 
    
         
            -
                  variant_comparison = variant 
     | 
| 
      
 188 
     | 
    
         
            +
                  remote_product = attributes_from product, product_fields
         
     | 
| 
      
 189 
     | 
    
         
            +
                  variant_comparison = attributes_from variant, product_fields
         
     | 
| 
       185 
190 
     | 
    
         | 
| 
       186 
191 
     | 
    
         
             
                  # Only when they haven't changed, so we don't waste time sending a
         
     | 
| 
       187 
192 
     | 
    
         
             
                  # request.
         
     | 
| 
       188 
193 
     | 
    
         
             
                  unless remote_product.slice(*variant_comparison.keys) == variant_comparison
         
     | 
| 
       189 
194 
     | 
    
         
             
                    # XXX: We don't have the product ID with the variant but we have
         
     | 
| 
       190 
195 
     | 
    
         
             
                    # the slug.
         
     | 
| 
      
 196 
     | 
    
         
            +
                    remote_variant = { id: variant['id'] }.merge!(attributes_from product, variant_only_fields)
         
     | 
| 
      
 197 
     | 
    
         
            +
             
     | 
| 
      
 198 
     | 
    
         
            +
                    remote_product.delete_if do |k, v|
         
     | 
| 
      
 199 
     | 
    
         
            +
                      remote_variant.key? k
         
     | 
| 
      
 200 
     | 
    
         
            +
                    end
         
     | 
| 
      
 201 
     | 
    
         
            +
             
     | 
| 
       191 
202 
     | 
    
         
             
                    remote_product[:id] = variant['slug']
         
     | 
| 
       192 
     | 
    
         
            -
                    remote_product[:sku] = product.data[sku_field]
         
     | 
| 
       193 
203 
     | 
    
         | 
| 
       194 
     | 
    
         
            -
                    unless remote_products.update ** 
     | 
| 
      
 204 
     | 
    
         
            +
                    unless remote_products.update(**remote_product) && spree.variants.update(**remote_variant)
         
     | 
| 
       195 
205 
     | 
    
         
             
                      Jekyll.logger.error "Couldn't update #{product.data['title']}"
         
     | 
| 
       196 
206 
     | 
    
         
             
                      mark_with_error product, error_messages
         
     | 
| 
       197 
207 
     | 
    
         
             
                    end
         
     | 
| 
         @@ -218,21 +228,22 @@ module Jekyll 
     | 
|
| 
       218 
228 
     | 
    
         
             
                # @param [Jekyll::Document]
         
     | 
| 
       219 
229 
     | 
    
         
             
                # @return [true,false]
         
     | 
| 
       220 
230 
     | 
    
         
             
                def create(product)
         
     | 
| 
       221 
     | 
    
         
            -
                  new_product = product 
     | 
| 
       222 
     | 
    
         
            -
                  new_product[:sku] = product.data[sku_field]
         
     | 
| 
      
 231 
     | 
    
         
            +
                  new_product = attributes_from(product, create_fields)
         
     | 
| 
       223 
232 
     | 
    
         | 
| 
       224 
     | 
    
         
            -
                  unless remote_products.create 
     | 
| 
      
 233 
     | 
    
         
            +
                  unless remote_products.create(**new_product)
         
     | 
| 
       225 
234 
     | 
    
         
             
                    Jekyll.logger.error "Couldn't create #{product.data['title']}"
         
     | 
| 
       226 
235 
     | 
    
         
             
                    mark_with_error product, error_messages
         
     | 
| 
       227 
236 
     | 
    
         | 
| 
       228 
237 
     | 
    
         
             
                    return false
         
     | 
| 
       229 
238 
     | 
    
         
             
                  end
         
     | 
| 
       230 
239 
     | 
    
         | 
| 
       231 
     | 
    
         
            -
                   
     | 
| 
      
 240 
     | 
    
         
            +
                  variant_id = remote_products.response.dig('master', 'id')
         
     | 
| 
      
 241 
     | 
    
         
            +
                  spree.variants(id: variant_id).update(attributes_from(product, variant_only_fields))
         
     | 
| 
      
 242 
     | 
    
         
            +
                  product.data['variant_id'] = variant_id
         
     | 
| 
       232 
243 
     | 
    
         
             
                  product.data['in_stock'] = product.data['stock'].to_i > 0
         
     | 
| 
       233 
244 
     | 
    
         | 
| 
       234 
245 
     | 
    
         
             
                  # Add the initial stock
         
     | 
| 
       235 
     | 
    
         
            -
                  spree.stock_items(id:  
     | 
| 
      
 246 
     | 
    
         
            +
                  spree.stock_items(id: variant_id, stock_location_id: stock_location['id'])
         
     | 
| 
       236 
247 
     | 
    
         
             
                       .stock_movements.create(quantity: product.data['stock'])
         
     | 
| 
       237 
248 
     | 
    
         | 
| 
       238 
249 
     | 
    
         
             
                  true
         
     | 
| 
         @@ -246,5 +257,14 @@ module Jekyll 
     | 
|
| 
       246 
257 
     | 
    
         
             
                    end
         
     | 
| 
       247 
258 
     | 
    
         
             
                  end&.flatten
         
     | 
| 
       248 
259 
     | 
    
         
             
                end
         
     | 
| 
      
 260 
     | 
    
         
            +
             
     | 
| 
      
 261 
     | 
    
         
            +
                # Converts fields from a Jekyll::Document into an attribute Hash.
         
     | 
| 
      
 262 
     | 
    
         
            +
                #
         
     | 
| 
      
 263 
     | 
    
         
            +
                # @param [Jekyll::Document,Hash] Document or Hash to extract data from
         
     | 
| 
      
 264 
     | 
    
         
            +
                # @param [Array] An Array of field names
         
     | 
| 
      
 265 
     | 
    
         
            +
                # @return [Hash] A symbolized Hash of fields and values
         
     | 
| 
      
 266 
     | 
    
         
            +
                def attributes_from(document_or_hash, fields)
         
     | 
| 
      
 267 
     | 
    
         
            +
                  (document_or_hash.respond_to?(:data) ? document_or_hash.data : document_or_hash).slice(*fields).transform_keys(&:to_sym)
         
     | 
| 
      
 268 
     | 
    
         
            +
                end
         
     | 
| 
       249 
269 
     | 
    
         
             
              end
         
     | 
| 
       250 
270 
     | 
    
         
             
            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. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.17
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - f
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire:
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2021- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2021-08-04 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: spree-api-client
         
     |