diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4040c6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.gem +.bundle +Gemfile.lock +pkg/* diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f380dde --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in stock_check.gemspec +gemspec diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/lib/stock_quote.rb b/lib/stock_quote.rb new file mode 100644 index 0000000..9087385 --- /dev/null +++ b/lib/stock_quote.rb @@ -0,0 +1,2 @@ +require "stock_quote/version" +require "stock_quote/stock" diff --git a/lib/stock_quote/stock.rb b/lib/stock_quote/stock.rb new file mode 100644 index 0000000..2445634 --- /dev/null +++ b/lib/stock_quote/stock.rb @@ -0,0 +1,120 @@ +require "rest-client" +require "hpricot" + +module StockQuote + class Stock + + attr_accessor :symbol, :pretty_symbol, :symbol_lookup_url, :company, :exchange, :exchange_timezone, :exchange_utc_offset, :exchange_closing, :divisor, :currency, :last, :high, :low, :volume, :avg_volume, :market_cap, :open, :y_close, :change, :perc_change, :delay, :trade_timestamp, :trade_date_utc, :trade_time_utc, :current_date_utc, :current_time_utc, :symbol_url, :chart_url, :disclaimer_url, :ecn_url, :isld_last, :isld_trade_date_utc, :isld_trade_time_utc, :brut_last, :brut_trade_date_utc, :brut_trade_time_utc, :daylight_savings + + def initialize(symbol, pretty_symbol, symbol_lookup_url, company, exchange, exchange_timezone, exchange_utc_offset, exchange_closing, divisor, currency, last, high, low, volume, avg_volume, market_cap, open, y_close, change, perc_change, delay, trade_timestamp, trade_date_utc, trade_time_utc, current_date_utc, current_time_utc, symbol_url, chart_url, disclaimer_url, ecn_url, isld_last, isld_trade_date_utc, isld_trade_time_utc, brut_last, brut_trade_date_utc, brut_trade_time_utc, daylight_savings) + # Instance variables + @symbol = symbol + @pretty_symbol = pretty_symbol + @symbol_lookup_url = symbol_lookup_url + @company = company + @exchange = exchange + @exchange_timezone = exchange_timezone + @exchange_utc_offset = exchange_utc_offset + @exchange_closing = exchange_closing + @divisor = divisor + @currency = currency + @last = last + @high = high + @low = low + @volume = volume + @avg_volume = avg_volume + @market_cap = market_cap + @open = open + @y_close = y_close + @change = change + @perc_change = perc_change + @delay = delay + @trade_timestamp = trade_timestamp + @trade_date_utc = trade_date_utc + @trade_time_utc = trade_time_utc + @current_date_utc = current_date_utc + @current_time_utc = current_time_utc + @symbol_url = symbol_url + @chart_url = chart_url + @disclaimer_url = disclaimer_url + @ecn_url = ecn_url + @isld_last = isld_last + @isld_trade_date_utc = isld_trade_date_utc + @isld_trade_time_utc = isld_trade_time_utc + @brut_last = brut_last + @brut_trade_date_utc = brut_trade_date_utc + @brut_trade_time_utc = brut_trade_time_utc + @daylight_savings = daylight_savings + + end + + def self.find(symbol) + url = "http://www.google.com/ig/api?" + query =[] + symbols = symbol.gsub(" ", "").split(",") + for s in symbols + query << "stock="+s + end + RestClient.get(url+query.join("&")){ |response, request, result, &block| + case response.code + when 200 + self.parse(response) + else + response.return!(request, result, &block) + end + } + end + def self.parse(xml) + doc = Hpricot::XML(xml) + data = doc.search(:finance); + results = [] + for d in data + stock=Stock.new(d.search(:symbol).first.attributes['data'], + d.search(:pretty_symbol).first.attributes['data'], + d.search(:symbol_lookup_url).first.attributes['data'], + d.search(:company).first.attributes['data'], + d.search(:exchange).first.attributes['data'], + d.search(:exchange_timezone).first.attributes['data'], + d.search(:exchange_utc_offset).first.attributes['data'], + d.search(:exchange_closing).first.attributes['data'], + d.search(:divisor).first.attributes['data'], + d.search(:currency).first.attributes['data'], + d.search(:last).first.attributes['data'], + d.search(:high).first.attributes['data'], + d.search(:low).first.attributes['data'], + d.search(:volume).first.attributes['data'], + d.search(:avg_volume).first.attributes['data'], + d.search(:market_cap).first.attributes['data'], + d.search(:open).first.attributes['data'], + d.search(:y_close).first.attributes['data'], + d.search(:change).first.attributes['data'], + d.search(:perc_change).first.attributes['data'], + d.search(:delay).first.attributes['data'], + d.search(:trade_timestamp).first.attributes['data'], + d.search(:trade_date_utc).first.attributes['data'], + d.search(:trade_time_utc).first.attributes['data'], + d.search(:current_date_utc).first.attributes['data'], + d.search(:current_time_utc).first.attributes['data'], + d.search(:symbol_url).first.attributes['data'], + d.search(:chart_url).first.attributes['data'], + d.search(:disclaimer_url).first.attributes['data'], + d.search(:ecn_url).first.attributes['data'], + d.search(:isld_last).first.attributes['data'], + d.search(:isld_trade_date_utc).first.attributes['data'], + d.search(:isld_trade_time_utc).first.attributes['data'], + d.search(:brut_last).first.attributes['data'], + d.search(:brut_trade_date_utc).first.attributes['data'], + d.search(:brut_trade_time_utc).first.attributes['data'], + d.search(:daylight_savings).first.attributes['data'] + ) + if data.count ==1 + results = stock; + else + results << stock; + end + end + return results; + end + end +end + diff --git a/lib/stock_quote/version.rb b/lib/stock_quote/version.rb new file mode 100644 index 0000000..3007f0c --- /dev/null +++ b/lib/stock_quote/version.rb @@ -0,0 +1,3 @@ +module StockQuote + VERSION = "1.0.0" +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..6777165 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require 'stock_quote' diff --git a/spec/stock_quote_spec.rb b/spec/stock_quote_spec.rb new file mode 100644 index 0000000..37d5b3a --- /dev/null +++ b/spec/stock_quote_spec.rb @@ -0,0 +1,13 @@ +require "stock_quote" + +describe StockQuote::Stock do + companies = [["aapl", "Apple, Inc."], ["goog", "Google Inc."]] + for c in companies + puts c[0].to_s+" is company "+c[1].to_s + symbol = c[0] + stock = StockQuote::Stock.find(symbol) + it c[0].to_s+" is company "+c[1].to_s do + stock.company.should eql(c[1]) + end + end +end diff --git a/stock_quote.gemspec b/stock_quote.gemspec new file mode 100644 index 0000000..a814121 --- /dev/null +++ b/stock_quote.gemspec @@ -0,0 +1,24 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "stock_quote/version" + +Gem::Specification.new do |s| + s.name = "stock_quote" + s.version = StockQuote::VERSION + s.authors = ["Ty Rauber"] + s.email = ["tyrauber@mac.com"] + s.homepage = "https://github.com/tyrauber/stock_quote" + s.summary = "A ruby gem that retrieves real-time stock quotes from google." + s.description = "Retrieve up to 100 stock quotes per query with the following variables - symbol, pretty_symbol, symbol_lookup_url, company, exchange, exchange_timezone, exchange_utc_offset, exchange_closing, divisor, currency, last, high, low, volume, avg_volume, market_cap, open, y_close, change, perc_change, delay, trade_timestamp, trade_date_utc, trade_time_utc, current_date_utc, current_time_utc, symbol_url, chart_url, disclaimer_url, ecn_url, isld_last, isld_trade_date_utc, isld_trade_time_utc, brut_last, brut_trade_date_utc, brut_trade_time_utc and daylight_savings - per stock.)" + s.rubyforge_project = "stock_quote" + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + # specify any dependencies here; for example: + s.add_development_dependency "rspec" + s.add_runtime_dependency "rest-client" + s.add_runtime_dependency "hpricot" +end