stripe-ruby-mock 1.8.3.8 → 1.8.3.9

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
@@ -116,11 +116,10 @@ describe MyApp do
116
116
 
117
117
  after do
118
118
  StripeMock.stop_client
119
- #
120
119
  # Alternatively:
121
- #
122
- # @client.close!
123
- # StripeMock.stop_client(:clear_server_data => true)
120
+ # @client.close!
121
+ # -- Or --
122
+ # StripeMock.stop_client(:clear_server_data => true)
124
123
  end
125
124
  end
126
125
  ```
@@ -133,7 +132,6 @@ Here are some other neat things you can do with the client:
133
132
  ```ruby
134
133
  @client.state #=> 'ready'
135
134
 
136
- @client.set_server_debug(true)
137
135
  @client.get_server_data(:customers) # Also works for :charges, :plans, etc.
138
136
  @client.clear_server_data
139
137
 
@@ -196,22 +194,30 @@ Also, you can override values whenever you create any webhook event:
196
194
 
197
195
  ```ruby
198
196
  it "can override default webhook values" do
197
+ # NOTE: given hash values get merged directly into event.data.object
199
198
  event = StripeMock.mock_webhook_event('customer.created', {
200
- :data => {
201
- :object => {
202
- :id => 'cus_my_custom_value',
203
- :email => 'joe@example.com'
204
- }
205
- }
199
+ :id => 'cus_my_custom_value',
200
+ :email => 'joe@example.com'
206
201
  })
207
202
  # Alternatively:
208
- # event.data.object.id = 'cus_my_custome_value'
203
+ # event.data.object.id = 'cus_my_custom_value'
209
204
  # event.data.object.email = 'joe@example.com'
210
- expect(event.data.object.id).to eq('cus_my_custome_value')
205
+ expect(event.data.object.id).to eq('cus_my_custom_value')
211
206
  expect(event.data.object.email).to eq('joe@example.com')
212
207
  end
213
208
  ```
214
209
 
