Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add active filter to Data::List #679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/stripe_mock/data/list.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module StripeMock
module Data
class List
attr_reader :data, :limit, :offset, :starting_after, :ending_before
attr_reader :data, :limit, :offset, :starting_after, :ending_before, :active

def initialize(data, options = {})
@data = Array(data.clone)
@limit = [[options[:limit] || 10, 100].min, 1].max # restrict @limit to 1..100
@starting_after = options[:starting_after]
@ending_before = options[:ending_before]
@active = options[:active]
if @data.first.is_a?(Hash) && @data.first[:created]
@data.sort_by! { |x| x[:created] }
@data.reverse!
Expand Down Expand Up @@ -53,14 +54,21 @@ def offset
(index || raise("No such object id: #{starting_after}")) + 1
when ending_before
index = data.index { |datum| datum[:id] == ending_before }
(index || raise("No such object id: #{ending_before}")) - 1
(index || raise("No such object id: #{ending_before}")) - 1
else
0
end
end

def data_page
data[offset, limit]
filtered_data[offset, limit]
end

def filtered_data
filtered_data = data
filtered_data = filtered_data.select { |d| d[:active] == active } unless active.nil?

filtered_data
end

def object_types
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/request_handlers/plans.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def delete_plan(route, method_url, params, headers)

def list_plans(route, method_url, params, headers)
limit = params[:limit] ? params[:limit] : 10
Data.mock_list_object(plans.values.first(limit), limit: limit)
Data.mock_list_object(plans.values.first(limit), params.merge!(limit: limit))
end

end
Expand Down
15 changes: 15 additions & 0 deletions spec/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@
end
end

context "active filter" do
it "accepts an active param which filters out data accordingly" do
product = Stripe::Product.create(id: "prod_123", name: "My Beautiful Product", type: "service")

plan_attributes = { product: product.id, interval: "month", currency: "usd", amount: 500 }
plan_a = Stripe::Plan.create(plan_attributes)
plan_b = Stripe::Plan.create(**plan_attributes, active: false)

list = StripeMock::Data::List.new([plan_a, plan_b], active: true)

expect(list.active).to eq(true)
expect(list.to_h[:data].count).to eq(1)
end
end

context "pagination" do
it "has a has_more field when it has more" do
list = StripeMock::Data::List.new(
Expand Down