Module: StripeMock

Defined in:
lib/stripe_mock.rb,
lib/stripe_mock/data.rb,
lib/stripe_mock/util.rb,
lib/stripe_mock/client.rb,
lib/stripe_mock/server.rb,
lib/stripe_mock/version.rb,
lib/stripe_mock/api/live.rb,
lib/stripe_mock/instance.rb,
lib/stripe_mock/api/debug.rb,
lib/stripe_mock/data/list.rb,
lib/stripe_mock/api/client.rb,
lib/stripe_mock/api/errors.rb,
lib/stripe_mock/api/server.rb,
lib/stripe_mock/error_queue.rb,
lib/stripe_mock/api/instance.rb,
lib/stripe_mock/api/webhooks.rb,
lib/stripe_mock/api/bank_tokens.rb,
lib/stripe_mock/api/card_tokens.rb,
lib/stripe_mock/api/test_helpers.rb,
lib/stripe_mock/api/global_id_prefix.rb,
lib/stripe_mock/test_strategies/base.rb,
lib/stripe_mock/test_strategies/live.rb,
lib/stripe_mock/test_strategies/mock.rb,
lib/stripe_mock/request_handlers/cards.rb,
lib/stripe_mock/request_handlers/plans.rb,
lib/stripe_mock/request_handlers/events.rb,
lib/stripe_mock/request_handlers/tokens.rb,
lib/stripe_mock/errors/stripe_mock_error.rb,
lib/stripe_mock/request_handlers/charges.rb,
lib/stripe_mock/request_handlers/coupons.rb,
lib/stripe_mock/request_handlers/sources.rb,
lib/stripe_mock/request_handlers/invoices.rb,
lib/stripe_mock/request_handlers/customers.rb,
lib/stripe_mock/request_handlers/transfers.rb,
lib/stripe_mock/errors/server_timeout_error.rb,
lib/stripe_mock/request_handlers/recipients.rb,
lib/stripe_mock/errors/unstarted_state_error.rb,
lib/stripe_mock/request_handlers/invoice_items.rb,
lib/stripe_mock/request_handlers/subscriptions.rb,
lib/stripe_mock/errors/unsupported_request_error.rb,
lib/stripe_mock/errors/uninitialized_instance_error.rb,
lib/stripe_mock/errors/closed_client_connection_error.rb,
lib/stripe_mock/request_handlers/helpers/card_helpers.rb,
lib/stripe_mock/request_handlers/helpers/token_helpers.rb,
lib/stripe_mock/request_handlers/helpers/charge_helpers.rb,
lib/stripe_mock/request_handlers/validators/param_validators.rb,
lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb

Defined Under Namespace

Modules: CardErrors, Data, RequestHandlers, TestStrategies, Util, Webhooks Classes: Client, ClosedClientConnectionError, ErrorQueue, Instance, Server, ServerTimeoutError, StripeMockError, UninitializedInstanceError, UnstartedStateError, UnsupportedRequestError

Constant Summary collapse

VERSION =

stripe-ruby-mock version

"2.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_server_log_path=(value) ⇒ Object (writeonly)

Sets the attribute default_server_log_path

Parameters:

  • value

    the value to set the attribute default_server_log_path to.



7
8
9
# File 'lib/stripe_mock/api/server.rb', line 7

def default_server_log_path=(value)
  @default_server_log_path = value
end

.default_server_pid_path=(value) ⇒ Object (writeonly)

Sets the attribute default_server_pid_path

Parameters:

  • value

    the value to set the attribute default_server_pid_path to.



7
8
9
# File 'lib/stripe_mock/api/server.rb', line 7

def default_server_pid_path=(value)
  @default_server_pid_path = value
end

.webhook_fixture_pathObject

Returns the value of attribute webhook_fixture_path.



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

def webhook_fixture_path
  @webhook_fixture_path
end

Class Method Details

.alias_stripe_method(new_name, method_object) ⇒ Object



21
22
23
# File 'lib/stripe_mock/api/instance.rb', line 21

def self.alias_stripe_method(new_name, method_object)
  Stripe.define_singleton_method(new_name) {|*args| method_object.call(*args) }
end

.clientObject



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

def self.client; @client; end

.create_test_helper(strategy = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/stripe_mock/api/test_helpers.rb', line 3

def self.create_test_helper(strategy=nil)
  if strategy
    get_test_helper_strategy(strategy).new
  elsif @__test_strat
    @__test_strat.new
  else
    TestStrategies::Mock.new
  end
end

.generate_bank_token(bank_params) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/stripe_mock/api/bank_tokens.rb', line 3

def self.generate_bank_token(bank_params)
  if @state == 'local'
    instance.generate_bank_token(bank_params)
  elsif @state == 'remote'
    client.generate_bank_token(bank_params)
  else
    raise UnstartedStateError
  end
end

.generate_card_token(card_params) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/stripe_mock/api/card_tokens.rb', line 3

def self.generate_card_token(card_params)
  if @state == 'local'
    instance.generate_card_token(card_params)
  elsif @state == 'remote'
    client.generate_card_token(card_params)
  else
    raise UnstartedStateError
  end
end

.get_test_helper_strategy(strategy) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/stripe_mock/api/test_helpers.rb', line 17

def self.get_test_helper_strategy(strategy)
  case strategy.to_sym
  when :mock then TestStrategies::Mock
  when :live then TestStrategies::Live
  else raise "Invalid test helper strategy: #{strategy.inspect}"
  end
end

.global_id_prefixObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/stripe_mock/api/global_id_prefix.rb', line 3

def self.global_id_prefix
  if StripeMock.client
    StripeMock.client.server_global_id_prefix
  else
    case @global_id_prefix
      when false then ""
      when nil then "test_"
      else @global_id_prefix
    end
  end
end

.global_id_prefix=(value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/stripe_mock/api/global_id_prefix.rb', line 15

def self.global_id_prefix=(value)
  if StripeMock.client
    StripeMock.client.set_server_global_id_prefix(value)
  else
    @global_id_prefix = value
  end
end

.instanceObject



25
# File 'lib/stripe_mock/api/instance.rb', line 25

def self.instance; @instance; end

.kill_server(pid_path = nil) ⇒ Object



27
28
29
30
31
# File 'lib/stripe_mock/api/server.rb', line 27

def kill_server(pid_path=nil)
  puts "Killing server at #{pid_path}"
  path = pid_path || default_server_pid_path
  Dante::Runner.new('stripe-mock-server').execute(:kill => true, :pid_path => path)
end

.mock_webhook_event(type, params = {}) ⇒ Object



31
32
33
# File 'lib/stripe_mock/api/webhooks.rb', line 31

def self.mock_webhook_event(type, params={})
  Stripe::Event.construct_from(mock_webhook_payload(type, params))
end

.mock_webhook_payload(type, params = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/stripe_mock/api/webhooks.rb', line 3

def self.mock_webhook_payload(type, params = {})

  fixture_file = File.join(@webhook_fixture_path, "#{type}.json")

  if File.exists?(fixture_file) == false
    unless Webhooks.event_list.include?(type)
      raise UnsupportedRequestError.new "Unsupported webhook event `#{type}` (Searched in #{@webhook_fixture_path})"
    end
    fixture_file = File.join(@webhook_fixture_fallback_path, "#{type}.json")
  end

  json = MultiJson.load  File.read(fixture_file)

  json = Stripe::Util.symbolize_names(json)
  params = Stripe::Util.symbolize_names(params)
  json[:data][:object] = Util.rmerge(json[:data][:object], params)
  json.delete(:id)

  if @state == 'local'
    event_data = instance.generate_webhook_event(json)
  elsif @state == 'remote'
    event_data = client.generate_webhook_event(json)
  else
    raise UnstartedStateError
  end
  event_data
end

.prepare_card_error(code, *handler_names) ⇒ Object

Raises:



15
16
17
18
19
20
21
# File 'lib/stripe_mock/api/errors.rb', line 15

def self.prepare_card_error(code, *handler_names)
  handler_names.push(:new_charge) if handler_names.count == 0

  args = CardErrors.argument_map[code]
  raise StripeMockError.new("Unrecognized stripe card error code: #{code}") if args.nil?
  self.prepare_error  Stripe::CardError.new(*args), *handler_names
end

.prepare_error(stripe_error, *handler_names) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/stripe_mock/api/errors.rb', line 3

def self.prepare_error(stripe_error, *handler_names)
  handler_names.push(:all) if handler_names.count == 0

  if @state == 'local'
    instance.error_queue.queue(stripe_error, handler_names)
  elsif @state == 'remote'
    client.error_queue.queue(stripe_error, handler_names)
  else
    raise UnstartedStateError
  end
end

.set_default_test_helper_strategy(strategy) ⇒ Object



13
14
15
# File 'lib/stripe_mock/api/test_helpers.rb', line 13

def self.set_default_test_helper_strategy(strategy)
  @__test_strat = get_test_helper_strategy(strategy)
end

.spawn_server(opts = {}) ⇒ Object



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

def spawn_server(opts={})
  pid_path = opts[:pid_path] || default_server_pid_path
  log_path = opts[:log_path] || default_server_log_path

  Dante::Runner.new('stripe-mock-server').execute(
    :daemonize => true, :pid_path => pid_path, :log_path => log_path
  ){
    StripeMock::Server.start_new(opts)
  }
  at_exit { kill_server(pid_path) }
end

.startObject



7
8
9
10
11
12
# File 'lib/stripe_mock/api/instance.rb', line 7

def self.start
  return false if @state == 'live'
  @instance = Instance.new
  alias_stripe_method :request, @instance.method(:mock_request)
  @state = 'local'
end

.start_client(port = 4999) ⇒ Object



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

def self.start_client(port=4999)
  return false if @state == 'live'
  return @client unless @client.nil?

  alias_stripe_method :request, StripeMock.method(:redirect_to_mock_server)
  @client = StripeMock::Client.new(port)
  @state = 'remote'
  @client
end

.stateObject



26
# File 'lib/stripe_mock/api/instance.rb', line 26

def self.state; @state; end

.stopObject



14
15
16
17
18
19
# File 'lib/stripe_mock/api/instance.rb', line 14

def self.stop
  return unless @state == 'local'
  alias_stripe_method :request, @original_request_method
  @instance = nil
  @state = 'ready'
end

.stop_client(opts = {}) ⇒ Object



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

def self.stop_client(opts={})
  return false unless @state == 'remote'
  @state = 'ready'

  alias_stripe_method :request, @original_request_method
  @client.clear_server_data if opts[:clear_server_data] == true
  @client.cleanup
  @client = nil
  true
end

.toggle_debug(toggle) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/stripe_mock/api/debug.rb', line 3

def self.toggle_debug(toggle)
  if @state == 'local'
    @instance.debug = toggle
  elsif @state == 'remote'
    @client.set_server_debug(toggle)
  end
end

.toggle_live(toggle) ⇒ Object



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

def self.toggle_live(toggle)
  if @state != 'ready' && @state != 'live'
    raise "You cannot toggle StripeMock live when it has already started."
  end
  if toggle
    @state = 'live'
    StripeMock.set_default_test_helper_strategy(:live)
  else
    @state = 'ready'
    StripeMock.set_default_test_helper_strategy(:mock)
  end
end