class TickerPicker::Price
Attributes
ask[RW]
bid[RW]
currency[RW]
last[RW]
timestamp[RW]
Public Class Methods
fetch(stock, market)
click to toggle source
Get prices for the market
Parameters¶ ↑
-
stock
- string -
market
- string
Returns¶ ↑
-
TickerPicker::Price
-TickerPicker::Price
instance object
# File lib/tickerpicker/price.rb, line 31 def fetch(stock, market) instance_mapping gather_info(markets(stock)[market]['url']), markets(stock)[market] end
new(price_hash = {})
click to toggle source
# File lib/tickerpicker/price.rb, line 11 def initialize(price_hash = {}) @ask = price_hash[:ask].to_f || 0.0 @bid = price_hash[:bid].to_f || 0.0 @currency = price_hash[:currency] || '' @last = price_hash[:last].to_f || 0.0 @timestamp = price_hash[:timestamp].to_f || Time.now.to_f end
Private Class Methods
gather_info(url)
click to toggle source
Get information from stock-market uri and convert it into Hash
Parameters¶ ↑
-
url
- Stock market URI
Returns¶ ↑
-
Hash
- Hash of information for given stock-market uri
# File lib/tickerpicker/price.rb, line 60 def gather_info(url) response = url =~ URI::regexp ? open(url, 'User-Agent' => user_agent, read_timeout: 2).read : open(url).read JSON.parse(response) end
instance_mapping(res_hash, stock_market)
click to toggle source
nodoc
# File lib/tickerpicker/price.rb, line 71 def instance_mapping(res_hash, stock_market) timestamp = eval("res_hash#{stock_market['mappings']['timestamp']}").to_f timestamp /= 1000000 if stock_market['mappings']['timestamp_representation'].eql?('microseconds') new({ ask: ("%f" % eval("res_hash#{stock_market['mappings']['ask']}")), bid: ("%f" % eval("res_hash#{stock_market['mappings']['bid']}")), currency: stock_market['currency'], last: ("%f" % eval("res_hash#{stock_market['mappings']['last']}")), timestamp: timestamp }) end
markets(stock)
click to toggle source
user_agent()
click to toggle source
nodoc
# File lib/tickerpicker/price.rb, line 66 def user_agent "TickerPicker Bot v#{TickerPicker::VERSION}" end