Class: HG::Finance::Data
- Inherits:
-
Object
- Object
- HG::Finance::Data
- Defined in:
- lib/hg/finance/data.rb
Instance Attribute Summary collapse
-
#currencies ⇒ Object
Returns the value of attribute currencies.
-
#key_status ⇒ Object
Returns the value of attribute key_status.
-
#request ⇒ Object
Returns the value of attribute request.
-
#requested_at ⇒ Object
Returns the value of attribute requested_at.
-
#stocks ⇒ Object
Returns the value of attribute stocks.
-
#taxes ⇒ Object
Returns the value of attribute taxes.
Instance Method Summary collapse
-
#initialize(params, host_name, use_ssl = true) ⇒ Data
constructor
A new instance of Data.
- #to_currency(r, iso_code, source) ⇒ Object
- #to_stock(r) ⇒ Object
- #to_taxes(r) ⇒ Object
Constructor Details
#initialize(params, host_name, use_ssl = true) ⇒ Data
Returns a new instance of Data.
13 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 |
# File 'lib/hg/finance/data.rb', line 13 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 = {} results['currencies'].each do |iso, currency| next if iso == 'source' @currencies[iso] = Currency.new(to_currency(currency, iso, results['currencies']['source'])) end @stocks = {} results['stocks'].each do |code, stock| @stocks[code] = Stock.new(to_stock(stock)) end end return self end |
Instance Attribute Details
#currencies ⇒ Object
Returns the value of attribute currencies.
11 12 13 |
# File 'lib/hg/finance/data.rb', line 11 def currencies @currencies end |
#key_status ⇒ Object
Returns the value of attribute key_status.
10 11 12 |
# File 'lib/hg/finance/data.rb', line 10 def key_status @key_status end |
#request ⇒ Object
Returns the value of attribute request.
10 11 12 |
# File 'lib/hg/finance/data.rb', line 10 def request @request end |
#requested_at ⇒ Object
Returns the value of attribute requested_at.
10 11 12 |
# File 'lib/hg/finance/data.rb', line 10 def requested_at @requested_at end |
#stocks ⇒ Object
Returns the value of attribute stocks.
11 12 13 |
# File 'lib/hg/finance/data.rb', line 11 def stocks @stocks end |
#taxes ⇒ Object
Returns the value of attribute taxes.
11 12 13 |
# File 'lib/hg/finance/data.rb', line 11 def taxes @taxes end |
Instance Method Details
#to_currency(r, iso_code, source) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hg/finance/data.rb', line 51 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
62 63 64 65 66 67 68 |
# File 'lib/hg/finance/data.rb', line 62 def to_stock r { name: r['name'], location: r['location'], variation: r['variation'] } end |
#to_taxes(r) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/hg/finance/data.rb', line 42 def to_taxes r { date: r['date'], cdi: r['cdi'], selic: r['selic'], daily_factor: r['daily_factor'] } end |