Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Got quote import working

  • Loading branch information...
commit 7b27306865685e98b775853bac074c2b68a8c209 1 parent 2469869
@brymck authored
View
18 lib/rupee/import.rb
@@ -1,3 +1,4 @@
+require "rupee/import/source"
autoload :Net, "net/http"
autoload :URI, "uri"
@@ -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
View
53 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
View
2  lib/rupee/version.rb
@@ -1,4 +1,4 @@
module Rupee
# The current version
- VERSION = "0.0.6"
+ VERSION = "0.0.7"
end
Please sign in to comment.
Something went wrong with that request. Please try again.