Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Much better structure for quotes

  • Loading branch information...
commit def947a3c2cd2faddc5273603a452fdc3710c5aa 1 parent c0f79ae
@brymck authored
View
0  .autotest 100755 → 100644
File mode changed
View
1  .gitignore
@@ -6,3 +6,4 @@ Gemfile.lock
doc
pkg
tmp
+wiki
View
0  .rspec 100755 → 100644
File mode changed
View
15 README.md
@@ -72,7 +72,20 @@ Fargo using the following (note that you only need to `require` the `quote`
module):
require "rupee/quote"
- Rupee::Quote.bloomberg "WFC", :price, :change, :pct_change
+ wfc = Rupee::Quote.new("WFC")
+
+ wfc.get :price, :change, :pct_change
+ #=> {:price=>24.96, :change=>0.17, :pct_change =>0.686}
+
+ wfc.price
+ #=> 24.96
+
+ wfc.change
+ #=> 0.17
+
+`wfc.get` will return a hash of the requested information for the security.
+Each valid parameter will also have its own utility method. The results will
+update every `wfc.frequency` seconds (defaults to 15).
Got it? Good. This will surely help you collect some rupees in real life.
View
93 lib/rupee/quote.rb
@@ -5,43 +5,86 @@
module Rupee
# The quote and data import functionality in Rupee
class Quote
- class << self
- # Retrieves the current price of a security
- def get(url, *params)
- results = {}
- params = [:price] if params.empty?
+ # A ticker symbol
+ attr_accessor :ticker
+
+ # The name of the quote source
+ attr_accessor :source
+
+ # The cached HTML
+ attr :html
+
+ attr_accessor :frequency
+
+ attr :next_pull
+
+ def initialize(ticker, opts = {})
+ opts = { :source => :bloomberg, :frequency => 15 }.merge opts
+ @ticker = ticker
+ @source = Quote.sources[opts[:source]]
+ @frequency = opts[:frequency]
+ @next_pull = Time.now
+ end
+
+ # Retrieves the current price of a security
+ def get(*params)
+ now = Time.now
+ params = [:price] if params.empty?
+
+ if now >= @next_pull
+ @next_pull = now + @frequency
+ url = BLOOMBERG_URL % ticker
+ @results = {}
url = URI.parse(url)
- html = Net::HTTP.start(url.host, url.port) do |http|
+ @html = Net::HTTP.start(url.host, url.port) do |http|
http.get url.request_uri
end.body
- params.each do |p|
- results[p] = @sources[0].params[p].match(html)[1]
+ @source.params.each do |param, regex|
+ begin
+ @results[param] = parse(regex.match(html)[1])
+ rescue
+ @results[param] = nil
+ end
end
+ end
- results
+ if params.length == 1
+ @results[params[0]]
+ else
+ @results.keep_if { |r| params.include?(r) }
end
+ end
- # Retrieves the current price of a security from Bloomberg
- def bloomberg(ticker, *params)
- get BLOOMBERG_URL % ticker, *params
+ [:price, :change, :pct_change, :date, :time, :bid, :ask, :open, :high,
+ :low, :volume, :mkt_cap, :p_e].each do |method_name|
+ define_method method_name do
+ get method_name
end
+ end
- private
+ private
- # The URL for Bloomberg's quotes service
- BLOOMBERG_URL = "http://www.bloomberg.com/apps/quote?ticker=%s"
+ # The URL for Bloomberg's quotes service
+ BLOOMBERG_URL = "http://www.bloomberg.com/apps/quote?ticker=%s"
- # Returns an intepretation of an abbreviated source name
- def shorten_source(source)
- case source.downcase.to_sym
- when :"", :bloomberg, :bberg, :bb, :b
- :bloomberg
- when :google, :goog, :g
- :google
- when :yahoo!, :yahoo, :yhoo, :y!, :y
- :yahoo
- end
+ # Returns an intepretation of an abbreviated source name
+ def shorten_source(source)
+ case source.downcase.to_sym
+ when :"", :bloomberg, :bberg, :bb, :b
+ :bloomberg
+ when :google, :goog, :g
+ :google
+ when :yahoo!, :yahoo, :yhoo, :y!, :y
+ :yahoo
+ end
+ end
+
+ def parse(result)
+ begin
+ Float(result)
+ rescue
+ result
end
end
end
View
8 lib/rupee/quote/source.rb
@@ -24,10 +24,10 @@ class << self
# Build the default sources that come with Rupee
def build_sources
- @sources ||= []
+ @sources ||= {}
# Bloomberg
- @sources << Source.new(:bloomberg, [:bberg, :bb, :b],
+ @sources[:bloomberg] = Source.new(:bloomberg, [:bberg, :bb, :b],
: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,})\%/,
@@ -41,8 +41,8 @@ def build_sources
: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 << Source.new(:yahoo)
- @sources << Source.new(:google)
+ @sources[:yahoo] = Source.new(:yahoo)
+ @sources[:google] = Source.new(:google)
@default_source = :bloomberg
end
end
View
2  lib/rupee/version.rb
@@ -1,4 +1,4 @@
module Rupee
# The current version
- VERSION = "0.1.1"
+ VERSION = "0.1.2"
end
View
8 spec/import/quote_spec.rb
@@ -2,21 +2,21 @@
describe Rupee::Quote do
it "should automatically have a Bloomberg source" do
- Rupee::Quote.sources[0].name.should == :bloomberg
+ Rupee::Quote.sources.should include :bloomberg
end
describe "when pulling quotes" do
describe "without any parameters specified" do
before :each do
- @wfc = Rupee::Quote.bloomberg("WFC")
+ @wfc = Rupee::Quote.new("WFC")
end
it "should default to pulling the price" do
- @wfc.should include :price
+ @wfc.get.should == @wfc.price
end
it "should return a price" do
- @wfc[:price].to_f.should be_a_kind_of Float
+ @wfc.price.should be_a_kind_of Float
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.