forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coupons.rb
35 lines (29 loc) · 1.3 KB
/
coupons.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
module StripeMock
module RequestHandlers
module Coupons
def Coupons.included(klass)
klass.add_handler 'post /v1/coupons', :new_coupon
klass.add_handler 'get /v1/coupons/(.*)', :get_coupon
klass.add_handler 'delete /v1/coupons/(.*)', :delete_coupon
klass.add_handler 'get /v1/coupons', :list_coupons
end
def new_coupon(route, method_url, params, headers)
params[:id] ||= new_id('coupon')
raise Stripe::InvalidRequestError.new('Missing required param: duration', 'coupon', http_status: 400) unless params[:duration]
raise Stripe::InvalidRequestError.new('You must pass currency when passing amount_off', 'coupon', http_status: 400) if params[:amount_off] && !params[:currency]
coupons[ params[:id] ] = Data.mock_coupon({amount_off: nil, percent_off:nil}.merge(params))
end
def get_coupon(route, method_url, params, headers)
route =~ method_url
assert_existence :coupon, $1, coupons[$1]
end
def delete_coupon(route, method_url, params, headers)
route =~ method_url
assert_existence :coupon, $1, coupons.delete($1)
end
def list_coupons(route, method_url, params, headers)
Data.mock_list_object(coupons.values, params)
end
end
end
end