Class: HG::Finance::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/hg/finance/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, host_name, use_ssl = true) ⇒ Data

Returns a new instance of Data.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#cryptocurrenciesObject

Returns the value of attribute cryptocurrencies.



12
13
14
# File 'lib/hg/finance/data.rb', line 12

def cryptocurrencies
  @cryptocurrencies
end

#currenciesObject

Returns the value of attribute currencies.



12
13
14
# File 'lib/hg/finance/data.rb', line 12

def currencies
  @currencies
end

#key_statusObject

Returns the value of attribute key_status.



11
12
13
# File 'lib/hg/finance/data.rb', line 11

def key_status
  @key_status
end

#requestObject

Returns the value of attribute request.



11
12
13
# File 'lib/hg/finance/data.rb', line 11

def request
  @request
end

#requested_atObject

Returns the value of attribute requested_at.



11
12
13
# File 'lib/hg/finance/data.rb', line 11

def requested_at
  @requested_at
end

#stocksObject

Returns the value of attribute stocks.



12
13
14
# File 'lib/hg/finance/data.rb', line 12

def stocks
  @stocks
end

#taxesObject

Returns the value of attribute taxes.



12
13
14
# File 'lib/hg/finance/data.rb', line 12

def taxes
  @taxes
end

Instance Method Details

#to_cryptocurrency(r, name = 'Bitcoin', iso_code = 'BTC') ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



87
88
89
90
91
92
93
# 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) ⇒ Object



54
55
56
57
58
59
60
61
# 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