-
Notifications
You must be signed in to change notification settings - Fork 5
/
stripe.rb
122 lines (99 loc) · 2.57 KB
/
stripe.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module Stripe
StripeError = Class.new(StandardError)
APIError = Class.new(StripeError)
OAuthError = Class.new(StripeError)
AuthenticationError = Class.new(StripeError)
APIConnectionError = Class.new(StripeError)
RateLimitError = Class.new(StripeError)
TimeoutError = Class.new(StripeError)
InvalidRequestError = Class.new(StripeError)
module Harness
module ClassMethods
def error_with(error)
@errors ||= []
len = @errors.length
@errors << error
yield
ensure
@errors.pop if @errors.try(:length).to_i > len
end
def slow_with(time_seconds)
@slow_times ||= []
len = @slow_times.length
@slow_times << time_seconds
yield
ensure
@slow_times.pop if @slow_times.try(:length).to_i > len
end
def retrieve_with(params)
@retrieve_params ||= []
len = @retrieve_params.length
@retrieve_params << params
yield
ensure
@retrieve_params.pop if @retrieve_params.try(:length).to_i > len
end
def create(params)
check_error!
check_slow!
new(params)
end
def retrieve(id)
check_error!
check_slow!
if @retrieve_params.try(:length).to_i > 0
new({id:}.merge(@retrieve_params.last))
else
raise Stripe::InvalidRequestError, "No such invoice: '#{id}'"
end
end
def check_slow!
sleep @slow_times.last if @slow_times.try(:length).to_i > 0
end
def check_error!
raise @errors.last if @errors.try(:length).to_i > 0
end
end
def update_attributes(params)
self.class.check_error!
self.class.check_slow!
assign_attributes(params)
end
end
class Invoice
extend Harness::ClassMethods
include Harness
attr_reader :customer
def initialize(params)
@id = params[:id]
assign_attributes(params)
end
def id
@id ||= SecureRandom.base36
end
private
def assign_attributes(params)
@customer = params[:customer]
end
end
class InvoiceItem
extend Harness::ClassMethods
include Harness
attr_reader :invoice
attr_reader :unit_amount_decimal
attr_reader :quantity
def initialize(params)
@id = params[:id]
assign_attributes(params)
end
def id
@id ||= SecureRandom.base36
end
private
def assign_attributes(params)
@invoice = params[:invoice]
@unit_amount_decimal = params[:unit_amount_decimal]
@quantity = params[:quantity]
end
end
end