Class: TickerPicker::Price

Inherits:
Object
  • Object
show all
Includes:
Hashable
Defined in:
lib/tickerpicker/price.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(price_hash = {}) ⇒ Price

Returns a new instance of Price.



11
12
13
14
15
16
17
# File 'lib/tickerpicker/price.rb', line 11

def initialize(price_hash = {})
  @ask = price_hash[:ask] || 0.0
  @bid = price_hash[:bid] || 0.0
  @currency = price_hash[:currency] || ''
  @last = price_hash[:last] || 0.0
  @timestamp = price_hash[:timestamp].to_f || Time.now.to_f
end

Instance Attribute Details

#askObject

Returns the value of attribute ask.



5
6
7
# File 'lib/tickerpicker/price.rb', line 5

def ask
  @ask
end

#bidObject

Returns the value of attribute bid.



6
7
8
# File 'lib/tickerpicker/price.rb', line 6

def bid
  @bid
end

#currencyObject

Returns the value of attribute currency.



7
8
9
# File 'lib/tickerpicker/price.rb', line 7

def currency
  @currency
end

#lastObject

Returns the value of attribute last.



8
9
10
# File 'lib/tickerpicker/price.rb', line 8

def last
  @last
end

#timestampObject

Returns the value of attribute timestamp.



9
10
11
# File 'lib/tickerpicker/price.rb', line 9

def timestamp
  @timestamp
end

Class Method Details

.gather_info(url) ⇒ Object

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



56
57
58
59
# File 'lib/tickerpicker/price.rb', line 56

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

.get_prices(market) ⇒ Object

Get prices for the market

Parameters

  • market - string

Returns

  • TickerPicker::Price - TickerPicker::Price instance object



30
31
32
# File 'lib/tickerpicker/price.rb', line 30

def get_prices(market)
  instance_mapping gather_info(markets[market]['url']), markets[market]['currency']
end

.instance_mappingObject

Abstact method for mapping foreign data with local instance

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/tickerpicker/price.rb', line 42

def instance_mapping
  raise NotImplementedError.new("#{self.class.name}##{__callee__} method is not implemented!")
end

.marketsObject

Abstact method for list of markets in the stock

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/tickerpicker/price.rb', line 36

def markets
  raise NotImplementedError.new("#{self.class.name}##{__callee__} method is not implemented!")
end

.user_agentObject

nodoc



62
63
64
# File 'lib/tickerpicker/price.rb', line 62

def user_agent
  "TickerPicker Bot v#{TickerPicker::VERSION}"
end