forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.rb
48 lines (41 loc) · 1.47 KB
/
events.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module StripeMock
module RequestHandlers
module Events
def Events.included(klass)
klass.add_handler 'get /v1/events/(.*)', :retrieve_event
klass.add_handler 'get /v1/events', :list_events
end
def retrieve_event(route, method_url, params, headers)
route =~ method_url
assert_existence :event, $1, events[$1]
end
def list_events(route, method_url, params, headers)
values = filter_by_created(events.values, params: params)
Data.mock_list_object(values, params)
end
private
def filter_by_created(event_list, params:)
if params[:created].nil?
return event_list
end
if params[:created].is_a?(Hash)
if params[:created][:gt]
event_list = event_list.select { |event| event[:created] > params[:created][:gt].to_i }
end
if params[:created][:gte]
event_list = event_list.select { |event| event[:created] >= params[:created][:gte].to_i }
end
if params[:created][:lt]
event_list = event_list.select { |event| event[:created] < params[:created][:lt].to_i }
end
if params[:created][:lte]
event_list = event_list.select { |event| event[:created] <= params[:created][:lte].to_i }
end
else
event_list = event_list.select { |event| event[:created] == params[:created].to_i }
end
event_list
end
end
end
end