Skip to content

Commit b86a17e

Browse files
committed
update request handlers to return list objects for list requests
1 parent d08a424 commit b86a17e

17 files changed

+53
-23
lines changed

lib/stripe_mock.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
require 'stripe'
66

77
require 'stripe_mock/version'
8-
require 'stripe_mock/data'
9-
require 'stripe_mock/list'
108
require 'stripe_mock/util'
119
require 'stripe_mock/error_queue'
1210

11+
require 'stripe_mock/data'
12+
require 'stripe_mock/data/list'
13+
1314
require 'stripe_mock/errors/stripe_mock_error'
1415
require 'stripe_mock/errors/unsupported_request_error'
1516
require 'stripe_mock/errors/uninitialized_instance_error'

lib/stripe_mock/data.rb

+5
Original file line numberDiff line numberDiff line change
@@ -440,5 +440,10 @@ def self.mock_delete_discount_response
440440
:id => "di_test_coupon"
441441
}
442442
end
443+
444+
def self.mock_list_object(data, params = {})
445+
list = StripeMock::Data::List.new(data, params)
446+
list.to_h
447+
end
443448
end
444449
end

lib/stripe_mock/list.rb renamed to lib/stripe_mock/data/list.rb

+14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ def has_more?
2222
(offset + limit) < data.size
2323
end
2424

25+
def method_missing(method_name, *args, &block)
26+
hash = to_hash
27+
28+
if hash.keys.include?(method_name)
29+
hash[method_name]
30+
else
31+
super
32+
end
33+
end
34+
35+
def respond_to?(method_name, priv = false)
36+
to_hash.keys.include?(method_name) || super
37+
end
38+
2539
private
2640

2741
# TODO: REFACTOR

lib/stripe_mock/request_handlers/cards.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ def create_card(route, method_url, params, headers)
2222
def retrieve_cards(route, method_url, params, headers)
2323
route =~ method_url
2424
customer = assert_existence :customer, $1, customers[$1]
25-
2625
cards = customer[:cards]
27-
cards[:count] = cards[:data].length
28-
cards
26+
27+
Data.mock_list_object(cards[:data])
2928
end
3029

3130
def retrieve_card(route, method_url, params, headers)

lib/stripe_mock/request_handlers/charges.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ def new_charge(route, method_url, params, headers)
3232

3333
def get_charges(route, method_url, params, headers)
3434
params[:offset] ||= 0
35-
params[:count] ||= 10
35+
params[:limit] ||= 10
3636

3737
clone = charges.clone
3838

3939
if params[:customer]
4040
clone.delete_if { |k,v| v[:customer] != params[:customer] }
4141
end
4242

43-
clone.values[params[:offset], params[:count]]
43+
Data.mock_list_object(clone.values, params)
4444
end
4545

4646
def get_charge(route, method_url, params, headers)

lib/stripe_mock/request_handlers/coupons.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def delete_coupon(route, method_url, params, headers)
2525
end
2626

2727
def list_coupons(route, method_url, params, headers)
28-
coupons.values
28+
Data.mock_list_object(coupons.values, params)
2929
end
3030

3131
end

lib/stripe_mock/request_handlers/customers.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def get_customer(route, method_url, params, headers)
6969
end
7070

7171
def list_customers(route, method_url, params, headers)
72-
customers.values
72+
Data.mock_list_object(customers.values, params)
7373
end
7474

7575
end

lib/stripe_mock/request_handlers/invoice_items.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def delete_invoice_item(route, method_url, params, headers)
3232
end
3333

3434
def list_invoice_items(route, method_url, params, headers)
35-
invoice_items.values
35+
Data.mock_list_object(invoice_items.values, params)
3636
end
3737

3838
def get_invoice_item(route, method_url, params, headers)

lib/stripe_mock/request_handlers/invoices.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ def update_invoice(route, method_url, params, headers)
2626

2727
def list_invoices(route, method_url, params, headers)
2828
params[:offset] ||= 0
29-
params[:count] ||= 10
29+
params[:limit] ||= 10
3030

3131
result = invoices.clone
3232

3333
if params[:customer]
3434
result.delete_if { |k,v| v[:customer] != params[:customer] }
3535
end
3636

