Skip to content

Commit

Permalink
Got quote import working
Browse files Browse the repository at this point in the history
  • Loading branch information
brymck committed Sep 27, 2011
1 parent 2469869 commit 7b27306
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
18 changes: 13 additions & 5 deletions lib/rupee/import.rb
@@ -1,3 +1,4 @@
require "rupee/import/source"
autoload :Net, "net/http"
autoload :URI, "uri"

Expand All @@ -6,17 +7,24 @@ module Rupee
class Import
class << self
# Retrieves the current price of a security
def quote(url, *options)
def quote(url, *params)
results = {}
params = [:price] if params.empty?
url = URI.parse(url)
res = 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]
end
puts res.body

results
end

# Retrieves the current price of a security from Bloomberg
def bloomberg(ticker, *options)
quote BLOOMBERG_URL % ticker, options
def bloomberg(ticker, *params)
quote BLOOMBERG_URL % ticker, *params
end

private
Expand Down
53 changes: 53 additions & 0 deletions lib/rupee/import/source.rb
@@ -0,0 +1,53 @@
module Rupee
class Import
# A class to hold quote sources
class Source
# The name of the source
attr :name

# The parameters available
attr :params

def initialize(name, aliases = [], params = {})
@name = name
@aliases = aliases
@params = params
end
end

class << self
# A holder for the sources available
attr :sources

# The default source to use when making generic requests
attr :default_source

# Build the default sources that come with Rupee
def build_sources
@sources ||= []

# Bloomberg
@sources << 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,})\%/,
: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 << Source.new(:yahoo)
@sources << Source.new(:google)
@default_source = :bloomberg
end
end

# Initialize sources
Import.build_sources
end
end
2 changes: 1 addition & 1 deletion lib/rupee/version.rb
@@ -1,4 +1,4 @@
module Rupee
# The current version
VERSION = "0.0.6"
VERSION = "0.0.7"
end

0 comments on commit 7b27306

Please sign in to comment.