Class: StripeMock::Instance

Constant Summary collapse

@@handlers =

Handlers are ordered by priority

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestHandlers::Tokens

#create_token, #get_token, included

Methods included from RequestHandlers::Recipients

#get_recipient, included, #new_recipient, #update_recipient

Methods included from RequestHandlers::Plans

#delete_plan, #get_plan, included, #list_plans, #new_plan, #update_plan

Methods included from RequestHandlers::InvoiceItems

#delete_invoice_item, #get_invoice_item, included, #list_invoice_items, #new_invoice_item, #update_invoice_item

Methods included from RequestHandlers::Invoices

#get_invoice, included, #list_invoices, #new_invoice, #pay_invoice, #upcoming_invoice, #update_invoice

Methods included from RequestHandlers::Events

included, #retrieve_event

Methods included from RequestHandlers::Coupons

#delete_coupon, #get_coupon, included, #list_coupons, #new_coupon

Methods included from RequestHandlers::Customers

#delete_customer, #get_customer, included, #list_customers, #new_customer, #update_customer

Methods included from RequestHandlers::Subscriptions

#cancel_subscription, #create_subscription, included, #retrieve_subscription, #retrieve_subscriptions, #update_subscription

Methods included from RequestHandlers::Cards

#create_card, #delete_card, included, #retrieve_card, #retrieve_cards, #retrieve_recipient_card, #update_card

Methods included from RequestHandlers::Charges

#capture_charge, #create_refund, #get_charge, #get_charges, included, #new_charge, #refund_charge

Methods included from RequestHandlers::ParamValidators

#validate_create_plan_params

Methods included from RequestHandlers::Helpers

#add_card_to_object, #add_refund_to_charge, #add_subscription_to_customer, #custom_subscription_params, #delete_subscription_from_customer, #generate_bank_token, #generate_card_token, #get_bank_by_token, #get_card, #get_card_by_token, #get_customer_subscription, #get_ending_time, #verify_trial_end

Constructor Details

#initializeInstance

Returns a new instance of Instance.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stripe_mock/instance.rb', line 39

def initialize
  @bank_tokens = {}
  @card_tokens = {}
  @customers = {}
  @charges = {}
  @coupons = {}
  @events = {}
  @invoices = {}
  @invoice_items = {}
  @plans = {}
  @recipients = {}
  @subscriptions = {}

  @debug = false
  @error_queue = ErrorQueue.new
  @id_counter = 0
  @balance_transaction_counter = 0

  # This is basically a cache for ParamValidators
  @base_strategy = TestStrategies::Base.new
end

Instance Attribute Details

#bank_tokensObject (readonly)

Returns the value of attribute bank_tokens.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def bank_tokens
  @bank_tokens
end

#chargesObject (readonly)

Returns the value of attribute charges.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def charges
  @charges
end

#couponsObject (readonly)

Returns the value of attribute coupons.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def coupons
  @coupons
end

#customersObject (readonly)

Returns the value of attribute customers.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def customers
  @customers
end

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#error_queueObject

Returns the value of attribute error_queue.



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

def error_queue
  @error_queue
end

#eventsObject (readonly)

Returns the value of attribute events.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def events
  @events
end

#invoice_itemsObject (readonly)

Returns the value of attribute invoice_items.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def invoice_items
  @invoice_items
end

#invoicesObject (readonly)

Returns the value of attribute invoices.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def invoices
  @invoices
end

#plansObject (readonly)

Returns the value of attribute plans.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def plans
  @plans
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def recipients
  @recipients
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



34
35
36
# File 'lib/stripe_mock/instance.rb', line 34

def subscriptions
  @subscriptions
end

Class Method Details

.add_handler(route, name) ⇒ Object



10
11
12
13
14
15
# File 'lib/stripe_mock/instance.rb', line 10

def self.add_handler(route, name)
  @@handlers << {
    :route => %r{^#{route}$},
    :name => name
  }
end

.handler_for_method_url(method_url) ⇒ Object



17
18
19
# File 'lib/stripe_mock/instance.rb', line 17

def self.handler_for_method_url(method_url)
  @@handlers.find {|h| method_url =~ h[:route] }
end

Instance Method Details

#generate_webhook_event(event_data) ⇒ Object



91
92
93
94
# File 'lib/stripe_mock/instance.rb', line 91

def generate_webhook_event(event_data)
  event_data[:id] ||= new_id 'evt'
  @events[ event_data[:id] ] = symbolize_names(event_data)
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/stripe_mock/instance.rb', line 61

def mock_request(method, url, api_key, params={}, headers={})
  return {} if method == :xtest

  # Ensure params hash has symbols as keys
  params = Stripe::Util.symbolize_names(params)

  method_url = "#{method} #{url}"

  if handler = Instance.handler_for_method_url(method_url)
    if @debug == true
      puts "- - - - " * 8
      puts "[StripeMock req]::#{handler[:name]} #{method} #{url}"
      puts "                  #{params}"
    end

    if mock_error = @error_queue.error_for_handler_name(handler[:name])
      @error_queue.dequeue
      raise mock_error
    else
      res = self.send(handler[:name], handler[:route], method_url, params, headers)
      puts "           [res]  #{res}" if @debug == true
      [res, api_key]
    end
  else
    puts "WARNING: Unrecognized method + url: [#{method} #{url}]"
    puts " params: #{params}"
    [{}, api_key]
  end
end