Module: WebPay::Mock::WebMockWrapper

Includes:
WebMock::API, FakeEntity, FakeError, Util
Defined in:
lib/webpay/mock/webmock_wrapper.rb

Instance Method Summary collapse

Methods included from FakeError

#bad_request, #card_error, #internal_server_error, #not_found, #unauthorized

Methods included from Util

#stringify_keys

Methods included from FakeEntity

#card_from, #charge_from, #customer_from, #fake_account, #fake_card, #fake_event, #fake_fingerprint, #fake_list, #recursion_from, #token_from

Instance Method Details

#webpay_stub(entity, action, options = {}) ⇒ Hash

Wrapper of “WebMock::API::stub_request()”. This provides simple stubbing. Use “stub_request()” and fake response generators to control details.



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
123
124
125
126
127
128
129
# File 'lib/webpay/mock/webmock_wrapper.rb', line 23

def webpay_stub(entity, action, options = {})
  base = options.delete(:base) || {}
  params = stringify_keys(options.delete(:params) || {})
  overrides = options.delete(:overrides) || {}
  id = params.delete('id') || options.delete(:id) || base['id']
  base_url = options[:base_url] || 'https://api.webpay.jp/v1'

  method, path, response =
    case entity.to_sym
    when :charge, :charges
      case action.to_sym
      when :create
        [:post, '/charges', charge_from(params, overrides)]
      when :retrieve
        [:get, '/charges/:id', charge_from({}, { id: id }.merge(overrides))]
      when :refund
        change = { 'id' => id, 'amount_refunded' => params['amount'] }
        change['refunded'] = !(params['amount'] && base['amount'] && params['amount'] != base['amount'])
        [:post, '/charges/:id/refund', charge_from({}, change.merge(overrides), base)]
      when :capture
        [:post, '/charges/:id/capture',
        charge_from({}, { 'id' => id, 'paid' => true, 'captured' => true }.merge(overrides), base)]
      when :all
        [:get, '/charges', fake_list('/charges', lambda { charge_from({}, overrides) })]
      end
    when :customer, :customers
      case action.to_sym
      when :create
        [:post, '/customers', customer_from(params, overrides)]
      when :retrieve
        [:get, '/customers/:id', customer_from({}, { id: id }.merge(overrides))]
      when :update
        [:post, '/customers/:id', customer_from(params, overrides, base)]
      when :delete
        [:delete, '/customers/:id', { 'id' => id, 'deleted' => true }]
      when :all
        [:get, '/customers', fake_list('/customers', lambda { customer_from({}, overrides) })]
      when :delete_active_card
        [:delete, '/customers/:id/active_card',customer_from({}, { id: id }.merge(overrides.merge(active_card: nil)))]
      end
    when :recursion, :recursions
      case action.to_sym
      when :create
        [:post, '/recursions', recursion_from(params, overrides)]
      when :retrieve
        [:get, '/recursions/:id', recursion_from({}, { id: id }.merge(overrides))]
      when :resume
        [:post, '/recursions/:id/resume', recursion_from({}, { id: id, status: 'active' }.merge(overrides), base)]
      when :delete
        [:delete, '/recursions/:id', { 'id' => id, 'deleted' => true }]
      when :all
        [:get, '/recursions', fake_list('/recursions', lambda { recursion_from({}, overrides) })]
      end
    when :token, :tokens
      case action.to_sym
      when :create
        [:post, '/tokens', token_from(params, overrides)]
      when :retrieve
        [:get, '/tokens/:id', token_from({}, { 'id' => id }.merge(overrides))]
      end
    when :event, :events
      case action.to_sym
      when :retrieve
        [:get, '/events/:id', fake_event({ 'id' => id }.merge(overrides))]
      when :all
        [:get, '/events', fake_list('/events', lambda { fake_event(overrides) })]
      end
    when :account
      case action.to_sym
      when :retrieve
        [:get, '/account', (overrides)]
      when :delete_data
        [:delete, '/account/data', { deleted: true }]
      end
    end

  unless path
    raise ArgumentError.new("Unknown entity or action is given to webpay_stub()")
  end

  if path.include?(':id')
    if id.nil? || id == ''
      raise ArgumentError.new(":id in parameters is required")
    end
    path = path.gsub(':id', id)
  end

  spec = options[:response] ||
    case options[:error]
    when :bad_request
      bad_request
    when :unauthorized
      unauthorized
    when :card_error
      card_error
    when :not_found
      not_found
    when :internal_server_error
      internal_server_error
    else # success
      { body: response.to_json }
    end

  stub_request(method, base_url + path).with(query: hash_including({})).with(body: params).to_return(spec)

  JSON.parse(spec[:body])
end