210
+ ## Debugging
211
+
212
+ To enable debug messages:
213
+
214
+ StripeMock.toggle_debug(true)
215
+
216
+ This will **only last for the session**; Once you call `StripeMock.stop` or `StripeMock.stop_client`,
217
+ debug will be toggled off.
218
+
219
+ If you always want debug to be on (it's quite verbose), you should put this in a `before` block.
220
+
215
221
  ## TODO
216
222
 
217
223
  * Cover all stripe urls/methods
data/lib/stripe_mock.rb CHANGED
@@ -23,6 +23,7 @@ require 'stripe_mock/api/client'
23
23
  require 'stripe_mock/api/server'
24
24
  require 'stripe_mock/api/errors'
25
25
  require 'stripe_mock/api/webhooks'
26
+ require 'stripe_mock/api/debug'
26
27
 
27
28
  require 'stripe_mock/request_handlers/charges.rb'
28
29
  require 'stripe_mock/request_handlers/customers.rb'
@@ -16,8 +16,8 @@ module StripeMock
16
16
  @state = 'ready'
17
17
 
18
18
  alias_stripe_method :request, @original_request_method
19
- @client.clear_data if opts[:clear_server_data] == true
20
- @client.close!
19
+ @client.clear_server_data if opts[:clear_server_data] == true
20
+ @client.cleanup
21
21
  @client = nil
22
22
  true
23
23
  end
@@ -0,0 +1,11 @@
1
+ module StripeMock
2
+
3
+ def self.toggle_debug(toggle)
4
+ if @state == 'local'
5
+ @instance.debug = toggle
6
+ elsif @state == 'remote'
7
+ @client.set_server_debug(toggle)
8
+ end
9
+ end
10
+
11
+ end
@@ -28,15 +28,25 @@ module StripeMock
28
28
  timeout_wrap { @pipe.set_debug(toggle) }
29
29
  end
30
30
 
31
+ def server_debug?
32
+ timeout_wrap { @pipe.debug? }
33
+ end
34
+
31
35
  def clear_server_data
32
- timeout_wrap { @pipe.clear }
36
+ timeout_wrap { @pipe.clear_data }
33
37
  end
34
38
 
35
39
  def close!
36
- @state = 'closed'
40
+ self.cleanup
37
41
  StripeMock.stop_client(:clear_server_data => false)
38
42
  end
39
43
 
44
+ def cleanup
45
+ return if @state == 'closed'
46
+ set_server_debug(false)
47
+ @state = 'closed'
48
+ end
49
+
40
50
  def timeout_wrap
41
51
  raise ClosedClientConnectionError if @state == 'closed'
42
52
  yield
@@ -50,13 +50,13 @@ module StripeMock
50
50
  handler = @@handlers.find {|h| method_url =~ h[:route] }
51
51
 
52
52
  if handler
53
- self.send(handler[:name], handler[:route], method_url, params, headers).tap {|json|
54
- puts "[StripeMock res] #{json}" if @debug == true
55
- }
53
+ res = self.send(handler[:name], handler[:route], method_url, params, headers)
54
+ puts "[StripeMock res] #{res}" if @debug == true
55
+ [res, api_key]
56
56
  else
57
57
  puts "WARNING: Unrecognized method + url: [#{method} #{url}]"
58
58
  puts " params: #{params}"
59
- {}
59
+ [{}, api_key]
60
60
  end
61
61
  end
62
62
 
@@ -8,6 +8,7 @@ module StripeMock
8
8
  klass.add_handler 'delete /v1/customers/(.*)/subscription', :cancel_subscription
9
9
  klass.add_handler 'post /v1/customers/(.*)', :update_customer
10
10
  klass.add_handler 'get /v1/customers/(.*)', :get_customer
11
+ klass.add_handler 'get /v1/customers', :list_customers
11
12
  end
12
13
 
13
14
  def new_customer(route, method_url, params, headers)
@@ -34,6 +35,10 @@ module StripeMock
34
35
  customers[$1] ||= Data.test_customer(:id => $1)
35
36
  end
36
37
 
38
+ def list_customers(route, method_url, params, headers)
39
+ customers.values
40
+ end
41
+
37
42
  end
38
43
  end
39
44
  end
@@ -5,6 +5,7 @@ module StripeMock
5
5
  def Plans.included(klass)
6
6
  klass.add_handler 'post /v1/plans', :new_plan
7
7
  klass.add_handler 'get /v1/plans/(.*)', :get_plan
8
+ klass.add_handler 'get /v1/plans', :list_plans
8
9
  end
9
10
 
10
11
  def new_plan(route, method_url, params, headers)
@@ -17,6 +18,10 @@ module StripeMock
17
18
  plans[$1] ||= Data.test_plan(:id => $1)
18
19
  end
19
20
 
21
+ def list_plans(route, method_url, params, headers)
22
+ plans.values
23
+ end
24
+
20
25
  end
21
26
  end
22
27
  end
@@ -36,6 +36,10 @@ module StripeMock
36
36
  @instance.debug = toggle
37
37
  end
38
38
 
39
+ def debug?
40
+ @instance.debug
41
+ end
42
+
39
43
  def ping; true; end
40
44
  end
41
45
 
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.8.3.8"
3
+ VERSION = "1.8.3.9"
4
4
  end
@@ -15,9 +15,26 @@ describe StripeMock::Instance do
15
15
  "id" => "str_abcde",
16
16
  :name => "String Plan"
17
17
  }
18
- res = StripeMock.instance.mock_request('post', '/v1/plans', 'api_key', string_params)
18
+ res, api_key = StripeMock.instance.mock_request('post', '/v1/plans', 'api_key', string_params)
19
19
  expect(res[:id]).to eq('str_abcde')
20
20
  expect(res[:name]).to eq('String Plan')
21
21
  end
22
22
 
