Module: YahooFinance

Defined in:
lib/yahoo_finance/stock.rb,
lib/yahoo_finance/version.rb,
lib/yahoo_finance/company_events.rb,
lib/yahoo_finance/key_statistics.rb,
lib/yahoo_finance/analyst_opinion.rb,
lib/yahoo_finance/company_profile.rb,
lib/yahoo_finance/analyst_estimates.rb,
lib/yahoo_finance/financial_statement.rb

Defined Under Namespace

Modules: AnalystEstimates, AnalystOpinion, CompanyEvents, CompanyProfile, FinancialStatement, KeyStatistics Classes: Stock, StockHistory

Constant Summary collapse

AVL_FIELDS =
{
  :YAHOO_STOCK_FIELDS => YahooStock::Quote.new(:stock_symbols => ['AAPL']).valid_parameters,
  :KEY_STATISTICS => YahooFinance::KeyStatistics.key_stats_available,
  :COMPANY_EVENTS => YahooFinance::CompanyEvents.key_events_available,
  :ANALYST_OPINION => YahooFinance::AnalystOpinion.key_events_available,
  :ANALYST_ESTIMATES => YahooFinance::AnalystEstimates.key_events_available,
  :COMPANY_PROFILE => YahooFinance::CompanyProfile.key_events_available
}
DRIVERS =
{
  :KEY_STATISTICS => YahooFinance::KeyStatistics::StatsPage,
  :COMPANY_EVENTS => YahooFinance::CompanyEvents::CompanyEventsPage,
  :ANALYST_OPINION => YahooFinance::AnalystOpinion::AnalystOpinionPage,
  :ANALYST_ESTIMATES => YahooFinance::AnalystEstimates::AnalystEstimatesPage,
  :COMPANY_PROFILE => YahooFinance::CompanyProfile::CompanyProfilePage
}
VERSION =
"1.0.2"

Class Method Summary collapse

Class Method Details

.parse_financial_statement_field(aField, multiplier = 1000.0) ⇒ Object

this really parses numeric fields only



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/yahoo_finance/stock.rb', line 100

def YahooFinance.parse_financial_statement_field aField, multiplier = 1000.0
  aField = aField.strip
  
  multiplier = 1000.00 if !multiplier   # just in case a nil is forced...
  
  if aField.match /^([\d\,])*(\.[\d]+)*$/
    # it's a number as far as we care; let's strip the commas....
    return aField.tr('\,', '').to_f * multiplier
  end
  
  # process the accounting negative field -- parentheses
  m = aField.match /^\(([\d\,])*(\.[\d]+)*\)$/
  if m
    # it's a number as far as we care; let's strip the commas....
    aField = aField.tr('\(\)\,', '')
    return aField.to_f * multiplier * -1.0
  end
  
  # anything else becomes zero by definition
  return 0.0
  
end

.parse_yahoo_field(aField) ⇒ Object

We are not interested parsing every type of field Key Fields: number (that may include , for 000s separator, NOT SUPPORTED YET), scaled number

percentage, date


30
31
32
33
34
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
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/yahoo_finance/stock.rb', line 30

def YahooFinance.parse_yahoo_field aField
  # we really need to oarse numbers, dates, and strings %%% STUB ALERT %%%%

  aField.strip!
  
  if aField.match /^([\-\+]{,1})([\d\,])*(\.[\d]+)*$/
    # it's a number as far as we care; let's strip the commas....
    aField.gsub! /\,/, ''
  end

  m=aField.match  /^([\-\+]{0,1})([\d]*)(\.[\d]{1,})([KMB\%]{0,1})$/
  if m # && (m.size == 3)
    num = ((m[1] || "")+(m[2] || "")+(m[3] || "")).to_f

    case m[4]
    when "K" then
      num *= 1000
      # puts "NUM got K = #{num.to_s}"
    when "M" then
      num *= 1000000
      # puts "NUM got B = #{num.to_s}"
    when "B" then
      num *= 1000000000
      # puts "NUM got B = #{num.to_s}"
    when '%' then
      num /= 100
      # puts "NUM got % = #{num.to_s}"
    end
    return num
  end

  if aField.match /^([\-\+]*[0-9]*)(\.[0-9]*)*$/
    # simple number
    return aField.to_f
  end
  
  
  m=aField.match /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\s]+([\d]{1,2})\,[\s]*([\d]{4})$/
  if m
    # got a date
    dt = nil
    begin
      dt = Date.strptime aField, '%b %d, %Y'
    rescue
    end
    return dt if dt
  end
  
  m=aField.match /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\s]+([\d]{1,2})\s*\-\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\s]+([\d]{1,2})\,[\s]*([\d]{4})/
  if m && (m.size == 6)
    # we found it, and it is a date range
    dt1 = dt2 = nil
    begin
      dt1 = Date.strptime "#{m[1]} #{m[2]}, #{m[5]}", '%b %d, %Y'
      dt2 = Date.strptime "#{m[3]} #{m[4]}, #{m[5]}", '%b %d, %Y'
    rescue
    end
    return [dt1, dt2]
  end
  
  #else make an attempt to parse as date -- here, we have to assume that the date is in American format, as Yahoo Quote returns for example
  dt = nil
  begin; dt = Date.strptime(aField, "%m/%d/%Y"); return dt if dt; rescue; end
  # but if this fails, we make a last ditch effort to let ruby library parse the date using other std formats
  begin; dt = Date.parse(aField); return dt if dt; rescue; end
  
  aField
end