Class: Ytterb::StockSymbol::Stock

Inherits:
Object
  • Object
show all
Defined in:
lib/ytterb/stock_symbol/stock.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Stock

Returns a new instance of Stock.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ytterb/stock_symbol/stock.rb', line 10

def initialize(options={})
  @options = {}
  options.reject {|k,v| !k }.each do |k,v|
    sym_k = k.gsub(/\s+/,"_").downcase.to_sym
    val = v.strip
    val.gsub!(/\^/,"-") if sym_k == :symbol
    @options[sym_k] = val
  end
  # not interested in variable data which was captured in the stock list
  @options.reject! {|k,v| [:lastsale, :marketcap, :adr_tso].include?(k) }
  load
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ytterb/stock_symbol/stock.rb', line 29

def method_missing(meth, *args, &block)
  if @options.has_key?(meth)
    return @options[meth]
  else
    # don't know how to handle it
    super
  end
end

Class Method Details

.builder_from_csv(stock_symbol_file, exchange) ⇒ Object



23
24
25
26
27
# File 'lib/ytterb/stock_symbol/stock.rb', line 23

def self.builder_from_csv(stock_symbol_file, exchange)
  CSV.foreach(stock_symbol_file, :headers => true) do |obj|
    yield Stock.new(obj.to_hash.merge("Exchange" => exchange))
  end
end

.get_settingsObject

load/save operations



43
44
45
# File 'lib/ytterb/stock_symbol/stock.rb', line 43

def self.get_settings
  @settings ||= Util::Settings.new
end

.get_yql_clientObject

history via yql operation



59
60
61
# File 'lib/ytterb/stock_symbol/stock.rb', line 59

def self.get_yql_client
  @yql_client ||= Yql::Client.new
end

Instance Method Details

#fetch_history(start_date = nil, end_date = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ytterb/stock_symbol/stock.rb', line 63

def fetch_history(start_date = nil, end_date = nil)
  @history||={}
  start_date = @history.keys.collect {|x| Date.parse(x)}.sort.last
  start_date = start_date.to_s if start_date
  start_date ||= self.class.get_settings[:sync_from]
    
  self.class.get_yql_client.get_symbol_historical(@options[:symbol], start_date, end_date).each do |line|
    @history[line["Date"]] = line
  end
  save
end

#historyObject



75
76
77
78
# File 'lib/ytterb/stock_symbol/stock.rb', line 75

def history
  return {} unless @history
  @history
end

#loadObject



47
48
49
50
# File 'lib/ytterb/stock_symbol/stock.rb', line 47

def load
  symbol_history_file = self.class.get_settings.get_symbol_store_file(self.symbol)
  @history = Util::DataPersistHelper.load(symbol_history_file) rescue nil
end

#saveObject



52
53
54
55
56
# File 'lib/ytterb/stock_symbol/stock.rb', line 52

def save
  return unless @history
  symbol_history_file = self.class.get_settings.get_symbol_store_file(self.symbol)
  Util::DataPersistHelper.save(symbol_history_file,@history)
end

#to_sObject



38
39
40
# File 'lib/ytterb/stock_symbol/stock.rb', line 38

def to_s
  @options.to_s
end