class HG::Finance::Data
Attributes
cryptocurrencies[RW]
currencies[RW]
key_status[RW]
request[RW]
requested_at[RW]
stocks[RW]
taxes[RW]
Public Class Methods
new(params, host_name, use_ssl = true)
click to toggle source
# File lib/hg/finance/data.rb, line 14 def initialize params, host_name, use_ssl = true query_params = params.map{|k,v| "#{k.to_s}=#{v.to_s}"}.join('&') @request = (use_ssl ? 'https' : 'http') + host_name + '?' + query_params @requested_at = Time.now request_data = JSON.parse(open(self.request).read) if request_data['results'] results = request_data['results'] @key_status = (params[:key] ? (request_data['valid_key'] ? :valid : :invalid) : :empty) @taxes = Taxes.new(to_taxes(results['taxes'].first)) if @key_status == :valid @currencies = {} if results.has_key?('currencies') results['currencies'].each do |iso, currency| next if iso == 'source' @currencies[iso] = Currency.new(to_currency(currency, iso, results['currencies']['source'])) end end @cryptocurrencies = {} if results.has_key?('bitcoin') results['bitcoin'].each do |exchange, c| @cryptocurrencies[exchange.to_sym] = { "btc#{c['format'][0]}".downcase.to_sym => CryptoCurrency.new(to_cryptocurrency(c, 'Bitcoin', 'BTC')) } end end @stocks = {} if results.has_key?('stocks') results['stocks'].each do |code, stock| @stocks[code] = Stock.new(to_stock(stock)) end end end return self end
Public Instance Methods
to_cryptocurrency(r, name = 'Bitcoin', iso_code = 'BTC')
click to toggle source
# File lib/hg/finance/data.rb, line 74 def to_cryptocurrency r, name = 'Bitcoin', iso_code = 'BTC' { to_currency: r['format'][0], exchange: r['name'], name: name, iso_code: iso_code, last: r['last'], buy: r['buy'], sell: r['sell'], variation: r['variation'] } end
to_currency(r, iso_code, source)
click to toggle source
# File lib/hg/finance/data.rb, line 63 def to_currency r, iso_code, source { source: source, iso_code: iso_code, name: r['name'], buy: r['buy'], sell: r['sell'], variation: r['variation'] } end
to_stock(r)
click to toggle source
# File lib/hg/finance/data.rb, line 87 def to_stock r { name: r['name'], location: r['location'], variation: r['variation'] } end
to_taxes(r)
click to toggle source
# File lib/hg/finance/data.rb, line 54 def to_taxes r { date: r['date'], cdi: r['cdi'], selic: r['selic'], daily_factor: r['daily_factor'] } end