lib/paragoz.rb in paragoz-2.2.4 vs lib/paragoz.rb in paragoz-2.2.5

- old
+ new

@@ -14,23 +14,23 @@ @@currencies_defined = 0 def initialize(code, amount, data, date) @data = data || parse_data(take_response(code.upcase, date)) - @time_array = @data["date"].split('-').map {|i| i = i.to_i} + @time_array = @data["date"].split('-').map(&:to_i) @base = @data["base"] @date = @time_array && @time_array.is_a?(Array) ? Time.new(*@time_array) : Time.now @rates = @data["rates"] @costs = cost_of_other_currencies @amount = amount @@currencies_defined += 1 end def amount=(value) - if value.is_a?(Numeric) && value > 0 - @amount = value + if value.is_a?(Numeric) && value.to_i > 0 + @amount = value.to_i else puts "ERROR! Amount must be a Number and greater than 0." end end @@ -58,19 +58,18 @@ costs[k] = 1 / v end costs end - def calculate_cost(currency_code ,calculate_amount = 1.0, info = false) - cost = self.costs[currency_code.upcase] * calculate_amount || self.amount - if amount.is_a?(Float) && amount > 0 && CURRENCY_CODES.include?(currency_code.upcase) + def calculate_cost(currency_code ,calculation_amount = 1.0, info = false) + cost = 0.0 + if amount.is_a?(Numeric) && amount > 0 && CURRENCY_CODES.include?(currency_code.upcase) + cost = self.costs[currency_code.upcase] * calculation_amount || self.amount puts "You need #{cost} #{@base} to buy #{amount} #{currency_code}" if info cost else puts "ERROR! You need to give 2 parameters >> currency_code & calculate_amount" - puts "Use 'Paragoz::CURRENCY_CODES' for see all defined currency codes." - puts "Amount has to be an float and greater than 0." end end def currency_to_currency(other_currency_object, info = false) if self.base != other_currency_object.base @@ -116,14 +115,14 @@ private class Comparation attr_reader :time_difference, :comparation_rates, :comparation_costs def initialize(currency_object, currency_object_to_compare) - @time_difference = currency_object.date > currency_object_to_compare.date ? "Currency Object's Rates Are Newer" : - "Currency Object's Rates Are Older" - @comparation_rates = compare_rates(currency_object.rates, currency_object_to_compare.rates) - @comparation_costs = compare_costs(currency_object.costs, currency_object_to_compare.costs) + @time_difference = currency_object.date > currency_object_to_compare.date ? "Currency Object's Rates Are Newer" : + "Currency Object's Rates Are Older" + @comparation_rates = compare_rates(currency_object.rates, currency_object_to_compare.rates) + @comparation_costs = compare_costs(currency_object.costs, currency_object_to_compare.costs) end private def compare_rates(rates, rates_to_cmpr) comparation_hash = {} rates.each_pair do |k, v| @@ -179,13 +178,13 @@ if code.is_a?(String) && CURRENCY_CODES.include?(code.upcase) && amount.is_a?(Float) && amount > 0 && date.nil? || date =~ /^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/ Currency.new(code, amount, data, date) else puts "ERROR!" - puts "to define a currency you have to give at least two named parameters:" - puts "code: 'currency code as a string' & amount: 'and float greater than 0'" - puts "Use 'Paragoz::CURRENCY_CODES' for see all defined currency codes." - puts "date: parameter format 'YYYY-MM-DD'" - puts "You can use customized data formated as fixer.io JSON" + puts "to define a currency you have to give at least two named parameters: + code: 'currency code as a string' & amount: 'and float greater than 0' + Use 'Paragoz::CURRENCY_CODES' for see all defined currency codes. + date: parameter format 'YYYY-MM-DD' + You can use customized data formated as fixer.io JSON" end end end