yahoofinance-typhoeus 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2009 Paul Legato. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions are met:
5
+ * Redistributions of source code must retain the above copyright
6
+ notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright
8
+ notice, this list of conditions and the following disclaimer in the
9
+ documentation and/or other materials provided with the distribution.
10
+ * Neither the name of Paul Legato nor the
11
+ names of its contributors may be used to endorse or promote products
12
+ derived from this software without specific prior written permission.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
18
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
+
25
+
@@ -0,0 +1,54 @@
1
+ = yahoofinance-typhoeus
2
+
3
+ Yahoo! Finance in Ruby. Downloads historic data with the Typhoeus library.
4
+ This project is not endorsed by or connected with Yahoo! in any way.
5
+
6
+ Copyright (C) 2010 Paul Legato. All rights reserved.
7
+ Licensed under the BSD-new license: See LICENSE for details.
8
+
9
+ == Usage
10
+
11
+ YahooFinance-Typhoeus is designed to be both highly efficient and easy to use.
12
+
13
+ === Parallel queries (efficient)
14
+
15
+ When you instantiate a new YahooFinance object, you can tell it how
16
+ many parallel HTTP requests to make at once. The default is 20. This
17
+ seems low, but it's designed to keep you out of trouble by avoiding
18
+ DoSing Yahoo's servers. You change it at your own risk!
19
+
20
+ require 'rubygems'
21
+ require 'yahoofinance-typhoeus'
22
+
23
+ yf = YahooFinance.new # or YahooFinance.new(max_number_of_concurrent_connections)
24
+
25
+ yf.add_query("AAPL", "2008-12-01", "2008-12-15") {|response| puts response}
26
+ yf.add_query("IBM", "2008-10-15", "2008-10-30") {|response| puts response}
27
+
28
+ yf.run
29
+
30
+ That's all there is to it.
31
+
32
+
33
+ === Quick, one-off query (inefficient)
34
+
35
+ require 'rubygems'
36
+ require 'yahoofinance-typhoeus'
37
+
38
+ YahooFinance.quick_query("AAPL", "2009-01-01", "2009-02-01")
39
+
40
+ Note that as quick_query is intended for one-off requests, it does NOT
41
+ take advantage of libcurl/typhoeus' concurrent HTTP connections
42
+ features.
43
+
44
+
45
+ == Note on Patches/Pull Requests
46
+
47
+ * Fork the project.
48
+ * Make your feature addition or bug fix.
49
+ * Add tests for it. This is important so I don't break it in a
50
+ future version unintentionally.
51
+ * Commit, do not mess with rakefile, version, or history.
52
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
53
+ * Send me a pull request. Bonus points for topic branches.
54
+
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "yahoofinance-typhoeus"
8
+ gem.summary = %Q{Yahoo! Finance historic quotes from Ruby.}
9
+ gem.description = %Q{Provides high-performance access to historic stock data from Yahoo! Finance}
10
+ gem.email = "pjlegato@gmail.com"
11
+ gem.homepage = "http://github.com/pjlegato/yahoofinance-typhoeus"
12
+ gem.authors = ["Paul Legato"]
13
+ gem.add_development_dependency "minitest", ">= 0"
14
+ gem.add_dependency "typhoeus", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "yahoofinance-typhoeus #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,118 @@
1
+ #
2
+ # High-performance historic stock data download from Yahoo! Finance.
3
+ # Uses the Typhoeus library and libcurl.
4
+ #
5
+ # This project is not endorsed by or connected with Yahoo! in any way.
6
+ #
7
+ # Author:: Paul Legato (pjlegato at gmail dot com)
8
+ # Copyright:: Copyright (C) 2010 Paul Legato. All rights reserved.
9
+ # License:: BSD-new license: see the file LICENSE for licensing details.
10
+ #
11
+
12
+ require 'rubygems'
13
+ require 'typhoeus'
14
+ require 'date'
15
+
16
+
17
+ class YahooFinanceException < Exception
18
+ end
19
+
20
+ class SymbolNotFoundException < YahooFinanceException
21
+ end
22
+
23
+ class YahooFinance
24
+
25
+ # Create a new YahooFinance object with the specified maximum number
26
+ # of concurrent connections. The default is 20.
27
+ #
28
+ # If you will be repeating the same query more than once, you can
29
+ # cache the responses from Yahoo by setting memoize_requests to
30
+ # true.
31
+ def initialize(concurrent_connections=20, memoize_requests=false)
32
+ @hydra = Typhoeus::Hydra.new(:max_concurrency => concurrent_connections)
33
+ @hydra.disable_memoization unless memoize_requests
34
+ end
35
+
36
+ # Run any pending historic data queries in the queue and potentially
37
+ # execute any defined callbacks.
38
+ # Blocking: will not return until the entire queue has run.
39
+ def run
40
+ @hydra.run
41
+ end
42
+
43
+
44
+ # Adds a query for the given symbol over the given date range to the
45
+ # queue. The given block is used as a callback that will be called
46
+ # with 1 argument, the response, upon successful completion. Note that no
47
+ # queries will actually execute until #run is called.
48
+ #
49
+ # The dates can be either Date objects or strings. If they're
50
+ # strings, they will be fed to Date.parse.
51
+ #
52
+ # Raises a SymbolNotFoundException if querying that symbol produces a 404 error on Yahoo.
53
+ # Raises a YahooFinanceException for any other problems.
54
+ #
55
+ # Otherwise, your block will be called with the body of the
56
+ # response, the raw data from Yahoo!, as its argument.
57
+ #
58
+ #
59
+ def add_query(symbol, start_date, end_date, &callback)
60
+ start_date = Date.parse(start_date) unless start_date.is_a?(Date)
61
+ end_date = Date.parse(end_date) unless end_date.is_a?(Date)
62
+
63
+ @hydra.queue(make_request(symbol, start_date, end_date, callback))
64
+
65
+ true
66
+ end
67
+
68
+
69
+ # Convenience method for one-off quick queries of a given symbol and
70
+ # date range.
71
+ #
72
+ # Note that as this is intended for one-off runs, it is not as
73
+ # efficient as queueing them up yourself and running them in
74
+ # parallel.
75
+ #
76
+ # Returns the raw CSV response data from Yahoo.
77
+ #
78
+ def self.quick_query(symbol, start_date, end_date)
79
+ yf = self.new
80
+ result = nil
81
+ yf.add_query(symbol, start_date, end_date) {|response| result = response }
82
+ yf.run
83
+ result
84
+ end
85
+
86
+ private
87
+
88
+ # Returns a Typhoeus HTTP request object that will query Yahoo!
89
+ # Finance for the given symbol and date range and call the given
90
+ # callback if all is well.
91
+ def make_request(symbol, start_date, end_date, callback)
92
+ url = "http://itable.finance.yahoo.com" +
93
+ "/table.csv?s=#{ symbol }&g=d" +
94
+ "&a=#{ start_date.month - 1 }&b=#{ start_date.mday }&c=#{ start_date.year }" +
95
+ "&d=#{ end_date.month - 1 }&e=#{ end_date.mday }&f=#{ end_date.year.to_s }"
96
+
97
+ request = Typhoeus::Request.new(url, :method => :get)
98
+
99
+ request.on_complete {|response|
100
+ if response.code == 200
101
+ if response.body[0..40] != "Date,Open,High,Low,Close,Volume,Adj Close"
102
+ raise YahooFinanceException.new(" * Error: Unknown response body from Yahoo - #{ response.body[0..40] } ...")
103
+ else
104
+ # good response. go.
105
+ callback.call(response.body)
106
+ end
107
+ elsif response.code == 404
108
+ raise SymbolNotFoundException.new(symbol + " not found at Yahoo")
109
+ else
110
+ raise YahooFinanceException.new("Error communicating with Yahoo. Response code #{ response.code }. URL: " +
111
+ "#{ url }. Response: #{ response.inspect }")
112
+ end
113
+ }
114
+
115
+ request
116
+ end # make_request
117
+
118
+ end # class
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'minitest/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'yahoofinance-typhoeus'
7
+
8
+ class MiniTest::Unit::TestCase
9
+ end
10
+
11
+ MiniTest::Unit.autorun
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ class TestYahoofinanceTyphoeus < MiniTest::Unit::TestCase
4
+ def test_quick_query
5
+ assert_equal("Date,Open,High,Low,Close,Volume,Adj Close\n2009-12-15,129.46,129.86,127.94,128.49,7862600,127.92\n2009-12-14,129.65,129.98,129.60,129.93,5201300,129.35\n2009-12-11,129.01,129.77,128.71,129.68,6597200,129.10\n2009-12-10,128.13,129.47,128.09,129.34,7077800,128.76\n2009-12-09,126.70,128.39,126.11,128.39,6071900,127.82\n2009-12-08,126.97,127.35,126.16,126.80,5351400,126.24\n2009-12-07,126.88,127.53,126.59,127.04,4144400,126.47\n2009-12-04,128.40,128.90,126.00,127.25,7068500,126.68\n2009-12-03,127.60,128.47,127.25,127.55,5760000,126.98\n2009-12-02,127.32,128.39,127.16,127.21,4597900,126.64\n2009-12-01,127.29,128.39,126.85,127.94,6578600,127.37\n",
6
+ YahooFinance.quick_query("IBM", "2009-12-01", "2009-12-15"))
7
+ end
8
+
9
+
10
+ def test_parallel_query
11
+ yf = YahooFinance.new
12
+ refute_nil(yf)
13
+
14
+ ibm_result = nil
15
+ aapl_result = nil
16
+
17
+ yf.add_query("AAPL", "2008-12-01", "2008-12-15") {|response| aapl_result = response}
18
+ yf.add_query("IBM", "2008-10-15", "2008-10-30") {|response| ibm_result = response}
19
+
20
+ yf.run
21
+
22
+ assert_equal("Date,Open,High,Low,Close,Volume,Adj Close\n2008-10-30,91.01,92.40,88.90,90.69,12033800,88.03\n2008-10-29,87.74,91.00,86.25,88.20,12692000,85.61\n2008-10-28,81.40,87.61,79.52,87.28,14205900,84.72\n2008-10-27,80.27,84.50,79.01,79.66,11099100,77.32\n2008-10-24,79.27,84.31,78.82,82.07,12287300,79.66\n2008-10-23,84.09,85.89,81.00,84.35,12358000,81.87\n2008-10-22,87.00,87.59,80.80,83.60,14198100,81.15\n2008-10-21,92.22,92.46,88.57,88.86,9716400,86.25\n2008-10-20,92.21,93.31,89.33,92.51,9747100,89.79\n2008-10-17,91.75,95.91,87.71,90.78,15230700,88.11\n2008-10-16,89.38,92.00,84.35,91.52,16271300,88.83\n2008-10-15,92.77,95.29,87.71,88.29,11330800,85.70\n",
23
+ ibm_result)
24
+
25
+ assert_equal("Date,Open,High,Low,Close,Volume,Adj Close\n2008-12-15,95.99,96.21,93.00,94.75,31848500,94.75\n2008-12-12,92.80,99.00,92.53,98.27,37184800,98.27\n2008-12-11,97.35,101.24,94.83,95.00,37164900,95.00\n2008-12-10,97.87,99.49,96.50,98.21,33501700,98.21\n2008-12-09,98.04,103.60,97.21,100.06,42982000,100.06\n2008-12-08,97.28,100.80,95.80,99.72,42326500,99.72\n2008-12-05,90.35,94.49,88.86,94.00,37278400,94.00\n2008-12-04,94.43,95.21,89.06,91.41,38977500,91.41\n2008-12-03,89.40,96.23,88.80,95.90,47810000,95.90\n2008-12-02,90.03,92.65,86.50,92.47,41025800,92.47\n2008-12-01,91.30,92.27,88.92,88.93,32991700,88.93\n",
26
+ aapl_result)
27
+
28
+ end
29
+
30
+
31
+ def test_invalid_symbol_quick_query
32
+ assert_raises(SymbolNotFoundException) { YahooFinance.quick_query("NonexistentSymbol", "2009-01-01", "2009-10-10") }
33
+ end
34
+
35
+ def test_invalid_symbol_parallel_query
36
+ yf = YahooFinance.new
37
+ refute_nil(yf)
38
+
39
+ ibm_result = nil
40
+ nonexistent_result = nil
41
+
42
+ yf.add_query("AAPL", "2009-12-01", "2009-12-15") {|response| aapl_result = response}
43
+ yf.add_query("NonexistentSymbol", "2009-10-15", "2009-10-30") {|response| nonexistent_result = response}
44
+
45
+ assert_raises(SymbolNotFoundException) { yf.run }
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{yahoofinance-typhoeus}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Legato"]
12
+ s.date = %q{2010-02-18}
13
+ s.description = %q{Provides high-performance access to historic stock data from Yahoo! Finance}
14
+ s.email = %q{pjlegato@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/yahoofinance-typhoeus.rb",
27
+ "test/helper.rb",
28
+ "test/test_yahoofinance-typhoeus.rb",
29
+ "yahoofinance-typhoeus.gemspec"
30
+ ]
31
+ s.homepage = %q{http://github.com/pjlegato/yahoofinance-typhoeus}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Yahoo! Finance historic quotes from Ruby.}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_yahoofinance-typhoeus.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<minitest>, [">= 0"])
47
+ s.add_runtime_dependency(%q<typhoeus>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<minitest>, [">= 0"])
50
+ s.add_dependency(%q<typhoeus>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<minitest>, [">= 0"])
54
+ s.add_dependency(%q<typhoeus>, [">= 0"])
55
+ end
56
+ end
57
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yahoofinance-typhoeus
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Legato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-18 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: minitest
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: typhoeus
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Provides high-performance access to historic stock data from Yahoo! Finance
36
+ email: pjlegato@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/yahoofinance-typhoeus.rb
52
+ - test/helper.rb
53
+ - test/test_yahoofinance-typhoeus.rb
54
+ - yahoofinance-typhoeus.gemspec
55
+ has_rdoc: true
56
+ homepage: http://github.com/pjlegato/yahoofinance-typhoeus
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Yahoo! Finance historic quotes from Ruby.
83
+ test_files:
84
+ - test/helper.rb
85
+ - test/test_yahoofinance-typhoeus.rb