Class: YahooFinance::KeyStatistics::StatsPage

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo_finance/key_statistics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol = nil) ⇒ StatsPage

Returns a new instance of StatsPage.



83
84
85
# File 'lib/yahoo_finance/key_statistics.rb', line 83

def initialize symbol=nil
  @symbol = symbol
end

Instance Attribute Details

#symbolObject

Returns the value of attribute symbol.



78
79
80
# File 'lib/yahoo_finance/key_statistics.rb', line 78

def symbol
  @symbol
end

Instance Method Details

#fetchObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/yahoo_finance/key_statistics.rb', line 87

def fetch
  url = "http://finance.yahoo.com/q/ks?s=#{@symbol}"
  begin
    tries ||= 3
    open(url) do |stream|
      doc = Nokogiri::HTML(stream)
      # puts "DATA IS: #{data}"
      @page_keys = doc.xpath('//td[@class="yfnc_tablehead1"]')
      @page_values = doc.xpath('//td[@class="yfnc_tabledata1"]')
    end
  rescue Exception => e
    puts "Failed to open and parse key stats for #{@symbol} because of #{e.message}. #{(tries > 0) ? 'retrying' : ''}"
    if (tries -= 1) > 0
      retry
    end
  end
end

#value_for(key_stat) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/yahoo_finance/key_statistics.rb', line 105

def value_for key_stat
  return nil if !@page_keys

  matchstr = "#{AVL_KEY_STATS[key_stat][0]}"
  @page_keys.each_with_index do |key, i|
    if key.text.match(/^#{AVL_KEY_STATS[key_stat][0]}/)
      value = YahooFinance.parse_yahoo_field @page_values[i].text.to_s
      if AVL_KEY_STATS[key_stat][2]
        value *= AVL_KEY_STATS[key_stat][2]
      end
      return value
    end
  end
  return nil
end