shopqi-app-webhook 0.1.4 → 0.1.5

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.
data/README.md CHANGED
@@ -26,6 +26,6 @@
26
26
  ## 启动
27
27
 
28
28
  $ rails s
29
- $ QUEUE=* bundle exec rake resque:work
29
+ $ script/delayed_job start
30
30
 
31
31
  访问 [http://localhost:3000](http://localhost:3000)
data/app/models/shop.rb CHANGED
@@ -3,6 +3,6 @@ class Shop
3
3
  after_create :add_webhook
4
4
 
5
5
  def add_webhook
6
- Resque.enqueue(WebhookWorker, self.id)
6
+ Delayed::Job.enqueue WebhookJob.new(self.id)
7
7
  end
8
8
  end
@@ -7,6 +7,7 @@ class ShopQiAppWebhookGenerator < Rails::Generators::Base
7
7
 
8
8
  def copy_files
9
9
  directory 'app'
10
+ directory 'script'
10
11
  end
11
12
 
12
13
  def add_routes
@@ -16,11 +17,10 @@ class ShopQiAppWebhookGenerator < Rails::Generators::Base
16
17
  def update_files
17
18
  prepend_to_file "app/models/shop.rb", "require \"\#{ShopQiAppWebhook::Engine.models_dir}/shop\"\n"
18
19
  insert_into_file "app/models/shop.rb", "\s\shas_many :orders\n", after: /class Shop( < ActiveRecord::Base){0,1}\n/
19
- insert_into_file "config/app_secret_config.yml", "\s\s\s\shost: 'lvh.me:3000'\n", after: /defaults:(.|\n|\s)+?domain:\n/
20
- insert_into_file "config/app_secret_config.yml", "\s\s\s\shost: 'example.shopqiapp.com'\n", after: /production:(.|\n|\s)+?domain:\n/
21
20
  end
22
21
 
23
22
  def install_migration
23
+ migration_template 'db/migrate/create_delayed_jobs.rb', 'db/migrate/create_delayed_jobs.rb'
24
24
  migration_template 'db/migrate/create_orders.rb', 'db/migrate/create_orders.rb'
25
25
  end
26
26
 
@@ -5,6 +5,6 @@ Step 1.
5
5
 
6
6
  Step 2.
7
7
  $ rails server
8
- $ QUEUE=* bundle exec rake resque:work
8
+ $ script/delayed_job start
9
9
 
10
10
  ===============================================================================
@@ -1,16 +1,16 @@
1
1
  class WebhookController < ShopQiAppWebhook::WebhookController
2
2
 
3
3
  def orders_fulfilled
4
- attrs = JSON(request.body.read)['order']
4
+ order = JSON(request.body.read)['order']
5
5
  shop.orders.where(order_id: order_id).first_or_create!({
6
- name: attrs['name'],
7
- financial_status_name: attrs['financial_status_name'],
8
- financial_status: attrs['financial_status'],
9
- fulfillment_status: attrs['fulfillment_status'],
10
- fulfillment_status_name: attrs['fulfillment_status_name'],
11
- order_id: attrs['id'],
12
- total_price: attrs['total_price'],
13
- created_at: attrs['created_at']
6
+ name: order['name'],
7
+ financial_status_name: order['financial_status_name'],
8
+ financial_status: order['financial_status'],
9
+ fulfillment_status: order['fulfillment_status'],
10
+ fulfillment_status_name: order['fulfillment_status_name'],
11
+ order_id: order['id'],
12
+ total_price: order['total_price'],
13
+ created_at: order['created_at']
14
14
  })
15
15
  render nothing: true
16
16
  end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ class WebhookJob < Struct.new(:shop_id)
3
+
4
+ def perform
5
+ Shop.find(shop_id).use_shopkit
6
+ Shopkit.create_webhook event: 'orders/fulfilled', callback_url: "http://#{SecretSetting.domain.host}/webhook"
7
+ end
8
+
9
+ end
@@ -3,6 +3,6 @@ class Order < ActiveRecord::Base
3
3
  attr_accessible :name, :financial_status, :financial_status_name, :fulfillment_status, :fulfillment_status_name, :order_id, :total_price, :created_at
4
4
 
5
5
  def admin_url
6
- "#{SecretSetting.shopqi_domain.protocol}#{shop.shopqi_domain}/admin/orders/#{order_id}"
6
+ "#{shop.admin_url}/orders/#{order_id}"
7
7
  end
8
8
  end
@@ -0,0 +1,22 @@
1
+ class CreateDelayedJobs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :delayed_jobs, :force => true do |table|
4
+ table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
5
+ table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
6
+ table.text :handler # YAML-encoded string of the object that will do work
7
+ table.text :last_error # reason for last failure (See Note below)
8
+ table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
9
+ table.datetime :locked_at # Set when a client is working on this object
10
+ table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
11
+ table.string :locked_by # Who is working on this object (if locked)
12
+ table.string :queue # The name of the queue this job is in
13
+ table.timestamps
14
+ end
15
+
16
+ add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
17
+ end
18
+
19
+ def self.down
20
+ drop_table :delayed_jobs
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
4
+ require 'delayed/command'
5
+ Delayed::Command.new(ARGV).daemonize
@@ -1,4 +1,5 @@
1
1
  require "shopqi_app_webhook/engine"
2
+ require "delayed_job_active_record"
2
3
 
3
4
  module ShopQiAppWebhook
4
5
 
@@ -17,11 +17,5 @@ module ShopQiAppWebhook
17
17
  end
18
18
  end
19
19
 
20
- initializer "shopqi_app_webhook.resque" do
21
- require 'resque/server'
22
- current_app_name = ::Rails.application.class.to_s.split("::").first
23
- Resque.redis.namespace = "resque:#{current_app_name}"
24
- end
25
-
26
20
  end
27
21
  end
@@ -4,7 +4,6 @@ module ShopQiAppWebhook
4
4
  module Helper
5
5
  def use_shopqi_webhook(options = {}, &block)
6
6
  self.post '/webhook', to: 'webhook#orders_fulfilled'#, format: :json
7
- #self.mount Resque::Server.new, :at => "/resque"
8
7
  end
9
8
  end
10
9
 
@@ -1,3 +1,3 @@
1
1
  module ShopQiAppWebhook
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -2,6 +2,3 @@
2
2
  # task :shopqi-app-webhook do
3
3
  # # Task goes here
4
4
  # end
5
-
6
- task "resque:setup" => :environment
7
- require 'resque/tasks'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopqi-app-webhook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-02 00:00:00.000000000 Z
12
+ date: 2012-08-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -44,13 +44,13 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: 0.2.5
46
46
  - !ruby/object:Gem::Dependency
47
- name: resque
47
+ name: delayed_job_active_record
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 1.21.0
53
+ version: 0.3.2
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 1.21.0
61
+ version: 0.3.2
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: sqlite3
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -91,22 +91,6 @@ dependencies:
91
91
  - - ~>
92
92
  - !ruby/object:Gem::Version
93
93
  version: 2.10.1
94
- - !ruby/object:Gem::Dependency
95
- name: resque_spec
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ! '>='
100
- - !ruby/object:Gem::Version
101
- version: '0'
102
- type: :development
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
94
  - !ruby/object:Gem::Dependency
111
95
  name: factory_girl_rails
112
96
  requirement: !ruby/object:Gem::Requirement
@@ -152,12 +136,14 @@ files:
152
136
  - app/models/shop.rb
153
137
  - app/assets/javascripts/shopqi_app_webhook/application.js
154
138
  - app/assets/stylesheets/shopqi_app_webhook/application.css
139
+ - lib/generators/shopqi_app_webhook/templates/db/migrate/create_delayed_jobs.rb
155
140
  - lib/generators/shopqi_app_webhook/templates/db/migrate/create_orders.rb
156
- - lib/generators/shopqi_app_webhook/templates/app/jobs/webhook_worker.rb
141
+ - lib/generators/shopqi_app_webhook/templates/app/jobs/webhook_job.rb
157
142
  - lib/generators/shopqi_app_webhook/templates/app/controllers/webhook_controller.rb
158
143
  - lib/generators/shopqi_app_webhook/templates/app/controllers/home_controller.rb
159
144
  - lib/generators/shopqi_app_webhook/templates/app/models/order.rb
160
145
  - lib/generators/shopqi_app_webhook/templates/app/views/home/index.html.haml
146
+ - lib/generators/shopqi_app_webhook/templates/script/delayed_job
161
147
  - lib/generators/shopqi_app_webhook/templates/README
162
148
  - lib/generators/shopqi_app_webhook/shopqi_app_webhook_generator.rb
163
149
  - lib/generators/shopqi_app_webhook/USAGE
@@ -183,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
169
  version: '0'
184
170
  segments:
185
171
  - 0
186
- hash: 272106625
172
+ hash: 715442061
187
173
  required_rubygems_version: !ruby/object:Gem::Requirement
188
174
  none: false
189
175
  requirements:
@@ -192,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
178
  version: '0'
193
179
  segments:
194
180
  - 0
195
- hash: 272106625
181
+ hash: 715442061
196
182
  requirements: []
197
183
  rubyforge_project:
198
184
  rubygems_version: 1.8.24
@@ -1,12 +0,0 @@
1
- # encoding: utf-8
2
- module WebhookWorker
3
-
4
- @queue = "webhook_worker"
5
-
6
- def self.perform(shop_id)
7
- shop = Shop.find(shop_id)
8
- Shopkit.setup url: shop.shopqi_domain, access_token: shop.access_token
9
- Shopkit.create_webhook event: 'orders/fulfilled', callback_url: "http://#{SecretSetting.domain.host}/webhook"
10
- end
11
-
12
- end