stripe_event 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/integrallis/stripe_event.png?branch=master)](http://travis-ci.org/integrallis/stripe_event)
4
4
 
5
- stripe_event is built on the ActiveSupport::Notifications API. Incoming webhook requests are authenticated by retrieving the [event object](https://stripe.com/docs/api?lang=ruby#event_object) from Stripe. Define subscriber blocks to handle one, many, or all event types.
5
+ stripe_event is built on the ActiveSupport::Notifications API[[0]](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html). Incoming webhook requests are authenticated by retrieving the [event object](https://stripe.com/docs/api?lang=ruby#event_object) from Stripe[[1]](https://answers.stripe.com/questions/what-is-the-recommended-way-to-authenticate-a-webhook-callback). Define subscriber blocks to handle one, many, or all event types.
6
6
 
7
7
  ## Install
8
8
 
@@ -13,7 +13,7 @@ gem 'stripe_event'
13
13
 
14
14
  ```ruby
15
15
  # config/routes.rb
16
- mount StripeEvent::Engine => "/my-chosen-path" # provide a custom path
16
+ mount StripeEvent::Engine => '/my-chosen-path' # provide a custom path
17
17
  ```
18
18
 
19
19
  ## Usage
@@ -37,6 +37,23 @@ StripeEvent.setup do
37
37
  end
38
38
  ```
39
39
 
40
+ ## Configuration
41
+
42
+ If you have built an application that has multiple Stripe accounts--say, each of your customers has their own--you may want to define your own way of retrieving events from Stripe (e.g. perhaps you want to use the `user_id` parameter[[2]](https://stripe.com/docs/apps/getting-started#webhooks) from the top level to detect the customer for the event, then grab their specific API key). You can do this:
43
+
44
+ ```ruby
45
+ StripeEvent.event_retriever = Proc.new do |params|
46
+ secret_key = Account.find_by_stripe_user_id(params[:user_id]).secret_key
47
+ Stripe::Event.retrieve(params[:id], secret_key)
48
+ end
49
+ ```
50
+
51
+ During development it may be useful to skip retrieving the event from Stripe, and deal with the params hash directly. Just remember that the data has not been authenticated.
52
+
53
+ ```ruby
54
+ StripeEvent.event_retriever = Proc.new { |params| params }
55
+ ```
56
+
40
57
  ## Register webhook url with Stripe
41
58
 
42
59
  ![Setup webhook url](https://raw.github.com/integrallis/stripe_event/master/screenshots/dashboard-webhook.png "webhook setup")
@@ -1,7 +1,7 @@
1
1
  module StripeEvent
2
2
  class WebhookController < ActionController::Base
3
3
  def event
4
- event = Stripe::Event.retrieve(params[:id])
4
+ event = StripeEvent.event_retriever.call(params)
5
5
  StripeEvent.publish(event)
6
6
  head :ok
7
7
  rescue Stripe::StripeError
data/lib/stripe_event.rb CHANGED
@@ -1,37 +1,23 @@
1
1
  require "stripe"
2
2
  require "stripe_event/engine"
3
3
  require "stripe_event/subscriber"
4
+ require "stripe_event/publisher"
4
5
  require "stripe_event/types"
6
+ require "stripe_event/errors"
5
7
 
6
8
  module StripeEvent
9
+ mattr_accessor :event_retriever
10
+ self.event_retriever = Proc.new { |params| Stripe::Event.retrieve(params[:id]) }
11
+
7
12
  class << self
8
13
  alias_method :setup, :instance_eval
9
14
  end
10
15
 
11
- def self.registration(&block)
12
- warn "[Deprecation Warning] StripeEvent.registration is deprecated. Use StripeEvent.setup."
13
- instance_eval(&block)
14
- end
15
-
16
- def self.publish(event_obj)
17
- ActiveSupport::Notifications.instrument(event_obj.type, :event => event_obj)
16
+ def self.publish(event)
17
+ Publisher.new(event).instrument
18
18
  end
19
19
 
20
20
  def self.subscribe(*names, &block)
21
- Subscriber.new(*names, &block).register
22
- end
23
-
24
- def self.subscribers(name)
25
- ActiveSupport::Notifications.notifier.listeners_for(name)
26
- end
27
-
28
- def self.clear_subscribers!
29
- TYPE_LIST.each do |type|
30
- subscribers(type).each { |s| unsubscribe(s) }
31
- end
32
- end
33
-
34
- def self.unsubscribe(subscriber)
35
- ActiveSupport::Notifications.notifier.unsubscribe(subscriber)
21
+ Subscriber.new(*names).register(&block)
36
22
  end
37
23
  end
@@ -0,0 +1,4 @@
1
+ module StripeEvent
2
+ class StripeEventError < StandardError; end
3
+ class InvalidEventTypeError < StripeEventError; end
4
+ end
@@ -0,0 +1,13 @@
1
+ module StripeEvent
2
+ class Publisher < Struct.new(:event)
3
+ def instrument
4
+ ActiveSupport::Notifications.instrument(type, :event => event)
5
+ end
6
+
7
+ def type
8
+ event[:type].tap { |type|
9
+ raise InvalidEventTypeError.new("Event type was not present for: #{event}") if !type.present?
10
+ }
11
+ end
12
+ end
13
+ end
@@ -1,26 +1,25 @@
1
1
  module StripeEvent
2
2
  class Subscriber
3
- def initialize(*names, &block)
3
+ def initialize(*names)
4
4
  @names = names
5
- @block = block
6
5
  ensure_valid_types!
7
6
  end
8
7
 
9
- def register
8
+ def register(&block)
10
9
  ActiveSupport::Notifications.subscribe(pattern) do |*_, payload|
11
- @block.call(payload[:event])
10
+ block.call(payload[:event])
12
11
  end
13
12
  end
14
13
 
15
- private
16
-
17
14
  def pattern
18
15
  Regexp.union(@names.empty? ? TYPE_LIST : @names)
19
16
  end
20
17
 
18
+ private
19
+
21
20
  def ensure_valid_types!
22
21
  invalid_names = @names.select { |name| !TYPE_LIST.include?(name) }
23
- raise InvalidEventType.new(invalid_names) if invalid_names.any?
22
+ raise InvalidEventTypeError.new(invalid_names) if invalid_names.any?
24
23
  end
25
24
  end
26
25
  end
@@ -1,7 +1,7 @@
1
1
  module StripeEvent
2
- InvalidEventType = Class.new(StandardError)
3
-
4
2
  TYPE_LIST = [
3
+ 'account.updated',
4
+ 'account.application.deauthorized',
5
5
  'charge.succeeded',
6
6
  'charge.failed',
7
7
  'charge.refunded',
@@ -30,6 +30,7 @@ module StripeEvent
30
30
  'coupon.updated',
31
31
  'coupon.deleted',
32
32
  'transfer.created',
33
+ 'transfer.updated',
33
34
  'transfer.failed',
34
35
  'ping'
35
36
  ]
@@ -1,3 +1,3 @@
1
1
  module StripeEvent
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe_event
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-15 00:00:00.000000000 Z
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70103674405340 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70103674405340
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: stripe
27
- requirement: &70103674404620 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '1.6'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70103674404620
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.6'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rspec-rails
38
- requirement: &70103674420300 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '2.10'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70103674420300
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.10'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: webmock
49
- requirement: &70103674419640 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ~>
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '1.8'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70103674419640
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.8'
58
78
  description: Stripe webhook integration for Rails applications.
59
79
  email:
60
80
  - dwhalen@integrallis.com
@@ -65,6 +85,8 @@ files:
65
85
  - app/controllers/stripe_event/webhook_controller.rb
66
86
  - config/routes.rb
67
87
  - lib/stripe_event/engine.rb
88
+ - lib/stripe_event/errors.rb
89
+ - lib/stripe_event/publisher.rb
68
90
  - lib/stripe_event/subscriber.rb
69
91
  - lib/stripe_event/types.rb
70
92
  - lib/stripe_event/version.rb
@@ -92,9 +114,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
114
  version: '0'
93
115
  requirements: []
94
116
  rubyforge_project:
95
- rubygems_version: 1.8.15
117
+ rubygems_version: 1.8.21
96
118
  signing_key:
97
119
  specification_version: 3
98
120
  summary: Stripe webhook integration for Rails applications.
99
121
  test_files: []
100
- has_rdoc: