class StripeMock::Client

Attributes

port[R]
state[R]

Public Class Methods

new(port) click to toggle source
# 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

Public Instance Methods

cleanup() click to toggle source
# File lib/stripe_mock/client.rb, line 97
def cleanup
  return if @state == 'closed'
  set_server_debug(false)
  @state = 'closed'
end
clear_server_data() click to toggle source
# File lib/stripe_mock/client.rb, line 84
def clear_server_data
  timeout_wrap { @pipe.clear_data }
end
close!() click to toggle source
# File lib/stripe_mock/client.rb, line 92
def close!
  self.cleanup
  StripeMock.stop_client(:clear_server_data => false)
end
destroy_resource(type, id) click to toggle source
# File lib/stripe_mock/client.rb, line 80
def destroy_resource(type, id)
  timeout_wrap { @pipe.destroy_resource(type, id) }
end
error_queue() click to toggle source
# File lib/stripe_mock/client.rb, line 36
def error_queue
  timeout_wrap { @pipe.error_queue }
end
generate_bank_token(recipient_params) click to toggle source
# File lib/stripe_mock/client.rb, line 56
def generate_bank_token(recipient_params)
  timeout_wrap { @pipe.generate_bank_token(recipient_params) }
end
generate_card_token(card_params) click to toggle source
# File lib/stripe_mock/client.rb, line 60
def generate_card_token(card_params)
  timeout_wrap { @pipe.generate_card_token(card_params) }
end
generate_webhook_event(event_data) click to toggle source
# File lib/stripe_mock/client.rb, line 64
def generate_webhook_event(event_data)
  timeout_wrap { Stripe::Util.symbolize_names @pipe.generate_webhook_event(event_data) }
end
get_conversion_rate() click to toggle source
# File lib/stripe_mock/client.rb, line 68
def get_conversion_rate
  timeout_wrap { @pipe.get_data(:conversion_rate) }
end
get_server_data(key) click to toggle source
# File lib/stripe_mock/client.rb, line 27
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: {}) click to toggle source
# 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'
        raise Stripe::InvalidRequestError.new(*response[:error_params])
      end
    }
  end
end
server_debug?() click to toggle source
# File lib/stripe_mock/client.rb, line 44
def server_debug?
  timeout_wrap { @pipe.debug? }
end
server_global_id_prefix() click to toggle source
# File lib/stripe_mock/client.rb, line 52
def server_global_id_prefix
  timeout_wrap { @pipe.global_id_prefix }
end
set_account_balance(value) click to toggle source
# File lib/stripe_mock/client.rb, line 76
def set_account_balance(value)
  timeout_wrap { @pipe.set_account_balance(value) }
end
set_conversion_rate(value) click to toggle source
# File lib/stripe_mock/client.rb, line 72
def set_conversion_rate(value)
  timeout_wrap { @pipe.set_conversion_rate(value) }
end
set_server_debug(toggle) click to toggle source
# File lib/stripe_mock/client.rb, line 40
def set_server_debug(toggle)
  timeout_wrap { @pipe.set_debug(toggle) }
end
set_server_global_id_prefix(value) click to toggle source
# File lib/stripe_mock/client.rb, line 48
def set_server_global_id_prefix(value)
  timeout_wrap { @pipe.set_global_id_prefix(value) }
end
timeout_wrap(tries=1) { || ... } click to toggle source
# File lib/stripe_mock/client.rb, line 103
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) click to toggle source
# File lib/stripe_mock/client.rb, line 88
def upsert_stripe_object(object, attributes)
  timeout_wrap { @pipe.upsert_stripe_object(object, attributes) }
end