shopifydev 0.0.22 → 0.0.23

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDAzOGMwZTczZTY0NTNmNDQ3NDAyMzZkOTZhNmI2MDNjMzcwOWQxZg==
4
+ OTQxYmJjYTU1Zjg1N2UzM2I4NGQ4YmM5ODk0ODY5Nzk0MmY2YTk4NA==
5
5
  data.tar.gz: !binary |-
6
- ZTYzZjQ0MGEzMmNmZjk0YTgyNDI5N2I1N2JlZGU5MzU0MWY5N2U2YQ==
6
+ MjcxMTgxOWE2ZDNlOTcxOWRhMjYxN2I1YmMwYWYyY2I1MzA4YWFiMA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ODYwNTAwZDQxODQ1YTFhMTQ4ZjRkY2MyNGQ5MzZkYzJjYjNmMWU1ZmNlZDQz
10
- ZDhkYTFkYTcwNDZiN2UwY2VlZTM0NzJjOGU1YzE2M2U0YWRmZGU2NWY2ZGVj
11
- YzMzNDBkYzEzNDkzOTdiZGJjNTY2ZDk5NjFkZjViMDNkMTZiNGU=
9
+ MDY3MjhhOTJlYjA2MzI5YzM3YzQyZDhjZjc4YTQzNGZkODFkZWRkYWZjNzhi
10
+ ZWY5MDdkNzgzOWIwNjhlZGI2NjY5ZTIzOTFhNWZkOGExNGYxOGMzYTAxNjc5
11
+ M2QzOWJhZTExY2FkMTk0YWM0MGUyNTY3YTlkODNlY2M5YzViNmM=
12
12
  data.tar.gz: !binary |-
13
- YmY3YTUzNDlkYzAwZGFiOWUzNmUxNjg5OTg4MDM2OTMxNmUwNTkwYjQzNTM5
14
- NDMwNzdkNGM1MzI4NDQ2Y2Y2ZDE0OWM5YTRkNWFmMTUxY2Y1YTE3YWQwMTM5
15
- NGM5ODBiYWJkZTkwM2ZlNDhiYmNlNmNkODdjN2FhODQ2MDg0ODU=
13
+ NWJkMTc1NWIxMjc0NDFjYWUwNTA0ZTg3MTAwMzE5YjMzMDAyZmI2YjQxZGEx
14
+ NjkzNTg5NDE0MzMwN2VmODgwMjA0MzY4OGZiOTQ3NTZlNGFmZGMwNjZhYzdi
15
+ NWNlZDM4YjBhMDQ2ODgxNDMzNGRlOWExNGRjYmNlMTA0NTJhZTg=
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Shopifydev
4
+ class ProductCacheGenerator < Rails::Generators::Base
5
+ desc "Creates a ProductCache model and a sidekiq worker to populate it."
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def generate_product_cache
9
+ generate 'model ProductCache shop:references shopify_product_id:integer shopify_title:string shopify_handle:string shopify_product_type:string shopify_vendor:string shopify_updated_at:datetime'
10
+ end
11
+
12
+ def generate_worker
13
+ template "sidekiq_status_initializer.rb", 'config/initializers/sidekiq_status_initializer.rb'
14
+ template "product_cache_updater.rb", "app/workers/product_cache_updater.rb"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ class CreateCacheJobs < ActiveRecord::Migration
2
+ def change
3
+ create_table :cache_jobs do
4
+ t.string :jid, index: true
5
+ t.integer :new_ids, array: true, default: []
6
+ t.integer :updated_ids, array: true, default: []
7
+ t.string :state, default: 'requested'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,57 @@
1
+ class ProductCacheUpdater
2
+ include Sidekiq::Worker
3
+ include Sidekiq::Status::Worker
4
+
5
+ def perform(shop_id, throttle=0)
6
+ shop = ::Shop.find(shop_id)
7
+
8
+ num = 0
9
+ done = 0
10
+ created_ids = []
11
+ updated_ids = []
12
+ loop do
13
+ last_update = ProductCache.maximum(:shopify_updated_at)
14
+ last_update ||= 10.years.ago
15
+ products = []
16
+
17
+ shop.with_shopify_session do
18
+ products = ShopifyAPI::Product.find(:all,
19
+ params: {limit: false, updated_at_min: last_update.to_s(:db)}
20
+ )
21
+ end
22
+
23
+ break if products.length < 1
24
+
25
+ num += products.length
26
+ at done, num
27
+ products.each_with_index do |product, ix|
28
+ product_cache = nil
29
+ begin
30
+ product_cache = ProductCache.find_or_create_by(shopify_product_id: product.id) do |p|
31
+ p.shop = shop
32
+ end
33
+ created_ids << product.id
34
+ rescue ActiveRecord::RecordNotUnique
35
+ retry
36
+ end
37
+ product_cache.shopify_title = product.title
38
+ product_cache.shopify_handle = product.handle
39
+ product_cache.shopify_product_type = product.product_type
40
+ product_cache.shopify_vendor = product.vendor
41
+ product_cache.shopify_updated_at = product.updated_at
42
+
43
+ if product_cache.changed?
44
+ updated_ids << product.id unless created_ids.include?(product.id)
45
+ product_cache.save!
46
+ end
47
+ store created_ids: created_ids.join(',')
48
+ store updated_ids: updated_ids.join(',')
49
+ done += 1
50
+ at done, num
51
+ sleep throttle
52
+ end
53
+ end
54
+ store created_ids: created_ids.join(',')
55
+ store updated_ids: updated_ids.join(',')
56
+ end
57
+ end
@@ -0,0 +1,17 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq-status'
3
+
4
+ Sidekiq.configure_client do |config|
5
+ config.client_middleware do |chain|
6
+ chain.add Sidekiq::Status::ClientMiddleware
7
+ end
8
+ end
9
+
10
+ Sidekiq.configure_server do |config|
11
+ config.server_middleware do |chain|
12
+ chain.add Sidekiq::Status::ServerMiddleware, expiration: 30.minutes # default
13
+ end
14
+ config.client_middleware do |chain|
15
+ chain.add Sidekiq::Status::ClientMiddleware
16
+ end
17
+ end
@@ -105,5 +105,19 @@ module Shopifydev
105
105
  out
