Class: StripeMock::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stripe_mock/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
# File 'lib/stripe_mock/client.rb', line 6

def initialize(port)
  @port = port
  @pipe = Jimson::Client.new("http://0.0.0.0:#{port}")
  # Ensure client can connect to server
  timeout_wrap { @pipe.ping }
  @state = 'ready'
  @error_queue = ErrorQueue.new
end

Instance Attribute Details

#error_queueObject (readonly)

Returns the value of attribute error_queue.



4
5
6
# File 'lib/stripe_mock/client.rb', line 4

def error_queue
  @error_queue
end

#portObject (readonly)

Returns the value of attribute port.



4
5
6
# File 'lib/stripe_mock/client.rb', line 4

def port
  @port
end

#stateObject (readonly)

Returns the value of attribute state.



4
5
6
# File 'lib/stripe_mock/client.rb', line 4

def state
  @state
end

Instance Method Details

#cleanupObject



68
69
70
71
72
# File 'lib/stripe_mock/client.rb', line 68

def cleanup
  return if @state == 'closed'
  set_server_debug(false)
  @state = 'closed'
end

#clear_server_dataObject



59
60
61
# File 'lib/stripe_mock/client.rb', line 59

def clear_server_data
  timeout_wrap { @pipe.clear_data }
end

#close!Object



63
64
65
66
# File 'lib/stripe_mock/client.rb', line 63

def close!
  self.cleanup
  StripeMock.stop_client(:clear_server_data => false)
end

#generate_card_token(card_params) ⇒ Object



55
56
57
# File 'lib/stripe_mock/client.rb', line 55

def generate_card_token(card_params)
  timeout_wrap { @pipe.generate_card_token(card_params) }
end

#generate_recipient_token(recipient_params) ⇒ Object



51
52
53
# File 'lib/stripe_mock/client.rb', line 51

def generate_recipient_token(recipient_params)
  timeout_wrap { @pipe.generate_recipient_token(recipient_params) }
end

#get_server_data(key) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/stripe_mock/client.rb', line 26

def get_server_data(key)
  timeout_wrap {
    # Massage the data make this behave the same as the local StripeMock.start
    result = {}
    @pipe.get_data(key).each {|k,v| result[k] = Stripe::Util.symbolize_names(v) }
    result
  }
end

#mock_request(method, url, api_key, params = {}, headers = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/stripe_mock/client.rb', line 15

def mock_request(method, url, api_key, params={}, headers={})
  timeout_wrap do
    @pipe.mock_request(method, url, api_key, params, headers).tap {|result|
      response, api_key = result
      if response.is_a?(Hash) && response['error_raised'] == 'invalid_request'
        raise Stripe::InvalidRequestError.new(*response['error_params'])
      end
    }
  end
end

#server_debug?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/stripe_mock/client.rb', line 39

def server_debug?
  timeout_wrap { @pipe.debug? }
end

#server_strict?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/stripe_mock/client.rb', line 47

def server_strict?
  timeout_wrap { @pipe.strict? }
end

#set_server_debug(toggle) ⇒ Object



35
36
37
# File 'lib/stripe_mock/client.rb', line 35

def set_server_debug(toggle)
  timeout_wrap { @pipe.set_debug(toggle) }
end

#set_server_strict(toggle) ⇒ Object



43
44
45
# File 'lib/stripe_mock/client.rb', line 43

def set_server_strict(toggle)
  timeout_wrap { @pipe.set_strict(toggle) }
end

#timeout_wrapObject



74
75
76
77
78
79
80
81
# File 'lib/stripe_mock/client.rb', line 74

def timeout_wrap
  raise ClosedClientConnectionError if @state == 'closed'
  yield
rescue ClosedClientConnectionError
  raise
rescue Errno::ECONNREFUSED => e
  raise StripeMock::ServerTimeoutError.new(e)
end