Class: StripeMock::Instance
- Inherits:
-
Object
- Object
- StripeMock::Instance
- Includes:
- RequestHandlers::Charges, RequestHandlers::Customers, RequestHandlers::InvoiceItems, RequestHandlers::Plans
- Defined in:
- lib/stripe_mock/instance.rb
Constant Summary collapse
- @@handlers =
Handlers are ordered by priority
[]
Instance Attribute Summary collapse
-
#charges ⇒ Object
readonly
Returns the value of attribute charges.
-
#customers ⇒ Object
readonly
Returns the value of attribute customers.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#error_queue ⇒ Object
readonly
Returns the value of attribute error_queue.
-
#plans ⇒ Object
readonly
Returns the value of attribute plans.
-
#strict ⇒ Object
Returns the value of attribute strict.
Class Method Summary collapse
Instance Method Summary collapse
- #generate_card_token(card_params) ⇒ Object
- #generate_recipient_token(recipient_params) ⇒ Object
- #get_card_by_token(token) ⇒ Object
-
#initialize ⇒ Instance
constructor
A new instance of Instance.
- #mock_request(method, url, api_key, params = {}, headers = {}) ⇒ Object
Methods included from RequestHandlers::Plans
#delete_plan, #get_plan, included, #list_plans, #new_plan, #update_plan
Methods included from RequestHandlers::InvoiceItems
Methods included from RequestHandlers::Customers
#cancel_subscription, #delete_customer, #get_customer, included, #list_customers, #new_customer, #update_customer, #update_subscription
Methods included from RequestHandlers::Charges
#capture_charge, #get_charge, included, #new_charge
Constructor Details
#initialize ⇒ Instance
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/stripe_mock/instance.rb', line 27 def initialize @customers = {} @charges = {} @plans = {} @recipient_tokens = {} @card_tokens = {} @id_counter = 0 @error_queue = ErrorQueue.new @debug = false @strict = true end |
Instance Attribute Details
#charges ⇒ Object (readonly)
Returns the value of attribute charges.
24 25 26 |
# File 'lib/stripe_mock/instance.rb', line 24 def charges @charges end |
#customers ⇒ Object (readonly)
Returns the value of attribute customers.
24 25 26 |
# File 'lib/stripe_mock/instance.rb', line 24 def customers @customers end |
#debug ⇒ Object
Returns the value of attribute debug.
25 26 27 |
# File 'lib/stripe_mock/instance.rb', line 25 def debug @debug end |
#error_queue ⇒ Object (readonly)
Returns the value of attribute error_queue.
24 25 26 |
# File 'lib/stripe_mock/instance.rb', line 24 def error_queue @error_queue end |
#plans ⇒ Object (readonly)
Returns the value of attribute plans.
24 25 26 |
# File 'lib/stripe_mock/instance.rb', line 24 def plans @plans end |
#strict ⇒ Object
Returns the value of attribute strict.
25 26 27 |
# File 'lib/stripe_mock/instance.rb', line 25 def strict @strict end |
Class Method Details
.add_handler(route, name) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/stripe_mock/instance.rb', line 7 def self.add_handler(route, name) @@handlers << { :route => %r{^#{route}$}, :name => name } end |
.handler_for_method_url(method_url) ⇒ Object
14 15 16 |
# File 'lib/stripe_mock/instance.rb', line 14 def self.handler_for_method_url(method_url) @@handlers.find {|h| method_url =~ h[:route] } end |
Instance Method Details
#generate_card_token(card_params) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/stripe_mock/instance.rb', line 76 def generate_card_token(card_params) token = new_id 'tok' card_params[:id] = new_id 'cc' @card_tokens[token] = Data.mock_card(card_params) token end |
#generate_recipient_token(recipient_params) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/stripe_mock/instance.rb', line 69 def generate_recipient_token(recipient_params) token = new_id 'tok' recipient_params[:id] = new_id 'rec' @recipient_tokens[token] = Data.mock_card(recipient_params) token end |
#get_card_by_token(token) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/stripe_mock/instance.rb', line 83 def get_card_by_token(token) if token.nil? || @card_tokens[token].nil? Data.mock_card :id => new_id('cc') else @card_tokens.delete(token) end end |
#mock_request(method, url, api_key, params = {}, headers = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/stripe_mock/instance.rb', line 40 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 "[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 |