moip-assinaturas 0.4.1 → 0.4.2

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5675234fd2e6fbad69922aa1fb708e3dd4a14f53
4
- data.tar.gz: 582c71bf26d906ac31117c7e437c1ca4825afb9c
3
+ metadata.gz: faa9afb48076405fa3883702d884b88d038bc12c
4
+ data.tar.gz: 72a14cac72bb731d727923ccdb84a3698f70cbb2
5
5
  SHA512:
6
- metadata.gz: 2f146429cef1ba2543b2962bc1c311b7422603a5fea640e10b6dd531ec07ceba7b4444f0f098143989d2882f29f8c00039bc0c432815335fb2327a3b807d008a
7
- data.tar.gz: 305c5a3845f7d85e078af1a0c42548954e42103267f1789629077b886355a0deefdbf7205622720612964076e836a428dcfe68e95510930e5f37a394da91dda9
6
+ metadata.gz: 1415ace8067d211cd38d82d312a21d301c22ef82cf340603ff79e4b00981642d5a51c2272941766bbab5e7815d0fe30f2ed747014faeffe9cbcbcad63f531fc0
7
+ data.tar.gz: a284e73ad19131edc2b2322d5dacb5b96f69501be1f8ee6ce45422986ba8aa8920ef2e6f07331f095a312de15978ebc62031b77b6c3ae630ce313ae440f21358
data/README.md CHANGED
@@ -202,7 +202,7 @@ A classe Webhooks foi desenvolvida para cobrir qualquer caso de envio do Moip. U
202
202
  # como eu costumo usar o rails então
203
203
  class WebhooksController < ApplicationController
204
204
  def webhooks
205
- Moip::Assinaturas::Webhooks.listen(request) do |hook|
205
+ resultado = Moip::Assinaturas::Webhooks.listen(request) do |hook|
206
206
 
207
207
  # quando o moip envia dado sobre a criação de um plano
208
208
  hook.on(:plan, :created) do
@@ -216,11 +216,24 @@ class WebhooksController < ApplicationController
216
216
  end
217
217
  end
218
218
 
219
+ # trata vários eventos de um model no mesmo hook
220
+ hook.on(:subscription, [:canceled, :suspended]) do |status|
221
+ deleta_assinatura(motivo: status)
222
+ end
223
+
219
224
  hook.on(:subscription, :created) do
220
225
  # Fazer algo
221
226
  end
227
+
228
+ # hook para capturar eventos que ainda não são explicitamente tratados
229
+ hook.missing do |model, event| do
230
+ Rails.logger.warn "Não encontrado hook para o modelo #{model} e evento #{event}"
231
+ false
232
+ end
222
233
  end
223
- render :text => "done ok"
234
+
235
+ render :text => "done ok" and return if resultado
236
+ render nothing: true, status: :bad_request
224
237
  end
225
238
  end
226
239
  ```
@@ -1,5 +1,5 @@
1
1
  module Moip
2
2
  module Assinaturas
3
- VERSION = "0.4.1"
3
+ VERSION = "0.4.2"
4
4
  end
5
5
  end
@@ -32,7 +32,6 @@ module Moip::Assinaturas
32
32
  end
33
33
 
34
34
  def on(model, on_events, &block)
35
-
36
35
  unless events[model]
37
36
  events[model] = {}
38
37
  end
@@ -42,12 +41,17 @@ module Moip::Assinaturas
42
41
  events[model][event] = []
43
42
  end
44
43
 
45
- events[model][event] << block
44
+ events[model][event] = block
46
45
  end
47
46
  end
48
47
 
48
+ def missing(&block)
49
+ events[:missing] = block
50
+ end
51
+
49
52
  def run
50
- events[model][event].each { |action| action.call } if (events[model] && events[model][event])
53
+ return events[model][event].call(event) if (events[model] && events[model][event])
54
+ events[:missing].call(model, event) if events[:missing]
51
55
  end
52
56
  end
53
- end
57
+ end
@@ -62,13 +62,26 @@ describe Moip::Assinaturas::Webhooks do
62
62
 
63
63
  it "should adding this block to events" do
64
64
  hook.on(model, event_1, &block)
65
- hook.events[model][event_1].should eq([block])
65
+ hook.events[model][event_1].should eq(block)
66
66
  end
67
67
 
68
68
  it "should receive an array of events and add this block to events" do
69
69
  hook.on(model, [event_1, event_2], &block)
70
- hook.events[model][event_1].should eq([block])
71
- hook.events[model][event_2].should eq([block])
70
+ hook.events[model][event_1].should eq(block)
71
+ hook.events[model][event_2].should eq(block)
72
+ end
73
+ end
74
+
75
+ describe '#missing(&block)' do
76
+ let!(:model) { 'model' }
77
+ let!(:missing) { 'missing_event' }
78
+ let!(:block) { lambda { 'missing_event' } }
79
+
80
+ subject(:hook) { Moip::Assinaturas::Webhooks.build(params) }
81
+
82
+ it "should adding this block to events" do
83
+ hook.missing(&block)
84
+ hook.events[:missing].should eq(block)
72
85
  end
73
86
  end
74
87
 
@@ -76,15 +89,57 @@ describe Moip::Assinaturas::Webhooks do
76
89
  let!(:model) { :model }
77
90
  let!(:event) { :event }
78
91
  let!(:block) { lambda { } }
92
+ let!(:params2) do
93
+ params2 = params.clone
94
+ params2['event'] = 'model.event2'
95
+ params2
96
+ end
79
97
 
80
98
  subject(:hook) { Moip::Assinaturas::Webhooks.build(params) }
81
99
 
82
100
  it 'should call block once' do
83
101
  hook.on(model, event, &block)
84
102
 
85
- block.should_receive(:call).once
103
+ block.should_receive(:call).once.with(:event)
86
104
 
87
105
  hook.run
88
106
  end
107
+
108
+ it 'should call twice when hook accepted 2 events' do
109
+ hook2 = Moip::Assinaturas::Webhooks.build(params2)
110
+
111
+ hook.on(model, [event, :event2], &block)
112
+ hook2.on(model, [event, :event2], &block)
113
+
114
+ block.should_receive(:call).with(:event)
115
+ block.should_receive(:call).with(:event2)
116
+
117
+ hook.run
118
+ hook2.run
119
+ end
120
+
121
+ it "should return the hook's return" do
122
+ hook.on(model, event) { |event| "#{event}_called" }
123
+
124
+ expect(hook.run).to eql("event_called")
125
+ end
126
+
127
+ describe 'missing hook' do
128
+ it 'should run the missing event if no model is found' do
129
+ params['event'] = 'not exist.event'
130
+ hook = Moip::Assinaturas::Webhooks.build(params)
131
+ hook.missing { |model, event| "#{model}_#{event}" }
132
+
133
+ expect(hook.run).to eql("not exist_event")
134
+ end
135
+
136
+ it 'should run the missing event if no event is found' do
137
+ params['event'] = 'model.not exist'
138
+ hook = Moip::Assinaturas::Webhooks.build(params)
139
+ hook.missing { |model, event| "#{model}_#{event}" }
140
+
141
+ expect(hook.run).to eql("model_not exist")
142
+ end
143
+ end
89
144
  end
90
- end
145
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moip-assinaturas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Warlley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-25 00:00:00.000000000 Z
11
+ date: 2014-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec