Class: Balanced::Customer
- Inherits:
-
Object
- Object
- Balanced::Customer
- Includes:
- Resource
- Defined in:
- lib/balanced/resources/customer.rb
Overview
A customer represents a business or person within your Marketplace. A customer can have many funding instruments such as cards and bank accounts associated to them.
Instance Attribute Summary
Attributes included from Resource
Class Method Summary collapse
-
.find_by_email(email) ⇒ Customer?
Attempts to find an existing customer by email.
- .uri ⇒ Object
Instance Method Summary collapse
- #active_bank_account ⇒ Object
- #active_card ⇒ Object
-
#add_bank_account(bank_account) ⇒ Customer
Associates the BankAccount represented by bank_account with this Customer.
-
#add_card(card) ⇒ Customer
Associates the Card represented by ‘card’ with this Customer.
- #credit(options = {}) ⇒ Object
- #debit(options = {}) ⇒ Object
-
#initialize(attributes = {}) ⇒ Customer
constructor
A new instance of Customer.
Methods included from Resource
#copy_from, #destroy, #find, included, #method_missing, #reload, #sanitize, #save, #unstore, #warn_on_positional
Constructor Details
#initialize(attributes = {}) ⇒ Customer
Returns a new instance of Customer.
14 15 16 17 18 19 20 |
# File 'lib/balanced/resources/customer.rb', line 14 def initialize attributes = {} Balanced::Utils.stringify_keys! attributes unless attributes.has_key? 'uri' attributes['uri'] = self.class.uri end super attributes end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Balanced::Resource
Class Method Details
.find_by_email(email) ⇒ Customer?
Attempts to find an existing customer by email
NOTE: There is no unique constraint on email_address.
Multiple customers with the same email may exist.
Only one Customer is returned.
31 32 33 |
# File 'lib/balanced/resources/customer.rb', line 31 def self.find_by_email email self.find(:first, :email => email) end |
.uri ⇒ Object
9 10 11 12 |
# File 'lib/balanced/resources/customer.rb', line 9 def self.uri # Override the default nesting self.collection_path end |
Instance Method Details
#active_bank_account ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/balanced/resources/customer.rb', line 105 def active_bank_account pager = Pager.new( self.bank_accounts_uri, :is_active => true, :sort => 'created_at,desc', :limit => 1) pager.first end |
#active_card ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/balanced/resources/customer.rb', line 96 def active_card pager = Pager.new( self.cards_uri, :is_active => true, :sort => 'created_at,desc', :limit => 1) pager.first end |
#add_bank_account(bank_account) ⇒ Customer
Associates the BankAccount represented by bank_account with this Customer.
88 89 90 91 92 93 94 |
# File 'lib/balanced/resources/customer.rb', line 88 def add_bank_account(bank_account) if bank_account.kind_of?(Balanced::BankAccount) && bank_account.fingerprint.nil? bank_account.save end self.bank_account_uri = Balanced::Utils.extract_uri_from_object(bank_account) save end |
#add_card(card) ⇒ Customer
Associates the Card represented by ‘card’ with this Customer.
78 79 80 81 82 |
# File 'lib/balanced/resources/customer.rb', line 78 def add_card(card) card.save if card.kind_of?(Balanced::Card) && card.hash.nil? self.card_uri = Balanced::Utils.extract_uri_from_object(card) save end |
#credit(options = {}) ⇒ Object
69 70 71 72 |
# File 'lib/balanced/resources/customer.rb', line 69 def credit( = {}) .merge!(:uri => self.credits_uri) Credit.new().save end |
#debit(options = {}) ⇒ Object
35 36 37 38 39 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/balanced/resources/customer.rb', line 35 def debit( = {}) amount = [:amount] appears_on_statement_as = [:appears_on_statement_as] hold_uri = [:hold_uri] = [:meta] description = [:description] source_uri = [:source_uri] on_behalf_of = [:on_behalf_of] || [:on_behalf_of_uri] if on_behalf_of if on_behalf_of.respond_to? :uri on_behalf_of = on_behalf_of.uri end if !on_behalf_of.is_a?(String) raise ArgumentError, 'The on_behalf_of parameter needs to be a customer URI' end if on_behalf_of == self.uri raise ArgumentError, 'The on_behalf_of parameter MAY NOT be the same account as the customer you are debiting!' end end debit = Debit.new( :uri => self.debits_uri, :amount => amount, :appears_on_statement_as => appears_on_statement_as, :hold_uri => hold_uri, :meta => , :description => description, :source_uri => source_uri, :on_behalf_of_uri => on_behalf_of, ) debit.save end |