Skip to content

Commit 64e614d

Browse files
author
Rolando Murillo
committed
Add active filter to Data::List
1 parent 5e42f81 commit 64e614d

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

lib/stripe_mock/data/list.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module StripeMock
22
module Data
33
class List
4-
attr_reader :data, :limit, :offset, :starting_after, :ending_before
4+
attr_reader :data, :limit, :offset, :starting_after, :ending_before, :active
55

66
def initialize(data, options = {})
77
@data = Array(data.clone)
88
@limit = [[options[:limit] || 10, 100].min, 1].max # restrict @limit to 1..100
99
@starting_after = options[:starting_after]
1010
@ending_before = options[:ending_before]
11+
@active = options[:active]
1112
if @data.first.is_a?(Hash) && @data.first[:created]
1213
@data.sort_by! { |x| x[:created] }
1314
@data.reverse!
@@ -53,14 +54,21 @@ def offset
5354
(index || raise("No such object id: #{starting_after}")) + 1
5455
when ending_before
5556
index = data.index { |datum| datum[:id] == ending_before }
56-
(index || raise("No such object id: #{ending_before}")) - 1
57+
(index || raise("No such object id: #{ending_before}")) - 1
5758
else
5859
0
5960
end
6061
end
6162

6263
def data_page
63-
data[offset, limit]
64+
filtered_data[offset, limit]
65+
end
66+
67+
def filtered_data
68+
filtered_data = data
69+
filtered_data = filtered_data.select { |d| d[:active] == active } unless active.nil?
70+
71+
filtered_data
6472
end
6573

6674
def object_types

lib/stripe_mock/request_handlers/plans.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def delete_plan(route, method_url, params, headers)
3434

3535
def list_plans(route, method_url, params, headers)
3636
limit = params[:limit] ? params[:limit] : 10
37-
Data.mock_list_object(plans.values.first(limit), limit: limit)
37+
Data.mock_list_object(plans.values.first(limit), params.merge!(limit: limit))
3838
end
3939

4040
end

spec/list_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@
109109
end
110110
end
111111

112+
context "active filter" do
113+
it "accepts an active param which filters out data accordingly" do
114+
product = Stripe::Product.create(id: "prod_123", name: "My Beautiful Product", type: "service")
115+
116+
plan_attributes = { product: product.id, interval: "month", currency: "usd", amount: 500 }
117+
plan_a = Stripe::Plan.create(plan_attributes)
118+
plan_b = Stripe::Plan.create(**plan_attributes, active: false)
119+
120+
list = StripeMock::Data::List.new([plan_a, plan_b], active: true)
121+
122+
expect(list.active).to eq(true)
123+
expect(list.to_h[:data].count).to eq(1)
124+
end
125+
end
126+
112127
context "pagination" do
113128
it "has a has_more field when it has more" do
114129
list = StripeMock::Data::List.new(

0 commit comments

Comments
 (0)