class Api::Api

Public Instance Methods

consume_api() click to toggle source

Consuming yahoo finances api and transform in hash for ruby

# File lib/inox_converter/api/api.rb, line 10
def consume_api
        @dados = RestClient::Request.execute(method: :get, url: 'https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote')
        hash_local = Hash.new
        @hash_local = Hash.from_xml(@dados)
end
convert_currency(valueToConvert, firstUnit, secondUnit) click to toggle source

new metodo for convert currency

# File lib/inox_converter/api/api.rb, line 82
def convert_currency(valueToConvert, firstUnit, secondUnit)
        dictionary_api
        if validate_usd_unit(firstUnit) && validate_usd_unit(secondUnit)
                return valueToConvert
        elsif validate_usd_unit(firstUnit) && validate_usd_unit(secondUnit) == false
                if validate_currency_unit(secondUnit)
                        finalValue = valueToConvert * @hash[secondUnit]
                        return finalValue
                else
                        return 0
                end
        elsif validate_usd_unit(firstUnit) == false && validate_usd_unit(secondUnit)
                if validate_currency_unit(firstUnit)
                        finalValue = valueToConvert / @hash[firstUnit]
                        return finalValue
                else
                        return 0
                end
        else
                if data_validate_api(firstUnit, secondUnit)
                        finalValue = (valueToConvert / @hash[firstUnit]) * @hash[secondUnit]
                        return finalValue
                else         
                        return 0
                end
        end          
end
data_validate_api(firstUnit, secondUnit) click to toggle source
# File lib/inox_converter/api/api.rb, line 50
def data_validate_api(firstUnit, secondUnit)
        if @data == false || return_hash_currency(firstUnit) == false || return_hash_currency(secondUnit) == false
        return false
        else
                return true
        end 
end
dictionary_api() click to toggle source

Template of execution sequence of the methods and return @hash

# File lib/inox_converter/api/api.rb, line 75
def dictionary_api
        consume_api
        treat_data   
end
return_hash_currency(valor) click to toggle source

verifield value in @hash for calcule currency convert

# File lib/inox_converter/api/api.rb, line 42
def return_hash_currency(valor)
        if @hash[valor].nil?
                return false
        else
                return true
        end          
end
treat_data() click to toggle source

Treating data in hash

# File lib/inox_converter/api/api.rb, line 17
def treat_data
        @hash_inter = Hash.new
        @hash = Hash.new
        if validate_api_return
                @hash_inter = @hash_local['list']['resources']['resource']
                @hash_inter.each do |cout|
                        simbol_string = cout['field'][0].to_s
                        simbol = simbol_string.split("/")
                    @hash[simbol[1]] = cout['field'][1].to_f
              end
      else
                @data = false
        end 
end
validate_api_return() click to toggle source

verifield return of consumir api

# File lib/inox_converter/api/api.rb, line 33
def validate_api_return
        if @dados.nil?
                return false
        else
                return true
        end
end
validate_currency_unit(currency) click to toggle source
# File lib/inox_converter/api/api.rb, line 58
def validate_currency_unit(currency)
        if @data == false || return_hash_currency(currency) == false
                return false
        else
                return true
        end
end
validate_usd_unit(usd) click to toggle source
# File lib/inox_converter/api/api.rb, line 66
def validate_usd_unit(usd) 
        if usd == "USD"
              return true;
      else
              return false
      end
end