Skip to content

Commit

Permalink
Got Google and Yahoo! quote services working
Browse files Browse the repository at this point in the history
  • Loading branch information
brymck committed Sep 29, 2011
1 parent a13290a commit 436a47a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.rdoc
Expand Up @@ -57,8 +57,8 @@ Fargo using the following (note that you only need to <tt>require</tt> the
require "rupee/quote"
wfc = Rupee::Quote.new("WFC")

wfc.get :price, :change, :pct_change
#=> {:price=>24.96, :change=>0.17, :pct_change =>0.686}
wfc.get :price, :change, :pct_chg
#=> {:price=>24.96, :change=>0.17, :pct_chg =>0.686}

wfc.price
#=> 24.96
Expand Down
2 changes: 1 addition & 1 deletion lib/rupee/quote.rb
Expand Up @@ -106,7 +106,7 @@ def frequency=(x) # :nodoc:
# parse "abc" #=> "abc"
def parse(result)
begin
Float(result)
Float(result.gsub /,/, "")
rescue
result
end
Expand Down
55 changes: 42 additions & 13 deletions lib/rupee/quote/source.rb
Expand Up @@ -29,21 +29,50 @@ def build_sources
# Bloomberg
@sources[:bloomberg] = Source.new(:bloomberg,
"http://www.bloomberg.com/apps/quote?ticker=%s",
:price => /(?:PRICE|VALUE): <span class="amount">([0-9.,NA-]{1,})/,
:change => /Change<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:pct_change => /Change<\/td>\n<td class="value[^>]+>[0-9.,NA-]{1,} \(([0-9NA.,-]{1,})\%/,
:date => /"date">(.*?)</,
:time => /"time">(.*?)</,
:bid => /Bid<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:ask => /Ask<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:open => /Open<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:high => /High<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:low => /Low<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:volume => /Volume<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:price => /(?:PRICE|VALUE): <span class="amount">([0-9.,NA-]{1,})/,
:change => /Change<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:pct_chg => /Change<\/td>\n<td class="value[^>]+>[0-9.,NA-]{1,} \(([0-9NA.,-]{1,})\%/,
:date => /"date">(.*?)</,
:time => /"time">(.*?)</,
:bid => /Bid<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:ask => /Ask<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:open => /Open<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:high => /High<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:low => /Low<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:volume => /Volume<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
:mkt_cap => /Market Cap[^<]+<\/td>\n<td class="value">([0-9.,NA-]{1,})/,
:p_e => /Price\/Earnings[^<]+<\/td>\n<td class="value">([0-9.,NA-]{1,})/)
@sources[:yahoo] = Source.new(:yahoo, "yahoo.com")
@sources[:google] = Source.new(:google, "google.com")

# Yahoo! Finance
@sources[: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>)/,
: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,})/,
:mkt_cap => /yfs_j10_[^>]+>([0-9,.-]{1,}[KMBT]?)/)

# Google Finance
@sources[:google] = Source.new(:google,
"http://www.google.com/ig/api?stock=%s",
:price => /<last 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.-]*)"/,
:open => /<open data="([0-9.-]*)"/,
:high => /<high data="([0-9.-]*)"/,
:low => /<low data="([0-9.-]*)"/,
:volume => /<volume data="([0-9.-]*)"/,
:mkt_cap => /<market_cap data="([0-9.-]*)"/)

@default_source = :bloomberg
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rupee/version.rb
@@ -1,4 +1,4 @@
module Rupee
# The current version
VERSION = "0.1.9"
VERSION = "0.2.0"
end
24 changes: 24 additions & 0 deletions spec/import/quote_spec.rb → spec/ruby/quote_spec.rb
Expand Up @@ -27,5 +27,29 @@
@wfc.next_pull.should == orig_pull - freq_change
end
end

describe "specifying Google Finance as the quote service" do
before :each do
@goog = Quote.new("GOOG", :source => :google)
end

it "should have around the same price as Bloomberg" do
bb_price = Quote.new("GOOG").price
bb_price.should be_a_kind_of Float
@goog.price.should be_within(0.05).of bb_price
end
end

describe "specifying Yahoo! Finance as the quote service" do
before :each do
@yahoo = Quote.new("YHOO", :source => :yahoo)
end

it "should have around the same price as Bloomberg" do
bb_price = Quote.new("YHOO").price
bb_price.should be_a_kind_of Float
@yahoo.price.should be_within(0.05).of bb_price
end
end
end
end

0 comments on commit 436a47a

Please sign in to comment.