106
106
  end
107
107
  end
108
+
109
+ def product(opts={})
110
+ num = opts.delete(:num) || 1
111
+ verbose = opts.delete(:verbose)
112
+ defaults = {
113
+
114
+ }
115
+ 1.upto(num) do i
116
+ interpolated_opts = defaults.merge(opts).deep_dup
117
+ interpolated_opts.deep_merge!(interpolated_opts){|key,v1| v1.gsub('###INDEX###', i)}
118
+ product = ::ShopifyAPI::Product.create(interpolated_opts)
119
+ puts "created #{product}" if verbose
120
+ end
121
+ end
108
122
  end
109
123
  end
@@ -1,3 +1,3 @@
1
1
  module Shopifydev
2
- VERSION = "0.0.22"
2
+ VERSION = "0.0.23"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopifydev
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Johnston
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-04 00:00:00.000000000 Z
11
+ date: 2014-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -244,8 +244,12 @@ files:
244
244
  - Rakefile
245
245
  - bin/shopify_console
246
246
  - bin/shopifydev
247
+ - lib/rails/generators/shopifydev/product_cache_generator.rb
247
248
  - lib/rails/generators/shopifydev/shops_generator.rb
249
+ - lib/rails/generators/shopifydev/templates/cache_job_migration.rb
250
+ - lib/rails/generators/shopifydev/templates/product_cache_updater.rb
248
251
  - lib/rails/generators/shopifydev/templates/shops.rake
252
+ - lib/rails/generators/shopifydev/templates/sidekiq_status_initializer.rb
249
253
  - lib/rails/tasks/shops.rake
250
254
  - lib/shopifydev.rb
251
255
  - lib/shopifydev/asset.rb