diff --git a/lib/rupee/import.rb b/lib/rupee/import.rb index 1ba8333..782881a 100755 --- a/lib/rupee/import.rb +++ b/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 diff --git a/lib/rupee/import/source.rb b/lib/rupee/import/source.rb new file mode 100755 index 0000000..a7cc0d8 --- /dev/null +++ b/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): ([0-9.,NA-]{1,})/, + :change => /Change<\/td>\n]+>[0-9.,NA-]{1,} \(([0-9NA.,-]{1,})\%/, + :date => /"date">(.*?) /"time">(.*?) /Bid<\/td>\n]+>([0-9.,NA-]{1,})/, + :open => /Open<\/td>\n]+>([0-9.,NA-]{1,})/, + :low => /Low<\/td>\n]+>([0-9.,NA-]{1,})/, + :mkt_cap => /Market Cap[^<]+<\/td>\n([0-9.,NA-]{1,})/, + :p_e => /Price\/Earnings[^<]+<\/td>\n([0-9.,NA-]{1,})/) + @sources << Source.new(:yahoo) + @sources << Source.new(:google) + @default_source = :bloomberg + end + end + + # Initialize sources + Import.build_sources + end +end diff --git a/lib/rupee/version.rb b/lib/rupee/version.rb index f632939..2d98dbf 100644 --- a/lib/rupee/version.rb +++ b/lib/rupee/version.rb @@ -1,4 +1,4 @@ module Rupee # The current version - VERSION = "0.0.6" + VERSION = "0.0.7" end