Class: Nordea::Account
- Defined in:
- lib/nordea/resources/account.rb
Instance Attribute Summary collapse
-
#balance ⇒ Object
Returns the value of attribute balance.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#index ⇒ Object
Returns the value of attribute index.
-
#name ⇒ Object
Returns the value of attribute name.
-
#session ⇒ Object
Returns the value of attribute session.
Class Method Summary collapse
Instance Method Summary collapse
- #account_type_name ⇒ Object
- #balance_to_s(decimals = 2) ⇒ Object
- #deposit(amount, currency = 'SEK', options = {}) ⇒ Object
-
#initialize(fields = {}, command_params = {}, session = nil) {|_self| ... } ⇒ Account
constructor
A new instance of Account.
- #reload ⇒ Object
- #reload_from_xml(xml_node) ⇒ Object
- #to_command_params ⇒ Object
- #to_s ⇒ Object
- #transactions(reload = false) ⇒ Object
- #withdraw(amount, currency = 'SEK', options = {}) ⇒ Object
Methods inherited from Resource
Constructor Details
#initialize(fields = {}, command_params = {}, session = nil) {|_self| ... } ⇒ Account
Returns a new instance of Account.
3 4 5 6 7 8 |
# File 'lib/nordea/resources/account.rb', line 3 def initialize(fields = {}, command_params = {}, session = nil) @name, @balance, @currency, @index = fields[:name], fields[:balance], fields[:currency], fields[:index] @command_params = command_params @session = session if session yield self if block_given? end |
Instance Attribute Details
#balance ⇒ Object
Returns the value of attribute balance.
10 11 12 |
# File 'lib/nordea/resources/account.rb', line 10 def balance @balance end |
#currency ⇒ Object
Returns the value of attribute currency.
10 11 12 |
# File 'lib/nordea/resources/account.rb', line 10 def currency @currency end |
#index ⇒ Object
Returns the value of attribute index.
10 11 12 |
# File 'lib/nordea/resources/account.rb', line 10 def index @index end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/nordea/resources/account.rb', line 10 def name @name end |
#session ⇒ Object
Returns the value of attribute session.
10 11 12 |
# File 'lib/nordea/resources/account.rb', line 10 def session @session end |
Class Method Details
.new_from_xml(xml_node, session) ⇒ Object
97 98 99 |
# File 'lib/nordea/resources/account.rb', line 97 def self.new_from_xml(xml_node, session) new({}, {}, session) { |acc| acc.reload_from_xml(xml_node) } end |
Instance Method Details
#account_type_name ⇒ Object
12 |
# File 'lib/nordea/resources/account.rb', line 12 def account_type_name; 'konton' end |
#balance_to_s(decimals = 2) ⇒ Object
41 42 43 |
# File 'lib/nordea/resources/account.rb', line 41 def balance_to_s(decimals = 2) %Q{%.#{decimals}f} % balance end |
#deposit(amount, currency = 'SEK', options = {}) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/nordea/resources/account.rb', line 88 def deposit(amount, currency = 'SEK', = {}) .symbolize_keys! from = .delete(:withdraw_from) raise ArgumentError unless from.is_a?(Nordea::Account) from.withdraw(amount, currency, .merge(:deposit_to => self)) end |
#reload ⇒ Object
45 46 47 48 |
# File 'lib/nordea/resources/account.rb', line 45 def reload @transactions = nil session.accounts(true) end |
#reload_from_xml(xml_node) ⇒ Object
50 51 52 53 54 |
# File 'lib/nordea/resources/account.rb', line 50 def reload_from_xml(xml_node) xml_node = Hpricot(xml_node) unless xml_node.class.to_s =~ /^Hpricot::/ xml_node = xml_node.at("anchor") unless xml_node.name == 'anchor' initialize(self.class._setup_fields(xml_node), self.class._setup_command_params(xml_node)) end |
#to_command_params ⇒ Object
14 15 16 |
# File 'lib/nordea/resources/account.rb', line 14 def to_command_params @command_params end |
#to_s ⇒ Object
37 38 39 |
# File 'lib/nordea/resources/account.rb', line 37 def to_s "#{name}: #{balance_to_s} #{currency}" end |
#transactions(reload = false) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/nordea/resources/account.rb', line 18 def transactions(reload = false) @transactions = nil if reload @transactions ||= begin # for some reason it needs this otherwise it would use the old transaction list session.request(Nordea::Commands::RESOURCE) nodes = session.request(Commands::TRANSACTIONS, to_command_params).parse_xml.search('go[@href="#trans"]') idx = nodes.length nodes.inject([]) do |all, node| trans = Transaction.new_from_xml(node, self) # since transactions don't have a time (just date) but are listed in chronological order # we add one second to each item in the collection so they can be sorted by date trans.date = Time.parse(trans.date.to_s) + (idx -= 1) all << trans end end end |
#withdraw(amount, currency = 'SEK', options = {}) ⇒ Object
56 57 58 59 60 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 |
# File 'lib/nordea/resources/account.rb', line 56 def withdraw(amount, currency = 'SEK', = {}) .symbolize_keys! to = [:deposit_to] raise ArgumentError, "options[:deposit_to] must be a Nordea::Account" unless to.is_a?(Nordea::Account) currency, amount = currency.to_s, amount.to_s.sub(".", ",") from_account_info = [index, currency, name].join(":") to_account_info = [to.index, to.currency, to.name].join(":") params = { :from_account_info => from_account_info, :to_account_info => to_account_info, :amount => amount, :currency_code => currency } session.request(Nordea::Commands::TRANSFER_TO_OWN_ACCOUNT_PHASE_1, params) session.request(Nordea::Commands::TRANSFER_TO_OWN_ACCOUNT_PHASE_2, params) session.request(Nordea::Commands::TRANSFER_TO_OWN_ACCOUNT_PHASE_3, { :currency_code => currency, :from_account_number => index, :from_account_name => name, :to_account_number => to.index, :to_account_name => to.name, :amount => amount, :exchange_rate => "0", :from_currency_code => currency, :to_currency_code => currency }) reload end |