Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.gem | ||
.bundle | ||
Gemfile.lock | ||
pkg/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in stock_check.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "stock_quote/version" | ||
require "stock_quote/stock" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module StockQuote | ||
VERSION = "1.0.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'stock_quote' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |