module StripeMock
Constants
- VERSION
stripe-ruby-mock version
Attributes
default_currency[RW]
default_server_log_path[W]
default_server_pid_path[W]
webhook_fixture_path[RW]
Public Class Methods
client()
click to toggle source
# File lib/stripe_mock/api/client.rb, line 3 def self.client @client end
create_test_helper(strategy=nil)
click to toggle source
# 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 = {})
click to toggle source
# File lib/stripe_mock/api/bank_tokens.rb, line 3 def self.generate_bank_token(bank_params = {}) case @state when 'local' instance.generate_bank_token(bank_params) when 'remote' client.generate_bank_token(bank_params) else raise UnstartedStateError end end
generate_card_token(card_params = {})
click to toggle source
# File lib/stripe_mock/api/card_tokens.rb, line 3 def self.generate_card_token(card_params = {}) case @state when 'local' instance.generate_card_token(card_params) when 'remote' client.generate_card_token(card_params) else raise UnstartedStateError end end
get_test_helper_strategy(strategy)
click to toggle source
# 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_prefix()
click to toggle source
# 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)
click to toggle source
# 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
instance()
click to toggle source
# File lib/stripe_mock/api/instance.rb, line 35 def self.instance; @instance; end
kill_server(pid_path=nil)
click to toggle source
# File lib/stripe_mock/api/server.rb, line 33 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() { || ... }
click to toggle source
Yield the given block between StripeMock.start
and StripeMock.stop
# File lib/stripe_mock/api/instance.rb, line 22 def self.mock(&block) begin self.start yield ensure self.stop end end
mock_webhook_event(type, params={})
click to toggle source
# File lib/stripe_mock/api/webhooks.rb, line 33 def self.mock_webhook_event(type, params={}) Stripe::Event.construct_from(mock_webhook_payload(type, params)) end
mock_webhook_payload(type, params = {})
click to toggle source
# 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") unless File.exists?(fixture_file) 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[:account] = params.delete(:account) if params.key?(:account) json[:data][:object] = Util.rmerge(json[:data][:object], params) json.delete(:id) json[:created] = params[:created] || Time.now.to_i 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)
click to toggle source
# File lib/stripe_mock/api/errors.rb, line 14 def self.prepare_card_error(code, *handler_names) handler_names.push(:new_charge) if handler_names.count == 0 error = CardErrors.build_error_for(code) if error.nil? raise StripeMockError, "Unrecognized stripe card error code: #{code}" end prepare_error error, *handler_names end
prepare_error(stripe_error, *handler_names)
click to toggle source
# File lib/stripe_mock/api/errors.rb, line 2 def self.prepare_error(stripe_error, *handler_names) handler_names.push(:all) if handler_names.count == 0 if @state == 'local' instance elsif @state == 'remote' client else raise UnstartedStateError end.error_queue.queue stripe_error, handler_names end
restore_stripe_execute_request_method()
click to toggle source
# File lib/stripe_mock/api/instance.rb, line 31 def self.restore_stripe_execute_request_method Stripe::StripeClient.send(:define_method, :execute_request, @original_execute_request_method) end
set_account_balance(value)
click to toggle source
# File lib/stripe_mock/api/account_balance.rb, line 3 def self.set_account_balance(value) case @state when 'local' instance.account_balance = value when 'remote' client.set_account_balance(value) else raise UnstartedStateError end end
set_conversion_rate(value)
click to toggle source
# File lib/stripe_mock/api/conversion_rate.rb, line 3 def self.set_conversion_rate(value) case @state when 'local' instance.conversion_rate = value when 'remote' client.set_conversion_rate(value) else raise UnstartedStateError end end
set_default_test_helper_strategy(strategy)
click to toggle source
# 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={})
click to toggle source
# File lib/stripe_mock/api/server.rb, line 14 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 { begin e = $! # last exception kill_server(pid_path) ensure raise e if $! != e end } end
start()
click to toggle source
# File lib/stripe_mock/api/instance.rb, line 7 def self.start return false if @state == 'live' @instance = instance = Instance.new Stripe::StripeClient.send(:define_method, :execute_request) { |*args| instance.mock_request(*args) } @state = 'local' end
start_client(port=4999)
click to toggle source
# File lib/stripe_mock/api/client.rb, line 7 def self.start_client(port=4999) return false if @state == 'live' return @client unless @client.nil? Stripe::StripeClient.send(:define_method, :execute_request) { |*args| StripeMock.redirect_to_mock_server(*args) } @client = StripeMock::Client.new(port) @state = 'remote' @client end
state()
click to toggle source
# File lib/stripe_mock/api/instance.rb, line 36 def self.state; @state; end
stop()
click to toggle source
# File lib/stripe_mock/api/instance.rb, line 14 def self.stop return unless @state == 'local' restore_stripe_execute_request_method @instance = nil @state = 'ready' end
stop_client(opts={})
click to toggle source
# File lib/stripe_mock/api/client.rb, line 17 def self.stop_client(opts={}) return false unless @state == 'remote' @state = 'ready' restore_stripe_execute_request_method @client.clear_server_data if opts[:clear_server_data] == true @client.cleanup @client = nil true end
toggle_debug(toggle)
click to toggle source
# 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)
click to toggle source
# 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
Private Class Methods
redirect_to_mock_server(method, url, api_key: nil, api_base: nil, params: {}, headers: {})
click to toggle source
# File lib/stripe_mock/api/client.rb, line 30 def self.redirect_to_mock_server(method, url, api_key: nil, api_base: nil, params: {}, headers: {}) handler = Instance.handler_for_method_url("#{method} #{url}") if mock_error = client.error_queue.error_for_handler_name(handler[:name]) client.error_queue.dequeue raise mock_error end Stripe::Util.symbolize_names client.mock_request(method, url, api_key: api_key, params: params, headers: headers) end