23
+ it "can toggle debug" do
24
+ StripeMock.toggle_debug(true)
25
+ expect(StripeMock.instance.debug).to eq(true)
26
+ StripeMock.toggle_debug(false)
27
+ expect(StripeMock.instance.debug).to eq(false)
28
+ end
29
+
30
+ it "should toggle off debug when mock session ends" do
31
+ StripeMock.toggle_debug(true)
32
+
33
+ StripeMock.stop
34
+ expect(StripeMock.instance).to be_nil
35
+
36
+ StripeMock.start
37
+ expect(StripeMock.instance.debug).to eq(false)
38
+ end
39
+
23
40
  end
data/spec/server_spec.rb CHANGED
@@ -20,7 +20,7 @@ describe 'StripeMock Server' do
20
20
  @client = StripeMock.start_client
21
21
  end
22
22
 
23
- after { StripeMock.stop_client }
23
+ after { StripeMock.stop_client(:clear_server_data => true) }
24
24
 
25
25
 
26
26
  it "uses an RPC client for mock requests" do
@@ -54,15 +54,27 @@ describe 'StripeMock Server' do
54
54
 
55
55
 
56
56
  it "returns a response with symbolized hash keys" do
57
- response = StripeMock.redirect_to_mock_server('get', '/v1/plans/x', 'xxx')
57
+ response, api_key = StripeMock.redirect_to_mock_server('get', '/v1/plans/x', 'xxx')
58
58
  response.keys.each {|k| expect(k).to be_a(Symbol) }
59
59
  end
60
60
 
61
61
 
62
62
  it "can toggle debug" do
63
- StripeMock.client.set_server_debug(true)
64
- StripeMock.client.set_server_debug(false)
65
- StripeMock.client.set_server_debug(true)
63
+ StripeMock.toggle_debug(true)
64
+ expect(@client.server_debug?).to eq(true)
65
+ StripeMock.toggle_debug(false)
66
+ expect(@client.server_debug?).to eq(false)
67
+ end
68
+
69
+
70
+ it "should toggle off debug when mock session ends" do
71
+ StripeMock.toggle_debug(true)
72
+
73
+ StripeMock.stop_client
74
+ expect(StripeMock.client).to be_nil
75
+
76
+ StripeMock.start_client
77
+ expect(StripeMock.client.server_debug?).to eq(false)
66
78
  end
67
79
 
68
80
 
@@ -48,6 +48,15 @@ shared_examples 'Customer API' do
48
48
  expect(customer.description).to_not be_nil
49
49
  end
50
50
 
51
+ it "retrieves all customers" do
52
+ Stripe::Customer.create({ email: 'one@one.com' })
53
+ Stripe::Customer.create({ email: 'two@two.com' })
54
+
55
+ all = Stripe::Customer.all
56
+ expect(all.length).to eq(2)
57
+ all.map(&:email).should include('one@one.com', 'two@two.com')
58
+ end
59
+
51
60
  it "updates a stripe customer" do
52
61
  original = Stripe::Customer.retrieve("test_customer_update")
53
62
  email = original.email
@@ -68,4 +68,14 @@ shared_examples 'Plan API' do
68
68
  expect(plan.interval).to_not be_nil
69
69
  end
70
70
 
71
+ it "retrieves all plans" do
72
+ Stripe::Plan.create({ id: 'Plan One', amount: 54321 })
73
+ Stripe::Plan.create({ id: 'Plan Two', amount: 98765 })
74
+
75
+ all = Stripe::Plan.all
76
+ expect(all.length).to eq(2)
77
+ all.map(&:id).should include('Plan One', 'Plan Two')
78
+ all.map(&:amount).should include(54321, 98765)
79
+ end
80
+
71
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe-ruby-mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.3.8
4
+ version: 1.8.3.9
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-06-20 00:00:00.000000000 Z
12
+ date: 2013-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stripe
@@ -107,6 +107,7 @@ files:
107
107
  - bin/stripe-mock-server
108
108
  - lib/stripe_mock.rb
109
109
  - lib/stripe_mock/api/client.rb
110
+ - lib/stripe_mock/api/debug.rb
110
111
  - lib/stripe_mock/api/errors.rb
111
112
  - lib/stripe_mock/api/instance.rb
112
113
  - lib/stripe_mock/api/server.rb