37-
result.values[params[:offset], params[:count]]
37+
Data.mock_list_object(result.values, params)
3838
end
3939

4040
def get_invoice(route, method_url, params, headers)

lib/stripe_mock/request_handlers/plans.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def delete_plan(route, method_url, params, headers)
3232
end
3333

3434
def list_plans(route, method_url, params, headers)
35-
plans.values
35+
Data.mock_list_object(plans.values)
3636
end
3737

3838
end

spec/list_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@
4747
)
4848
end
4949

50+
it "delegates other methods to hash keys" do
51+
list = StripeMock::Data::List.new([double, double, double])
52+
53+
expect(list).to respond_to(:data)
54+
expect(list.data).to be_kind_of(Array)
55+
expect(list.object).to eq("list")
56+
expect(list.has_more).to eq(false)
57+
expect(list.url).to eq("/v1/doubles")
58+
expect { list.foobar }.to raise_error(NoMethodError)
59+
end
60+
5061
context "with a limit" do
5162
it "accepts a limit which is reflected in the data returned" do
5263
list = StripeMock::Data::List.new([double] * 25)

spec/shared_stripe_examples/charge_examples.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@
120120
end
121121

122122
it "stores charges for a customer in memory" do
123-
expect(@customer.charges.map(&:id)).to eq([@charge.id])
123+
expect(@customer.charges.data.map(&:id)).to eq([@charge.id])
124124
end
125125

126126
it "stores all charges in memory" do
127-
expect(Stripe::Charge.all.map(&:id)).to eq([@charge.id, @charge2.id])
127+
expect(Stripe::Charge.all.data.map(&:id)).to eq([@charge.id, @charge2.id])
128128
end
129129

130130
it "defaults count to 10 charges" do
131131
11.times { Stripe::Charge.create }
132-
expect(Stripe::Charge.all.count).to eq(10)
132+
expect(Stripe::Charge.all.data.count).to eq(10)
133133
end
134134

135-
context "when passing count" do
135+
context "when passing limit" do
136136
it "gets that many charges" do
137-
expect(Stripe::Charge.all(count: 1).count).to eq(1)
137+
expect(Stripe::Charge.all(limit: 1).count).to eq(1)
138138
end
139139
end
140140
end

spec/shared_stripe_examples/coupon_examples.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
Stripe::Coupon.create({ id: 'Coupon Two', amount_off: 3000 })
8989

9090
all = Stripe::Coupon.all
91-
expect(all.length).to eq(2)
91+
expect(all.count).to eq(2)
9292
expect(all.map &:id).to include('Coupon One', 'Coupon Two')
9393
expect(all.map &:amount_off).to include(1500, 3000)
9494
end

spec/shared_stripe_examples/customer_examples.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def gen_card_tk
197197
Stripe::Customer.create({ email: '[email protected]' })
198198

199199
all = Stripe::Customer.all
200-
expect(all.length).to eq(2)
200+
expect(all.count).to eq(2)
201201
expect(all.map &:email).to include('[email protected]', '[email protected]')
202202
end
203203

spec/shared_stripe_examples/invoice_examples.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
context "when passing count" do
6464
it "gets that many invoices" do
65-
expect(Stripe::Invoice.all(count: 1).count).to eq(1)
65+
expect(Stripe::Invoice.all(limit: 1).count).to eq(1)
6666
end
6767
end
6868
end

spec/shared_stripe_examples/invoice_item_examples.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
it "retrieves all invoice items" do
4242
all = Stripe::InvoiceItem.all
43-
expect(all.length).to eq(2)
43+
expect(all.count).to eq(2)
4444
expect(all.map &:amount).to include(1075, 1540)
4545
end
4646
end

spec/shared_stripe_examples/plan_examples.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
stripe_helper.create_plan(id: 'Plan Two', amount: 98765)
9797

9898
all = Stripe::Plan.all
99-
expect(all.length).to eq(2)
99+
expect(all.count).to eq(2)
100100
expect(all.map &:id).to include('Plan One', 'Plan Two')
101101
expect(all.map &:amount).to include(54321, 98765)
102102
end

0 commit comments

Comments
 (0)