Class: PayjpMock::RequestBuilder

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/payjp_mock/request_builder.rb

Constant Summary collapse

UnknownResource =
Class.new(StandardError)
UnknownOperation =
Class.new(StandardError)

Instance Method Summary collapse

Methods included from Util

#generate_fingerprint, #generate_resource_id

Constructor Details

#initialize(resource, operation, params, source, error, response) ⇒ RequestBuilder

Returns a new instance of RequestBuilder.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/payjp_mock/request_builder.rb', line 6

def initialize(resource, operation, params, source, error, response)
  @resource  = resource.is_a?(Hash) ? resource.symbolize_keys : resource.to_sym
  @operation = operation.to_sym
  @params    = params.symbolize_keys
  @source    = source.symbolize_keys

  @response = response ||
    case error&.to_sym
    when :card_error
      Response::Error::CardError.new
    when :invalid_request_error
      Response::Error::InvalidRequestError.new
    when :authentication_error
      Response::Error::AuthenticationError.new
    when :api_connection_error
      Response::Error::ApiConnectionError.new
    when :api_error
      Response::Error::ApiError.new
    end
end

Instance Method Details

#buildObject



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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/payjp_mock/request_builder.rb', line 27

def build
  method, path_pattern, success_resp =
    case @resource
    when :charge, :charges
      case @operation
      when :create
        [:post, '/charges', Response::Resource::Charge.new(@params)]
      when :retrieve
        [:get, '/charges/{id}', Response::Resource::Charge.new(@source)]
      when :save
        charge = Response::Resource::Charge.new(@source).set(@params)
        [:post, '/charges/{id}', charge]
      when :refund
        charge = Response::Resource::Charge.new(@source)
        charge
          .set(@params.except(:amount))
          .set(amount_refunded: @params[:amount] || charge.attributes[:amount], refunded: true)

        [:post, '/charges/{id}/refund', charge]
      when :reauth
        charge = Response::Resource::Charge.new(@source)
        charge.set(
          captured:    false,
          captured_at: nil,
          expiry_days: @params[:expiry_days] || 7
        )
        [:post, '/charges/{id}/reauth', charge]
      when :capture
        charge = Response::Resource::Charge.new(@source)
        charge.set(captured: true, captured_at: Time.now.to_i)
        charge.set(
          if @params[:amount]
            { amount_refunded: charge.attributes[:amount] - @params[:amount], refunded: true }
          else
            { amount_refunded: 0, refunded: false }
          end
        )
        [:post, '/charges/{id}/capture', charge]
      when :all
        list = Response::List.new('/charges') { Response::Resource::Charge.new(@source) }
        [:get, '/charges', list]
      else
        raise UnknownOperation, @operation
      end
    when :customer, :customers
      case @operation
      when :create
        [:post, '/customers', Response::Resource::Customer.new(@params)]
      when :retrieve
        [:get, '/customers/{id}', Response::Resource::Customer.new(@source)]
      when :save
        customer = Response::Resource::Customer.new(@source).set(@params)
        [:post, '/customers/{id}', customer]
      when :delete
        cus_id = generate_resource_id(Response::Resource::Customer::PREFIX)
        [:delete, '/customers/{id}', Response::Deleted.new(cus_id)]
      when :all
        list = Response::List.new('/customers') { Response::Resource::Customer.new(@source) }
        [:get, '/customers', list]
      else
        raise UnknownOperation, @operation
      end
    when { customer: :card }, { customer: :cards }, { customers: :card }, { customers: :cards }
      case @operation
      when :create
        [:post, '/customers/{customer_id}/cards', Response::Resource::Card.new(@params)]
      when :retrieve
        card = Response::Resource::Card.new(@source)
        [:get, '/customers/{customer_id}/cards/{id}', card]
      when :save
        card = Response::Resource::Card.new(@source).set(@params)
        [:post, '/customers/{customer_id}/cards/{id}', card]
      when :delete
        car_id = generate_resource_id(Response::Resource::Card::PREFIX)
        [:delete, '/customers/{customer_id}/cards/{id}', Response::Deleted.new(car_id)]
      when :all
        cus_id = generate_resource_id(Response::Resource::Customer::PREFIX)
        list = Response::List.new("/customres/#{cus_id}/cards") do
          Response::Resource::Card.new(@source)
        end
        [:get, '/customers/{customer_id}/cards', list]
      else
        raise UnknownOperation, @operation
      end
    when :plan, :plans
      case @operation
      when :create
        [:post, '/plans', Response::Resource::Plan.new(@params)]
      when :retrieve
        [:get, '/plans/{id}', Response::Resource::Plan.new(@source)]
      when :save
        plan = Response::Resource::Plan.new(@source).set(@params)
        [:post, '/plans/{id}', plan]
      when :delete
        pln_id = generate_resource_id(Response::Resource::Plan::PREFIX)
        [:delete, '/plans/{id}', Response::Deleted.new(pln_id)]
      when :all
        list = Response::List.new('/plans') { Response::Resource::Plan.new(@source) }
        [:get, '/plans', list]
      else
        raise UnknownOperation, @operation
      end
    when :subscription, :subscriptions
      case @operation
      when :create
        subscription =
          if @params[:trial_end] == 'now'
            warn('parameter `trial_end` cannot be set to `now` yet')
            Response::Resource::Subscription.new(@params.except(:trial_end))
          else
            Response::Resource::Subscription.new(@params)
          end
        [:post, '/subscriptions', subscription]
      when :retrieve
        [:get, '/subscriptions/{id}', Response::Resource::Subscription.new(@source)]
      when :save
        subscription = Response::Resource::Subscription.new(@source)
        subscription.set(
          if @params[:trial_end] == 'now'
            warn('parameter `trial_end` cannot be set to `now` yet')
            @params.except(:trial_end)
          else
            @params
          end
        )
        [:post, '/subscriptions/{id}', subscription]
      when :pause
        subscription = Response::Resource::Subscription.new(@source)
        subscription.set(status: 'paused', paused_at: Time.now.to_i)

        [:post, '/subscriptions/{id}/pause', subscription]
      when :resume
        subscription = Response::Resource::Subscription.new(@source)
        subscription.set(resumed_at: Time.now.to_i)

        case @params[:trial_end]
        when nil
          subscription.set(@params).set(status: 'active')
        when 'now'
          warn('parameter `trial_end` cannot be set to `now` yet')
          subscription.set(@params.except(:trial_end)).set(status: 'active')
        else
          subscription.set(@params)
        end

        [:post, '/subscriptions/{id}/resume', subscription]
      when :cancel
        subscription = Response::Resource::Subscription.new(@source)
        subscription.set(status: 'canceled', canceled_at: Time.now.to_i)

        [:post, '/subscriptions/{id}/cancel', subscription]
      when :delete
        sub_id = generate_resource_id(Response::Resource::Subscription::PREFIX)
        [:delete, '/subscriptions/{id}', Response::Deleted.new(sub_id)]
      when :all
        list = Response::List.new('/subscriptions') do
          Response::Resource::Subscription.new(@source)
        end
        [:get, '/subscriptions', list]
      else
        raise UnknownOperation, @operation
      end
    when :token, :tokens
      case @operation
      when :create
        [:post, '/tokens', Response::Resource::Token.new(@params)]
      when :retrieve
        [:get, '/tokens/{id}', Response::Resource::Token.new(@source)]
      else
        raise UnknownOperation, @operation
      end
    when :transfer, :transfers
      case @operation
      when :retrieve
        [:get, '/transfers/{id}', Response::Resource::Transfer.new(@source)]
      when :all
        list = Response::List.new('/transfers') { Response::Resource::Transfer.new(@source) }
        [:get, '/transfers', list]
      else
        raise UnknownOperation, @operation
      end
    when { transfer: :charge }, { transfer: :charges }, { transfers: :charge }, { transfers: :charges }
      case @operation
      when :all
        tr_id = generate_resource_id(Response::Resource::Transfer::PREFIX)
        list = Response::List.new("/transfers/#{tr_id}/charges") do
          Response::Resource::Charge.new(@source)
        end
        [:get, '/transfers/{transfer_id}/charges', list]
      else
        raise UnknownOperation, @operation
      end
    when :event, :events
      case @operation
      when :retrieve
        [:get, '/events/{id}', Response::Resource::Event.new(@source)]
      when :all
        list = Response::List.new('/events') { Response::Resource::Event.new(@source) }
        [:get, '/events', list]
      else
        raise UnknownOperation, @operation
      end
    when :account, :accounts
      case @operation
      when :retrieve
        [:get, '/accounts', Response::Resource::Account.new(@source)]
      else
        raise UnknownOperation, @operation
      end
    else
      raise UnknownResource, @resource
    end

  Request.new(method, path_pattern, @params, @response || success_resp)
end