Skip to content

Commit

Permalink
Committing the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrauber committed Nov 17, 2011
1 parent 6315346 commit e9776fa
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in stock_check.gemspec
gemspec
1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require "bundler/gem_tasks"
2 changes: 2 additions & 0 deletions lib/stock_quote.rb
@@ -0,0 +1,2 @@
require "stock_quote/version"
require "stock_quote/stock"
120 changes: 120 additions & 0 deletions 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

3 changes: 3 additions & 0 deletions lib/stock_quote/version.rb
@@ -0,0 +1,3 @@
module StockQuote
VERSION = "1.0.0"
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -0,0 +1 @@
require 'stock_quote'
13 changes: 13 additions & 0 deletions 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
24 changes: 24 additions & 0 deletions 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

0 comments on commit e9776fa

Please sign in to comment.