Permalink
Browse files

Update to account for recent changes to Yahoo

  • Loading branch information...
1 parent 7839581 commit ae2e55228c8867b74f6ff00eca92d7c7a429f1d6 @brymck committed Feb 15, 2012
Showing with 38 additions and 17 deletions.
  1. +8 −8 lib/rupee/quote.rb
  2. +1 −1 lib/rupee/source/google.rb
  3. +6 −6 lib/rupee/source/yahoo.rb
  4. +1 −1 lib/rupee/version.rb
  5. +22 −1 spec/ruby/quote_spec.rb
View
@@ -72,9 +72,9 @@ def get(*params)
case res
when Net::HTTPSuccess
@source.params.each do |param, regex|
- begin
- @results[param] = parse(regex.match(res.body)[1])
- rescue
+ if res.body =~ regex
+ @results[param] = parse($1)
+ else
@results[param] = nil
end
end
@@ -146,11 +146,11 @@ def diff(x, y)
# parse "15" # => 15
# parse "abc" # => "abc"
def parse(result)
- begin
- Float(result.gsub /,/, "")
- rescue
- result
- end
+ return 0.0 if result =~ /N\.A\./
+
+ Float(result.gsub /,/, "")
+ rescue
+ result
end
# Scans the values provided for the number with the greatest number of
@@ -4,7 +4,7 @@ class Source
GOOGLE = Source.new(:google,
"http://www.google.com/ig/api?stock=%s",
:price => /<last data="([0-9.-]*)"/,
- :change => /<change data="([0-9.-]*)"/,
+ :change => /<change data="\+?([0-9.-]*)"/,
:pct_chg => /<perc_change data="([0-9.-]*)"/,
:date => /<trade_date_utc data="([0-9.-]*)"/,
:time => /<trade_date_utc data="([0-9.-]*)"/,
View
@@ -2,17 +2,17 @@ module Rupee
class Source
YAHOO = Source.new(:yahoo,
"http://finance.yahoo.com/q?s=%s",
- :price => /(?:<\/a> |"yfnc_tabledata1"><big><b>)<span id="yfs_l[19]0_[^>]+>([0-9,.-]{1,})/,
- :change => /><span id="yfs_c[16]0_.*?(?:\n[\s\w\-]*\n[\s">\(]*?)?([0-9.,-]{1,})/,
- :pct_chg => /yfs_p[24]0_.*?(?:\n[\s\w\-]*\n[\s">\(]*?)?([0-9.,-]{1,})%\)(?:<\/span>|<\/b><\/span><\/td>)/,
+ :price => /yfs_l84_[^>]+>([0-9,.-]+)/,
+ :change => /yfs_c6[34]_[^>]+>(?:<img[^>]+>)\s*([0-9,.-])+/,
+ :pct_chg => /yfs_p4[34]_[^>]+>\(([0-9,.-]+)%\)/,
:date => /<span id="yfs_market_time">.*?, (.*?20[0-9]{1,2})/,
:time => /(?:"time"|"yfnc_tabledata1")><span id="yfs_t[51]0_[^>]+>(.*?)</,
:bid => /yfs_b00_[^>]+>([0-9,.-]{1,})/,
:ask => /yfs_a00_[^>]+>([0-9,.-]{1,})/,
:open => /Open:<\/th><td class="yfnc_tabledata1">([0-9,.-]{1,})/,
- :high => /yfs_h00_[^>]+>([0-9,.-]{1,})/,
- :low => /yfs_g00_[^>]+>([0-9,.-]{1,})/,
- :volume => /yfs_v00_[^>]+>([0-9,.-]{1,})/,
+ :high => /yfs_h53_[^>]+>([0-9,.-]{1,})/,
+ :low => /yfs_g53_[^>]+>([0-9,.-]{1,})/,
+ :volume => /yfs_v53_[^>]+>([0-9,.-]{1,})/,
:mkt_cap => /yfs_j10_[^>]+>([0-9,.-]{1,}[KMBT]?)/)
end
end
View
@@ -1,4 +1,4 @@
module Rupee
# The current version
- VERSION = "0.2.8"
+ VERSION = "0.2.9"
end
View
@@ -32,7 +32,7 @@ def run_if_connected(&block)
[:change, :pct_chg].each do |param|
it "should have a #{param}" do
run_if_connected do
- @wfc.price.should be_a_kind_of Float
+ @wfc.send(param).should be_a_kind_of Float
end
end
end
@@ -58,6 +58,17 @@ def run_if_connected(&block)
@goog.price.should be_within(10).of bb_price
end
end
+
+ [:change, :pct_chg].each do |param|
+ it "should have around the same #{param} as Bloomberg" do
+ run_if_connected do
+ bb_result = Quote.new("GOOG").send(param)
+ bb_result.should be_a_kind_of Float
+ @goog.send(param).should be_within(10).of bb_result
+ end
+ end
+ end
+
end
describe "specifying Yahoo! Finance as the quote service" do
@@ -72,5 +83,15 @@ def run_if_connected(&block)
@yahoo.price.should be_within(1).of bb_price
end
end
+
+ [:change, :pct_chg].each do |param|
+ it "should have around the same #{param} as Bloomberg" do
+ run_if_connected do
+ bb_result = Quote.new("YHOO").send(param)
+ bb_result.should be_a_kind_of Float
+ @yahoo.send(param).should be_within(10).of bb_result
+ end
+ end
+ end
end
end

0 comments on commit ae2e552

Please sign in to comment.