Class: YahooFinance::AnalystOpinion::AnalystOpinionPage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol = nil) ⇒ AnalystOpinionPage

Returns a new instance of AnalystOpinionPage.



24
25
26
# File 'lib/yahoo_finance/analyst_opinion.rb', line 24

def initialize symbol=nil
  @symbol = symbol
end

Instance Attribute Details

#symbolObject

Returns the value of attribute symbol.



22
23
24
# File 'lib/yahoo_finance/analyst_opinion.rb', line 22

def symbol
  @symbol
end

Instance Method Details

#fetchObject



28
29
30
31
32
33
# File 'lib/yahoo_finance/analyst_opinion.rb', line 28

def fetch
  url = "http://finance.yahoo.com/q/ao?s=#{@symbol}"
  open(url) do |stream|
    @doc = Nokogiri::HTML(stream)
  end
end

#value_for(key_stat) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/yahoo_finance/analyst_opinion.rb', line 35

def value_for key_stat
  begin
    if key_stat == :upgrades_downgrades_history
      ret = []
      tbl = @doc.xpath("//th[text() = 'Upgrades & Downgrades History']")[0].parent.parent.parent.children[1].xpath("tr")
      for i in 1..(tbl.size-1) do
        r = {}
        r[:date] = YahooFinance.parse_yahoo_field(tbl[i].children[0].text)
        r[:firm] = tbl[i].children[1].text
        r[:action] = tbl[i].children[2].text
        r[:from]  = tbl[i].children[3].text
        r[:to] = tbl[i].children[4].text
        ret << r
      end
      return ret
    end
    if key_stat == :recommendation_trends
      ret = {}
      tbl1 = @doc.xpath("//th[text() = 'Recommendation Trends']")[0].parent.parent
      elem = tbl1.parent
      sibling_idx = elem.children.index(tbl1)+1
      sibling = elem.children[sibling_idx]
      # now get all the subnodes...
      table_rows = sibling.xpath(".//table//tr")  # really contained within a subtable
      for i in 1..(table_rows.size-1) do
        r = table_rows[i]
        header = r.xpath(".//th")
        symbol = header[0].text.gsub(' ','').to_sym
        cells = r.xpath(".//td")
        ar = []
        cells.each do |cell|
          begin
            ar << YahooFinance.parse_yahoo_field(cell.text)
          rescue
            puts " COULD NOT PARSE NUMERIC TREND CELL"
            ar << " "
          end
        end
        ret[symbol] = ar
        # THIS IS NOT WORKING YET. THE LOGIC IS FALSE
      end
      return ret
    end
    if [:upgrades_downgrades_history, :recommendation_trends].include?(key_stat) == false
      value = @doc.xpath("//td[text() = '#{AVL_KEY_STATS[key_stat][0]}']")[0].parent.children[1].text
      return YahooFinance.parse_yahoo_field(value)
    end
  rescue
  end
  return nil
end