moip-assinaturas 0.0.3 → 0.1.0

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Essa gem permite utilizar a API do Moip Assinaturas.
4
4
 
5
- O Moip Assinaturas permite que você faça cobranças de forma automática, no valor e intervalo que escolher por meio da criação de planos.
5
+ O Moip Assinaturas permite que você faça cobranças de forma automática, no valor e intervalo que escolher por meio da criação de planos.
6
6
 
7
7
  [http://site.moip.com.br/assinaturas/](http://site.moip.com.br/assinaturas)
8
8
 
@@ -167,6 +167,38 @@ Obter detalhes de um pagamento:
167
167
  Moip::Assinaturas::Invoice.details(payment_id)
168
168
  ```
169
169
 
170
+ ## Webhooks
171
+
172
+ A classe Webhooks foi desenvolvida para cobrir qualquer caso de envio do Moip. Um exemplo de como ela é utilizada.
173
+
174
+ ```ruby
175
+ # como eu costumo usar o rails então
176
+ class WebhooksController < ApplicationController
177
+ def webhooks
178
+ Moip::Assinaturas::Webhooks.listen(request) do |hook|
179
+
180
+ # quando o moip envia dado sobre a criação de um plano
181
+ hook.on(:plan, :created) do
182
+ # Fazer algo
183
+ end
184
+
185
+ hook.on(:payment, :status_updated) do
186
+ # quando o pagamento do meu cliente está confirmado
187
+ if hook.resource['status']['code'] == 4
188
+ # Fazer algo
189
+ end
190
+ end
191
+
192
+ hook.on(:subscription, :created) do
193
+ # Fazer algo
194
+ end
195
+ end
196
+ render :text => "done ok"
197
+ end
198
+ end
199
+ ```
200
+ A ideia da arquitetura da classe Webhooks foi baseada na gem - [https://github.com/xdougx/api-moip-assinaturas](https://github.com/xdougx/api-moip-assinaturas) - substituindo os objetos daquela gem por hashs
201
+
170
202
  ## Contribuindo
171
203
 
172
204
  1. Fork it
@@ -45,6 +45,10 @@ module Moip::Assinaturas
45
45
  peform_action!(:get, "/customers/#{code}", { headers: { 'Content-Type' => 'application/json' } })
46
46
  end
47
47
 
48
+ def update_credit_card(customer_code, credit_card)
49
+ peform_action!(:put, "/customers/#{customer_code}/billing_infos", { body: credit_card.to_json, headers: { 'Content-Type' => 'application/json' } })
50
+ end
51
+
48
52
  def create_subscription(subscription, new_customer)
49
53
  peform_action!(:post, "/subscriptions?new_customer=#{new_customer}", { body: subscription.to_json, headers: { 'Content-Type' => 'application/json' } })
50
54
  end
@@ -54,6 +54,28 @@ module Moip::Assinaturas
54
54
  end
55
55
  end
56
56
 
57
+ def update_credit_card(customer_code, credit_card)
58
+ response = Moip::Assinaturas::Client.update_credit_card(customer_code, credit_card)
59
+ hash = JSON.load(response.body).with_indifferent_access
60
+
61
+ case response.code
62
+ when 200
63
+ return {
64
+ success: true,
65
+ message: hash[:message]
66
+ }
67
+ when 400
68
+ return {
69
+ success: false,
70
+ message: hash[:message],
71
+ errors: hash[:errors]
72
+ }
73
+ else
74
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
75
+ end
76
+
77
+ end
78
+
57
79
  end
58
80
  end
59
81
  end
@@ -1,5 +1,5 @@
1
1
  module Moip
2
2
  module Assinaturas
3
- VERSION = "0.0.3"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,50 @@
1
+ module Moip::Assinaturas
2
+ class Webhooks
3
+ attr_accessor :model, :event, :date, :env, :resource, :events
4
+
5
+ class << self
6
+ def build(json)
7
+ object = new
8
+ object.model = get_model(json['event'])
9
+ object.event = get_event(json['event'])
10
+ object.events = {}
11
+ object.date = json['date']
12
+ object.env = json['env']
13
+ object.resource = json['resource']
14
+
15
+ object
16
+ end
17
+
18
+ def listen(params, &block)
19
+ hook = build(params)
20
+ yield hook
21
+ hook.run
22
+ end
23
+
24
+ private
25
+ def get_model(event)
26
+ event.split(".")[0].to_sym
27
+ end
28
+
29
+ def get_event(event)
30
+ event.split(".")[1].to_sym
31
+ end
32
+ end
33
+
34
+ def on(model, event, &block)
35
+ unless events[model]
36
+ events[model] = {}
37
+ end
38
+
39
+ unless events[model][event]
40
+ events[model][event] = []
41
+ end
42
+
43
+ events[model][event] << block
44
+ end
45
+
46
+ def run
47
+ events[model][event].each { |action| action.call } if (events[model] && events[model][event])
48
+ end
49
+ end
50
+ end
@@ -12,6 +12,7 @@ module Moip
12
12
  autoload :Subscription, 'moip-assinaturas/subscription'
13
13
  autoload :Invoice, 'moip-assinaturas/invoice'
14
14
  autoload :Payment, 'moip-assinaturas/payment'
15
+ autoload :Webhooks, 'moip-assinaturas/webhooks'
15
16
  autoload :Client, 'moip-assinaturas/client'
16
17
 
17
18
  mattr_accessor :sandbox
@@ -0,0 +1 @@
1
+ { "message": "Dados alterados com sucesso" }
@@ -54,6 +54,13 @@ describe Moip::Assinaturas::Customer do
54
54
  body: File.join(File.dirname(__FILE__), '..', 'fixtures', 'details_customer.json'),
55
55
  status: [200, 'OK']
56
56
  )
57
+
58
+ FakeWeb.register_uri(
59
+ :put,
60
+ "https://TOKEN:KEY@api.moip.com.br/assinaturas/v1/customers/18/billing_infos",
61
+ body: File.join(File.dirname(__FILE__), '..', 'fixtures', 'update_credit_card.json'),
62
+ status: [200, 'OK']
63
+ )
57
64
  end
58
65
 
59
66
  it "should create a new customer" do
@@ -73,4 +80,17 @@ describe Moip::Assinaturas::Customer do
73
80
  request[:customer][:code].should == '18'
74
81
  end
75
82
 
83
+ it "should update the customer card info" do
84
+ request = Moip::Assinaturas::Customer.update_credit_card(18, {
85
+ credit_card: {
86
+ holder_name: 'Novo nome',
87
+ number: '5555666677778884',
88
+ expiration_month: '04',
89
+ expiration_year: '15'
90
+ }
91
+ })
92
+
93
+ request[:success].should be_true
94
+ end
95
+
76
96
  end
@@ -0,0 +1,83 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+ require 'json'
4
+
5
+ describe Moip::Assinaturas::Webhooks do
6
+
7
+ let!(:params) do
8
+ {
9
+ 'event' => 'model.event',
10
+ 'date' => '28/12/2012 15:38:46',
11
+ 'env' => 'generic_env',
12
+ 'resource' => {
13
+ 'test' => 'test generic resource'
14
+ }
15
+ }
16
+ end
17
+
18
+ describe '.listen(params, &block)' do
19
+ let!(:block) { lambda { |hook| } }
20
+ let!(:hook) { Moip::Assinaturas::Webhooks.build(params) }
21
+
22
+ before(:each) do
23
+ Moip::Assinaturas::Webhooks.stub(:build) { hook }
24
+ end
25
+
26
+ it 'should call build' do
27
+ Moip::Assinaturas::Webhooks.should_receive(:build).with(params)
28
+ Moip::Assinaturas::Webhooks.listen(params, &block)
29
+ end
30
+
31
+ it 'should yield block' do
32
+ Moip::Assinaturas::Webhooks.should_receive(:listen).and_yield(block)
33
+ Moip::Assinaturas::Webhooks.listen(params, &block)
34
+ end
35
+
36
+ it 'should call run' do
37
+ hook.should_receive(:run).once
38
+ Moip::Assinaturas::Webhooks.listen(params, &block)
39
+ end
40
+ end
41
+
42
+ describe '.build(params)' do
43
+ subject(:hook) { Moip::Assinaturas::Webhooks.build(params) }
44
+
45
+ its(:model) { should eq(:model) }
46
+ its(:event) { should eq(:event) }
47
+ its(:events) { should eq({}) }
48
+
49
+ its(:date) { should eq('28/12/2012 15:38:46') }
50
+
51
+ its(:env) { should eq('generic_env') }
52
+ its(:resource) { should eq({ 'test' => 'test generic resource' }) }
53
+ end
54
+
55
+ describe '#on(model, event, &block)' do
56
+ let!(:model) { 'model' }
57
+ let!(:event) { 'event' }
58
+ let!(:block) { lambda { } }
59
+
60
+ subject(:hook) { Moip::Assinaturas::Webhooks.build(params) }
61
+
62
+ it "should adding this block to events" do
63
+ hook.on(model, event, &block)
64
+ hook.events[model][event].should eq([block])
65
+ end
66
+ end
67
+
68
+ describe '#run' do
69
+ let!(:model) { :model }
70
+ let!(:event) { :event }
71
+ let!(:block) { lambda { } }
72
+
73
+ subject(:hook) { Moip::Assinaturas::Webhooks.build(params) }
74
+
75
+ it 'should call block once' do
76
+ hook.on(model, event, &block)
77
+
78
+ block.should_receive(:call).once
79
+
80
+ hook.run
81
+ end
82
+ end
83
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moip-assinaturas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
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: 2013-05-25 00:00:00.000000000 Z
12
+ date: 2013-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -160,6 +160,7 @@ files:
160
160
  - lib/moip-assinaturas/plan.rb
161
161
  - lib/moip-assinaturas/subscription.rb
162
162
  - lib/moip-assinaturas/version.rb
163
+ - lib/moip-assinaturas/webhooks.rb
163
164
  - lib/rails/generators/moip_assinaturas/install_generator.rb
164
165
  - lib/rails/generators/templates/moip_assinaturas.rb
165
166
  - moip-assinaturas.gemspec
@@ -176,11 +177,13 @@ files:
176
177
  - spec/fixtures/list_payments.json
177
178
  - spec/fixtures/list_plans.json
178
179
  - spec/fixtures/list_subscriptions.json
180
+ - spec/fixtures/update_credit_card.json
179
181
  - spec/moip-assinaturas/customer_spec.rb
180
182
  - spec/moip-assinaturas/invoice_spec.rb
181
183
  - spec/moip-assinaturas/payment_spec.rb
182
184
  - spec/moip-assinaturas/plan_spec.rb
183
185
  - spec/moip-assinaturas/subscription_spec.rb
186
+ - spec/moip-assinaturas/webhooks_spec.rb
184
187
  - spec/moip_assinaturas_spec.rb
185
188
  - spec/spec_helper.rb
186
189
  homepage: https://github.com/ibody/moip-assinaturas
@@ -203,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
206
  version: '0'
204
207
  requirements: []
205
208
  rubyforge_project:
206
- rubygems_version: 1.8.24
209
+ rubygems_version: 1.8.25
207
210
  signing_key:
208
211
  specification_version: 3
209
212
  summary: Ruby Gem para uso do serviço de assinaturas do Moip
@@ -221,10 +224,12 @@ test_files:
221
224
  - spec/fixtures/list_payments.json
222
225
  - spec/fixtures/list_plans.json
223
226
  - spec/fixtures/list_subscriptions.json
227
+ - spec/fixtures/update_credit_card.json
224
228
  - spec/moip-assinaturas/customer_spec.rb
225
229
  - spec/moip-assinaturas/invoice_spec.rb
226
230
  - spec/moip-assinaturas/payment_spec.rb
227
231
  - spec/moip-assinaturas/plan_spec.rb
228
232
  - spec/moip-assinaturas/subscription_spec.rb
233
+ - spec/moip-assinaturas/webhooks_spec.rb
229
234
  - spec/moip_assinaturas_spec.rb
230
235
  - spec/spec_helper.rb