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.



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

def initialize(port)
  @port = port

  DRb.start_service
  @pipe = DRbObject.new_with_uri "druby://localhost:#{port}"

  # Ensure client can connect to server
  timeout_wrap(5) { @pipe.ping }
  @state = 'ready'
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#cleanupObject



98
99
100
101
102
# File 'lib/stripe_mock/client.rb', line 98

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

#clear_server_dataObject



85
86
87
# File 'lib/stripe_mock/client.rb', line 85

def clear_server_data
  timeout_wrap { @pipe.clear_data }
end

#close!Object



93
94
95
96
# File 'lib/stripe_mock/client.rb', line 93

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

#destroy_resource(type, id) ⇒ Object



81
82
83
# File 'lib/stripe_mock/client.rb', line 81

def destroy_resource(type, id)
  timeout_wrap { @pipe.destroy_resource(type, id) }
end

#error_queueObject



37
38
39
# File 'lib/stripe_mock/client.rb', line 37

def error_queue
  timeout_wrap { @pipe.error_queue }
end

#generate_bank_token(recipient_params) ⇒ Object



57
58
59
# File 'lib/stripe_mock/client.rb', line 57

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

#generate_card_token(card_params) ⇒ Object



61
62
63
# File 'lib/stripe_mock/client.rb', line 61

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

#generate_webhook_event(event_data) ⇒ Object



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

def generate_webhook_event(event_data)
  timeout_wrap { Stripe::Util.symbolize_names @pipe.generate_webhook_event(event_data) }
end

#get_conversion_rateObject



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

def get_conversion_rate
  timeout_wrap { @pipe.get_data(:conversion_rate) }
end

#get_server_data(key) ⇒ Object



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

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: nil, params: {}, headers: {}) ⇒ Object



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

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

#server_debug?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/stripe_mock/client.rb', line 45

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

#server_global_id_prefixObject



53
54
55
# File 'lib/stripe_mock/client.rb', line 53

def server_global_id_prefix
  timeout_wrap { @pipe.global_id_prefix }
end

#set_account_balance(value) ⇒ Object



77
78
79
# File 'lib/stripe_mock/client.rb', line 77

def (value)
  timeout_wrap { @pipe.(value) }
end

#set_conversion_rate(value) ⇒ Object



73
74
75
# File 'lib/stripe_mock/client.rb', line 73

def set_conversion_rate(value)
  timeout_wrap { @pipe.set_conversion_rate(value) }
end

#set_server_debug(toggle) ⇒ Object



41
42
43
# File 'lib/stripe_mock/client.rb', line 41

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

#set_server_global_id_prefix(value) ⇒ Object



49
50
51
# File 'lib/stripe_mock/client.rb', line 49

def set_server_global_id_prefix(value)
  timeout_wrap { @pipe.set_global_id_prefix(value) }
end

#timeout_wrap(tries = 1) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/stripe_mock/client.rb', line 104

def timeout_wrap(tries=1)
  original_tries = tries
  begin
    raise ClosedClientConnectionError if @state == 'closed'
    yield
  rescue ClosedClientConnectionError
    raise
  rescue Errno::ECONNREFUSED, DRb::DRbConnError => e
    tries -= 1
    if tries > 0
      if tries == original_tries - 1
        print "Waiting for StripeMock Server.."
      else
        print '.'
      end
      sleep 1
      retry
    else
      raise StripeMock::ServerTimeoutError.new(e)
    end
  end
end

#upsert_stripe_object(object, attributes) ⇒ Object



89
90
91
# File 'lib/stripe_mock/client.rb', line 89

def upsert_stripe_object(object, attributes)
  timeout_wrap { @pipe.upsert_stripe_object(object, attributes) }
end