Skip to content

Commit

Permalink
Merge pull request #679 from rnmp/feature/add_active_filter
Browse files Browse the repository at this point in the history
Add active filter to Data::List
  • Loading branch information
alexmamonchik committed Dec 22, 2019
2 parents 5e42f81 + 64e614d commit 56bfdbf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit 56bfdbf

Please sign in to comment.