brm_finance 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15a4372351dd4190d42d0cad2c8f9818329630b8
4
- data.tar.gz: 1a141cd7f63b0e31269ac21b5fb30492627b78b1
3
+ metadata.gz: 4c62e2bb9375c13be790efcc0bc5c1b9195c5050
4
+ data.tar.gz: 53ba9ca7ca4d234366111a5081fed99070c262f6
5
5
  SHA512:
6
- metadata.gz: 3f5f44b12c69d3f162ae1ab95d37b3c655581f3326f0aa78338736d96b39109796736bfd9e7fa257228e5b2505085f7a00a82627cdfd3ddd60e93651ebfe9a81
7
- data.tar.gz: d268ae7b9023627ebc5cbe98c4f31072839c8347c8dda63c7d2003fd564006240f912eca7b142ed43047699a30b2d1e8cb8cbb0de23f2f2b21162e19cc35b015
6
+ metadata.gz: 13923f0054942dfe1479d44c16124bedf26a6624ce083e622cef9dd126ea68303aa9bd8e3ecafdded831f9d08623211493ee8aedbd57b75620ab5b5393f367a6
7
+ data.tar.gz: 774b119e2f3a1502966cba9830b177cd09677daff44256388894f7651a66ddaf31c8054c728722945719ae995d4dcdd01d99797c986c1cf4880a489cf83bfa92
data/.DS_Store ADDED
Binary file
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in stock_check.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Ty Rauber
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
Binary file
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "brm_finance/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "brm_finance"
7
+ s.version = BrmFinance::VERSION
8
+ s.authors = ["Alex Adamson, Ty Rauber"]
9
+ s.email = ["aadamson@stanford.edu"]
10
+ s.homepage = "https://github.com/aadamson/brm_finance"
11
+ s.summary = "A ruby gem that retrieves real-time stock quotes from google."
12
+ 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. Extended for use with keystats and stocks Yahoo tables. Will upgrade it for use with Yahoo options tables soon."
13
+ #s.rubyforge_project = "brm_finance"
14
+
15
+ s.files = Dir["README.md","Gemfile","Rakefile", "spec/*", "lib/**/*"]
16
+ s.files += `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "rest-client"
24
+ s.add_runtime_dependency "json"
25
+ s.add_development_dependency 'fakeweb'
26
+ s.add_development_dependency 'vcr'
27
+ end
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,4 @@
1
+ require "rubygems"
2
+ require "brm_finance/utility"
3
+ require "brm_finance/version"
4
+ require "brm_finance/stock"
@@ -0,0 +1,145 @@
1
+ require "rubygems"
2
+ require "rest-client"
3
+ require "json"
4
+ require "date"
5
+ require 'csv'
6
+
7
+ module BrmFinance
8
+ class Stock
9
+ @@fields = ["symbol", "Ask","AverageDailyVolume", "Bid", "AskRealtime", "BidRealtime",
10
+ "BookValue", "Change_PercentChange","Change", "Commission", "ChangeRealtime",
11
+ "AfterHoursChangeRealtime", "DividendShare","LastTradeDate","TradeDate",
12
+ "EarningsShare", "ErrorIndicationreturnedforsymbolchangedinvalid",
13
+ "EPSEstimateCurrentYear", "EPSEstimateNextYear", "EPSEstimateNextQuarter",
14
+ "DaysLow","DaysHigh","YearLow", "YearHigh", "HoldingsGainPercent",
15
+ "AnnualizedGain","HoldingsGain", "HoldingsGainPercentRealtime",
16
+ "HoldingsGainRealtime", "MoreInfo", "OrderBookRealtime", "MarketCapitalization",
17
+ "MarketCapRealtime", "EBITDA", "ChangeFromYearLow", "PercentChangeFromYearLow",
18
+ "LastTradeRealtimeWithTime", "ChangePercentRealtime","ChangeFromYearHigh",
19
+ "PercebtChangeFromYearHigh", "LastTradeWithTime", "LastTradePriceOnly",
20
+ "HighLimit", "LowLimit", "DaysRange", "DaysRangeRealtime",
21
+ "FiftydayMovingAverage", "TwoHundreddayMovingAverage",
22
+ "ChangeFromTwoHundreddayMovingAverage",
23
+ "PercentChangeFromTwoHundreddayMovingAverage",
24
+ "ChangeFromFiftydayMovingAverage", "PercentChangeFromFiftydayMovingAverage",
25
+ "Name", "Notes", "Open", "PreviousClose", "PricePaid", "ChangeinPercent",
26
+ "PriceSales","PriceBook", "ExDividendDate", "PERatio", "DividendPayDate",
27
+ "PERatioRealtime", "PEGRatio", "PriceEPSEstimateCurrentYear",
28
+ "PriceEPSEstimateNextYear", "Symbol", "SharesOwned", "ShortRatio",
29
+ "LastTradeTime", "TickerTrend", "OneyrTargetPrice", "Volume", "HoldingsValue",
30
+ "HoldingsValueRealtime", "YearRange","DaysValueChange",
31
+ "DaysValueChangeRealtime", "StockExchange","DividendYield","PercentChange",
32
+ "ErrorIndicationreturnedforsymbolchangedinvalid"]
33
+ attr_accessor :response_code, :no_data_message
34
+
35
+ @@fields.each do | field|
36
+ self.__send__(:attr_accessor, field.underscore.to_sym)
37
+ end
38
+
39
+ def self.fields
40
+ @@fields
41
+ end
42
+
43
+ def initialize(data)
44
+ if data['ErrorIndicationreturnedforsymbolchangedinvalid']
45
+ @no_data_message = data['ErrorIndicationreturnedforsymbolchangedinvalid']
46
+ @response_code = 404
47
+ elsif data['count'] && data['count'] == 0
48
+ @no_data_message = "Query returns no valid data"
49
+ @response_code = 404
50
+ else
51
+ @response_code = 200
52
+ data.map do |k,v|
53
+ instance_variable_set("@#{k.underscore}", (v.nil? ? nil : v.to_fs))
54
+ end
55
+ end
56
+ end
57
+
58
+ def success?
59
+ response_code==200
60
+ end
61
+
62
+ def failure?
63
+ response_code==404
64
+ end
65
+
66
+ def self.quote(symbol, start_date=nil, end_date=nil)
67
+ 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22YHOO%22%20and%20startDate%20%3D%20%222009-09-11%22%20and%20endDate%20%3D%20%222010-03-10%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback='
68
+ url = 'http://query.yahooapis.com/v1/public/yql?q=';
69
+ if start_date && end_date
70
+ url = url+URI::encode("select * from yahoo.finance.historicaldata where symbol in (#{symbol.to_p}) and startDate = '#{start_date}' and endDate = '#{end_date}'")
71
+
72
+ else
73
+ url = url+URI::encode("select * from yahoo.finance.quotes where symbol in (#{symbol.to_p})")
74
+ end
75
+ url = url+"&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json"
76
+ response = RestClient.get(url)
77
+ self.parse(response, symbol)
78
+ end
79
+
80
+ def self.dividends_splits(symbol, start_date='01-01-2000', end_date=Date.today.strftime('%m-%d-%Y'))
81
+ symbol = [symbol] unless symbol.is_a?(Array)
82
+
83
+ start_date = start_date.split('-')
84
+ start_date = "a=#{start_date[0]}&b=#{start_date[1]}&c=#{start_date[2]}"
85
+ end_date = end_date.split('-')
86
+ end_date = "d=#{end_date[0]}&e=#{end_date[1]}&f=#{end_date[2]}"
87
+
88
+ results = []
89
+ symbol.each do |s|
90
+ url = "http://ichart.finance.yahoo.com/x?s=#{s}&#{start_date}&#{end_date}&g=v&y=0&z=30000"
91
+ response = RestClient.get(url)
92
+ results.push({'Symbol' => s, 'Series' => CSV.parse(response)})
93
+ end
94
+
95
+ results
96
+ end
97
+
98
+ def self.company_info(symbol)
99
+ url = 'http://query.yahooapis.com/v1/public/yql?q='
100
+
101
+ url += URI::encode("select * from yahoo.finance.stocks where symbol in (#{symbol.to_p})")
102
+ url += "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json"
103
+ response = RestClient.get(url)
104
+ self.parse(response, symbol)
105
+ end
106
+
107
+ def self.key_stats(symbol)
108
+ url = 'http://query.yahooapis.com/v1/public/yql?q='
109
+
110
+ url += URI::encode("SELECT * FROM yahoo.finance.keystats WHERE symbol in (#{symbol.to_p})")
111
+ url += "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json"
112
+ response = RestClient.get(url)
113
+ self.parse(response, symbol)
114
+ end
115
+
116
+ def self.parse(json, symbol)
117
+ results = []
118
+ json = JSON.parse(json)
119
+ count = json["query"]["count"]
120
+ return Stock.new(json["query"]) if count == 0
121
+ results = json["query"]["results"]
122
+ #data = count == 1 ? [data] : data
123
+
124
+ results.each do |k, v|
125
+ results = results[k].is_a?(Array) ? results[k] : [results[k]]
126
+ return results
127
+
128
+ #d["symbol"] = symbol.to_p unless d["symbol"]
129
+ #stock = Stock.new(d)
130
+ #results += stock
131
+ end
132
+ #return results
133
+ end
134
+
135
+ def self.history(symbol, start_date='2012-01-01', end_date=Date.today.strftime('%Y-%m-%d'))
136
+ results = []
137
+ (start_date.to_date..end_date.to_date+1.year).step(365) do |date|
138
+ break if date + 1.day > Date.today
139
+ results += self.quote(symbol, (date + 1.day).strftime('%Y-%m-%d'), (date + 1.year).strftime('%Y-%m-%d'))
140
+ end
141
+
142
+ results
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,23 @@
1
+ class String
2
+
3
+ def underscore
4
+ self.gsub(/::/, '/').
5
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
6
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
7
+ tr("-", "_").
8
+ downcase
9
+ end
10
+
11
+ def to_fs
12
+ (!!Float(self) rescue false) ? Float(self) : self
13
+ end
14
+
15
+ def to_p
16
+ self.split(',').to_p
17
+ end
18
+ end
19
+ class Array
20
+ def to_p
21
+ "'#{self.join("','").gsub(" ", "").upcase}'"
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module BrmFinance
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,81 @@
1
+ require "brm_finance"
2
+ require "spec_helper"
3
+
4
+ describe BrmFinance::Stock do
5
+ describe "quote" do
6
+ context "success" do
7
+ describe "single symbol" do
8
+
9
+ @fields = BrmFinance::Stock.class_variable_get(:@@fields)
10
+
11
+ use_vcr_cassette "aapl"
12
+
13
+ @fields.each do | field |
14
+ it ".#{field}" do
15
+ @stock = BrmFinance::Stock.quote('aapl')
16
+ @stock.should respond_to(field.underscore.to_sym)
17
+ end
18
+ end
19
+
20
+ it "should result in a successful query with " do
21
+ @stock = BrmFinance::Stock.quote('aapl')
22
+ @stock.should be_success
23
+ @stock.response_code.should be_eql(200)
24
+ @stock.should respond_to(:no_data_message)
25
+ @stock.no_data_message.should be_nil
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "comma seperated symbols" do
31
+
32
+ use_vcr_cassette "aapl,tsla"
33
+
34
+ it "should result in a successful query" do
35
+ @stocks = BrmFinance::Stock.quote('aapl,tsla')
36
+ @stocks.each do |stock|
37
+ stock.should be_success
38
+ stock.response_code.should be_eql(200)
39
+ stock.should respond_to(:no_data_message)
40
+ stock.no_data_message.should be_nil
41
+ end
42
+ end
43
+ end
44
+
45
+ context "failure" do
46
+
47
+ @fields = BrmFinance::Stock.class_variable_get(:@@fields)
48
+
49
+ use_vcr_cassette "asdf"
50
+
51
+ it "should fail... gracefully" do
52
+ @stock = BrmFinance::Stock.quote('asdf')
53
+ @stock.should be_failure
54
+ @stock.response_code.should be_eql(404)
55
+ @stock.should respond_to(:no_data_message)
56
+ @stock.no_data_message.should_not be_nil
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "history" do
62
+ context "success" do
63
+ use_vcr_cassette "aapl_history"
64
+
65
+ it "should result in a successful query" do
66
+ @stock = BrmFinance::Stock.history('aapl')
67
+ @stock.count.should >=1
68
+ end
69
+ end
70
+ context "failure" do
71
+ use_vcr_cassette "asdf_history"
72
+
73
+ it "should result in a successful query" do
74
+ @stock = BrmFinance::Stock.history('asdf')
75
+ @stock.response_code.should be_eql(404)
76
+ @stock.should respond_to(:no_data_message)
77
+ @stock.no_data_message.should_not be_nil
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,8 @@
1
+ require 'brm_finance'
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'vcr_setup'
5
+
6
+ RSpec.configure do |config|
7
+ config.extend VCR::RSpec::Macros
8
+ end
data/spec/vcr_setup.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'vcr'
2
+
3
+ VCR.configure do |c|
4
+ c.cassette_library_dir = 'spec/vcr_cassettes'
5
+ c.hook_into :fakeweb
6
+ c.ignore_localhost = true
7
+ c.default_cassette_options = { :record => :new_episodes }
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brm_finance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Adamson, Ty Rauber
@@ -96,6 +96,20 @@ extensions: []
96
96
  extra_rdoc_files: []
97
97
  files:
98
98
  - README.md
99
+ - Gemfile
100
+ - Rakefile
101
+ - spec/brm_finance_spec.rb
102
+ - spec/spec_helper.rb
103
+ - spec/vcr_setup.rb
104
+ - lib/brm_finance/stock.rb
105
+ - lib/brm_finance/utility.rb
106
+ - lib/brm_finance/version.rb
107
+ - lib/brm_finance.rb
108
+ - .DS_Store
109
+ - LICENSE
110
+ - brm_finance-1.0.0.gem
111
+ - brm_finance.gemspec
112
+ - lib/.DS_Store
99
113
  homepage: https://github.com/aadamson/brm_finance
100
114
  licenses: []
101
115
  metadata: {}
@@ -119,4 +133,7 @@ rubygems_version: 2.0.6
119
133
  signing_key:
120
134
  specification_version: 4
121
135
  summary: A ruby gem that retrieves real-time stock quotes from google.
122
- test_files: []
136
+ test_files:
137
+ - spec/brm_finance_spec.rb
138
+ - spec/spec_helper.rb
139
+ - spec/vcr_setup.rb