Class: Money
- Inherits:
-
Object
- Object
- Money
- Defined in:
- lib/waehrungsrechner/money.rb
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#base_currency ⇒ Object
readonly
Returns the value of attribute base_currency.
-
#rates ⇒ Object
readonly
Returns the value of attribute rates.
Instance Method Summary collapse
-
#+(other) ⇒ Object
adding money objects together.
- #conversion_amount(transfer_currency) ⇒ Object
- #convert_to(transfer_currency) ⇒ Object
-
#initialize(amount, base_currency) ⇒ Money
constructor
A new instance of Money.
- #inspect ⇒ Object
Constructor Details
#initialize(amount, base_currency) ⇒ Money
Returns a new instance of Money.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/waehrungsrechner/money.rb', line 6 def initialize amount, base_currency raise ArgumentError, "Amount must be a number" unless amount.is_a? Numeric raise ArgumentError, " `#{base_currency}` is invalid (currency symbol must be a 3 character string)`#{base_currency}` is invalid." unless valid_currency? base_currency begin @rate_info = HTTParty.get "http://api.fixer.io/latest?base=#{base_currency}" raise ArgumentError, "`#{base_currency}` is an invalid currency symbol." if @rate_info.include? 'error' rescue Exception => e puts "Something went wrong. Please make sure are connected to the internet." puts e.inspect end base_currency.upcase! @amount = amount @base_currency = base_currency end |
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
4 5 6 |
# File 'lib/waehrungsrechner/money.rb', line 4 def amount @amount end |
#base_currency ⇒ Object (readonly)
Returns the value of attribute base_currency.
4 5 6 |
# File 'lib/waehrungsrechner/money.rb', line 4 def base_currency @base_currency end |
#rates ⇒ Object (readonly)
Returns the value of attribute rates.
4 5 6 |
# File 'lib/waehrungsrechner/money.rb', line 4 def rates @rates end |
Instance Method Details
#+(other) ⇒ Object
adding money objects together
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/waehrungsrechner/money.rb', line 36 def +(other) if base_currency == other.base_currency Money.new(amount + other.amount, base_currency) else # maybe ask the user? puts "Which currency would you like to see the total in?\n1.#{base_currency}\n2.#{other.base_currency}" answer = gets.chomp case answer.upcase # calculate the total in the base currency when '1', "#{base_currency}" Money.new(amount + other.conversion_amount(base_currency), base_currency) # calculate the total in the transfer currency when '2', "#{other.base_currency}" Money.new(self.conversion_amount(other.base_currency) + other.amount, other.base_currency) end end end |
#conversion_amount(transfer_currency) ⇒ Object
31 32 33 |
# File 'lib/waehrungsrechner/money.rb', line 31 def conversion_amount transfer_currency convert_to(transfer_currency).amount end |
#convert_to(transfer_currency) ⇒ Object
25 26 27 28 29 |
# File 'lib/waehrungsrechner/money.rb', line 25 def convert_to transfer_currency raise ArgumentError, "`#{transfer_currency}` must be a 3 character string" unless transfer_currency.is_a?(String) && transfer_currency.length == 3 raise ArgumentError, "`#{transfer_currency}` is either an invalid currency symbol, or it is not currently supported." unless (rates = @rate_info['rates']) && rates.keys.include?(transfer_currency) Money.new(amount * rates[transfer_currency], transfer_currency) end |
#inspect ⇒ Object
21 22 23 |
# File 'lib/waehrungsrechner/money.rb', line 21 def inspect "#{('%.2f' % amount)} #{base_currency.upcase}" end |