Nas-yahoo_stock_kalus 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,94 @@
1
+ === 0.1.1 2009-08-31
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
5
+
6
+ === 0.1.2 2009-09-01
7
+
8
+ * 1 minor change:
9
+ * changed method name YahooStock::Quote#get_data to get
10
+
11
+ === 0.1.3 2009-09-05
12
+
13
+ * 3 additions:
14
+ * adds a new class called YahooStock::ScripSymbol that allows to
15
+ find out the stock symbol for a company
16
+ * adds specs for previous and new code
17
+ * adds
18
+
19
+ === 0.1.4 2009-09-05
20
+
21
+ * 2 changes related with ruby 1.9:
22
+ * modifies quote class to be compatible with ruby 1.9
23
+ * modifies interface class to be compatible with 1.9
24
+
25
+ === 0.1.5 2009-09-06
26
+
27
+ * 1 changes related with ruby 1.9:
28
+ * modifies ScripSymbol class to be compatible with ruby 1.9
29
+
30
+ === 0.1.6 2009-09-07
31
+
32
+ * 1 addition:
33
+ * adds convenience methods Quote#realtime, standard and extended to
34
+ fetch relevant stock values
35
+
36
+ === 1.0.1 2009-09-10
37
+
38
+ * 1 addition:
39
+ * adds feature to retrieve historical stock data
40
+
41
+ === 1.0.2 2009-10-04
42
+ Major changes in the public API. Check the README.rdoc file and individual classes for more information
43
+
44
+ * 8 addition:
45
+ * adds YahooStock::Interface::History
46
+ * adds YahooStock::Interface::Quote
47
+ * adds YahooStock::Interface::ScripSymbol
48
+ * adds YahooStock::Result as a superclass for all formatter classes
49
+ * adds array format, Result::ArrayFormat, to generate results as an array
50
+ * adds hash format, Result::ArrayFormat, to generate results as key value pairs
51
+ * adds base class as parent for public facing classes
52
+ * adds observers to check when it is required to send request to yahoo again to get fresh data
53
+
54
+ * 7 changes:
55
+ * changes YahooStock::Interface to be the main interface class. Other interface classes will inherit from this
56
+ * changes YahooStock::ScripSymbol to use YahooStock::Interface::ScripSymbol
57
+ * removes all history interface logic from YahooStock::History and uses YahooStock::Interface::History
58
+ * changes YahooStock::Quote, YahooStock::History, YahooStock::ScripSymbol to extend from YahooStock::Base
59
+ * sorts url parameters before sending request to yahoo
60
+ * YahooInterface::Quote#realtime, extended, and standard methods should return self to allow calling different format output method calls
61
+ * do not discard any empty values when finding scrip symbol options for a company as this interferes with different format options
62
+
63
+ === 1.0.3 2009-10-04
64
+
65
+ * 2 changes:
66
+ * changes README file to add reference for gemcutter
67
+ * changes gemspec to use nas-yahoo_stock
68
+
69
+ === 1.0.4 2009-10-23
70
+
71
+ * 2 addition:
72
+ * adds support for xml format
73
+ * adds cucumber features
74
+
75
+ * 1 changes:
76
+ * changes README file to remove git source add for github
77
+
78
+ === 1.0.5 2010-05-07
79
+
80
+ * 1 change:
81
+ * made the case statement in YahooStock::Interface::History on line 86 ruby 1.9.1 compatible
82
+
83
+ === 1.0.6 2010-11-13
84
+
85
+ * 2 addition:
86
+ * adds dividend query option by Ryan Sandridge
87
+ * adds cucumber rake task
88
+
89
+ * 1 changes:
90
+ * Make sure all tests are run with rspec 2.1.0
91
+
92
+ === 1.0.7 2010-11-14
93
+
94
+ * Some refactoring after the addition of dividend option to the history
data/Manifest.txt ADDED
@@ -0,0 +1,37 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/yahoo_stock/base.rb
6
+ lib/yahoo_stock/history.rb
7
+ lib/yahoo_stock/interface.rb
8
+ lib/yahoo_stock/interface/history.rb
9
+ lib/yahoo_stock/interface/quote.rb
10
+ lib/yahoo_stock/interface/scrip_symbol.rb
11
+ lib/yahoo_stock/quote.rb
12
+ lib/yahoo_stock/result.rb
13
+ lib/yahoo_stock/result/array_format.rb
14
+ lib/yahoo_stock/result/hash_format.rb
15
+ lib/yahoo_stock/result/xml_format.rb
16
+ lib/yahoo_stock/scrip_symbol.rb
17
+ lib/yahoo_stock.rb
18
+ spec/spec_helper.rb
19
+ spec/yahoo_stock/base_spec.rb
20
+ spec/yahoo_stock/interface_spec.rb
21
+ spec/yahoo_stock/interface/history_spec.rb
22
+ spec/yahoo_stock/interface/quote_spec.rb
23
+ spec/yahoo_stock/interface/scrip_symbol_spec.rb
24
+ spec/yahoo_stock/quote_spec.rb
25
+ spec/yahoo_stock/history_spec.rb
26
+ spec/yahoo_stock/result_spec.rb
27
+ spec/yahoo_stock/result/array_format_spec.rb
28
+ spec/yahoo_stock/result/hash_format_spec.rb
29
+ spec/yahoo_stock/result/xml_format_spec.rb
30
+ spec/yahoo_stock/scrip_symbol_spec.rb
31
+ features/history.feature
32
+ features/quotes.feature
33
+ features/script_symbol.feature
34
+ features/step_definitions/history_steps.rb
35
+ features/step_definitions/quotes_steps.rb
36
+ features/step_definitions/script_symbol_steps.rb
37
+
data/README.rdoc ADDED
@@ -0,0 +1,143 @@
1
+ = yahoo_stock
2
+
3
+ * http://github.com/nas/yahoo_stock
4
+
5
+ == DESCRIPTION:
6
+
7
+ Provides an interface to yahoo finance to get stock related data. For instance, latest trade related data, volume, 50 day moving average, market cap, etc, virtually any thing that yahoo finance provides.
8
+
9
+ If you don't know the stock / scrip symbol of the company then you can find that out by using the YahooStock::ScripSymbol class. The methods are mentioned in the Usage section below. For instance, YHOO for yahoo, GOOG for google, etc.
10
+ The kind of parameters to be passed can be found after initializing the
11
+ YahooStock::Quote object and passing valid_parameters message to the quote object, example is given below in the USAGE section.
12
+
13
+ For details : http://nasir.wordpress.com/2009/10/28/ruby-gem-for-finance-data
14
+
15
+
16
+ == INSTALL:
17
+
18
+ gem sources -a http://gemcutter.org
19
+
20
+ sudo gem install nas-yahoo_stock
21
+
22
+
23
+ == USAGE:
24
+
25
+ require 'rubygems'
26
+ require 'yahoo_stock'
27
+
28
+ * Initialize quote object:
29
+
30
+ quote = YahooStock::Quote.new(:stock_symbols => ['YHOO', 'GOOG'])
31
+
32
+ * To view the valid parameters that can be passed:
33
+
34
+ quote.valid_parameters
35
+
36
+ * To view the current parameters used:
37
+
38
+ quote.current_parameters
39
+
40
+ * To view the current stock symbols used:
41
+
42
+ quote.current_symbols
43
+
44
+ * To add more stocks to the list:
45
+
46
+ quote.add_symbols('MSFT', 'AAPL')
47
+
48
+ * To remove stocks from list:
49
+
50
+ quote.remove_symbols('MSFT', 'AAPL')
51
+
52
+ * To find data for all stocks, this will give a string containing all values:
53
+
54
+ quote.results.output
55
+
56
+ * To return an array instead of a string use:
57
+
58
+ quote.results(:to_array).output
59
+
60
+ * To return a hash, use:
61
+
62
+ quote.results(:to_hash).output
63
+
64
+ * To return xml, use:
65
+
66
+ quote.results(:to_xml).output
67
+
68
+ * to store results in a file, use:
69
+
70
+ quote.results.store('filename')
71
+
72
+ * To find out the stock symbol for a company:
73
+
74
+ symbol = YahooStock::ScripSymbol.new('Yahoo')
75
+
76
+ * To find all symbols for that company, this will give a string containing all values:
77
+
78
+ symbol.results.output
79
+
80
+ * Similarly to YahooStock::Quote, results can be converted to an array, a hash, xml, or stored in a file:
81
+
82
+ symbol.results(:to_array).output
83
+ symbol.results(:to_hash).output
84
+ symbol.results(:to_xml).output
85
+ symbol.results.store('filename')
86
+
87
+ * To find symbols for multiple companies:
88
+
89
+ symbols = YahooStock::ScripSymbol.results('Yahoo', 'Company1', 'Company3')
90
+
91
+ * To find historical data for a stock:
92
+
93
+ history = YahooStock::History.new(:stock_symbol => 'yhoo', :start_date => Date.today-20, :end_date => Date.today-2)
94
+
95
+ * As with regular quotes and symbol lookups, the results can be returned in various formats:
96
+
97
+ history.results.output
98
+ history.results(:to_array).output
99
+ history.results(:to_hash).output
100
+ history.results(:to_xml).output
101
+ history.results.store('filename')
102
+
103
+ ==Docs
104
+
105
+ http://rdoc.info/projects/nas/yahoo_stock
106
+
107
+ == TESTS:
108
+
109
+ To run all tests 'rspec spec' from the root directory. It requires rspec >= 2.1.0
110
+
111
+ To run cucumber features "rake cucumber". It requires cucumber >= 0.9.4
112
+
113
+ Cucumber Test html output is logged in log/features.html
114
+
115
+ ==Thanks
116
+
117
+ * Thanks to {Ryan Sandridge}[http://github.com/dissolved] for adding the dividend query option.
118
+
119
+
120
+ == LICENSE:
121
+
122
+ (The MIT License)
123
+
124
+ Copyright (c) 2009 Nasir Jamal
125
+
126
+ Permission is hereby granted, free of charge, to any person obtaining
127
+ a copy of this software and associated documentation files (the
128
+ 'Software'), to deal in the Software without restriction, including
129
+ without limitation the rights to use, copy, modify, merge, publish,
130
+ distribute, sublicense, and/or sell copies of the Software, and to
131
+ permit persons to whom the Software is furnished to do so, subject to
132
+ the following conditions:
133
+
134
+ The above copyright notice and this permission notice shall be
135
+ included in all copies or substantial portions of the Software.
136
+
137
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
138
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
139
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
140
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
141
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
142
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
143
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+
3
+ require 'cucumber'
4
+ require 'cucumber/rake/task'
5
+
6
+ desc 'Run Cucumber features and generate an HTML summary'
7
+ Cucumber::Rake::Task.new(:cucumber) do |t|
8
+ t.cucumber_opts = ["--no-color",
9
+ "--format html -o log/features.html",
10
+ "--format progress"]
11
+ end
12
+
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'date'
5
+ require 'yahoo_stock/base.rb'
6
+ require 'yahoo_stock/interface.rb'
7
+ require 'yahoo_stock/history.rb'
8
+ require 'yahoo_stock/scrip_symbol.rb'
9
+ require 'yahoo_stock/quote.rb'
10
+ require 'yahoo_stock/interface/quote.rb'
11
+ require 'yahoo_stock/interface/history.rb'
12
+ require 'yahoo_stock/interface/scrip_symbol.rb'
13
+ require 'yahoo_stock/result.rb'
14
+ require 'yahoo_stock/result/array_format.rb'
15
+ require 'yahoo_stock/result/hash_format.rb'
16
+ require 'yahoo_stock/result/xml_format.rb'
17
+
18
+ module YahooStock
19
+ VERSION = '1.0.8'
20
+ end
@@ -0,0 +1,42 @@
1
+ # YahooStock::Base class to find and output results.
2
+ module YahooStock
3
+
4
+ class Base
5
+
6
+ def initialize(interface)
7
+ @interface = interface
8
+ end
9
+
10
+ # Uses the values method of the interface used to show results .
11
+ def find
12
+ @interface.values
13
+ end
14
+
15
+ def symbols
16
+ @interface.stock_symbols
17
+ end
18
+
19
+ # Method takes one parameter type or a block
20
+ # type uses the existing format classes namespaced in Result::ClassName.output
21
+ # e.g. * results :to_array
22
+ # * results {YourSpecialFormat.method_name}
23
+ # to store
24
+ def results(type=nil, &block)
25
+ if block_given?
26
+ yield
27
+ else
28
+ return YahooStock::Result.new(find) if !type || type.to_s.empty?
29
+ format_type = type.to_s.sub(/^to_/,'').strip
30
+ return YahooStock::Result.const_get("#{format_type.capitalize}Format").new(find){data_attributes} unless format_type == "hash"
31
+ return YahooStock::Result.const_get("#{format_type.capitalize}Format").new(find, symbols){data_attributes}
32
+ end
33
+ end
34
+
35
+ # Abstract method
36
+ def data_attributes
37
+ raise 'Abstract method called. Use the subclass data_attributes method'
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,32 @@
1
+ module YahooStock
2
+ class History < Base
3
+
4
+ class HistoryError < RuntimeError; end
5
+
6
+ def initialize(options)
7
+ unless options.is_a?(Hash)
8
+ raise HistoryError, 'A hash of start_date, end_date and stock_symbol is expected as parameters'
9
+ end
10
+ @interface = YahooStock::Interface::History.new(options)
11
+ end
12
+
13
+ def find
14
+ @find_values = super()
15
+ @find_values.sub(/Date.*\s/,'') if @find_values
16
+ end
17
+
18
+ def values_with_header
19
+ @interface.values
20
+ end
21
+
22
+ def data_attributes
23
+ return unless values_with_header
24
+ data_attributes = /Date.*\s/.match(values_with_header)
25
+ unless data_attributes.nil?
26
+ @data_attributes = data_attributes[0].sub(/\s*$/,'').split(',')
27
+ @data_attributes.map &:strip
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,71 @@
1
+ =begin
2
+ Main class to interface with Yahoo
3
+ =end
4
+
5
+ require 'net/http'
6
+ require 'observer'
7
+ module YahooStock
8
+ # ==DESCRIPTION:
9
+ #
10
+ # Class to generate the right url and interface with yahoo
11
+ #
12
+ class Interface
13
+ include Observable
14
+ class InterfaceError < RuntimeError ; end
15
+
16
+ BASE_URLS = {
17
+ :quote => "http://download.finance.yahoo.com/d/quotes.csv",
18
+ :history => "http://ichart.finance.yahoo.com/table.csv",
19
+ :scrip_symbol => "http://finance.yahoo.com/lookup/all"
20
+ } unless defined?(BASE_URLS)
21
+
22
+ attr_accessor :base_url, :uri_parameters
23
+
24
+ # Send request to the uri and get results
25
+ def get
26
+ begin
27
+ response = Net::HTTP.get_response(URI.parse(uri))
28
+ rescue => e
29
+ raise InterfaceError, "#{e.message}\n\n#{e.backtrace}"
30
+ end
31
+ response.code == '200' ? response.body : response_error(response)
32
+ end
33
+
34
+ # Generates full url to connect to yahoo
35
+ def uri
36
+ raise InterfaceError, 'Base url is require to generate full uri.' unless @base_url
37
+ return @base_url if @uri_parameters.nil? || @uri_parameters.empty?
38
+ params_with_values = []
39
+ @uri_parameters.each {|k,v| params_with_values << "#{k}=#{v}"}
40
+ @base_url+'?'+params_with_values.join('&')
41
+ end
42
+
43
+ # Get result string
44
+ def values
45
+ @values ||= get
46
+ end
47
+
48
+ def update
49
+ @values = nil
50
+ end
51
+
52
+ private
53
+
54
+ # Generate error for debugging when something goes wrong while sending
55
+ # request to yahoo
56
+ def response_error(response)
57
+ trace = "Response \n\nbody : #{response.body}\n\ncode: #{response.code}\n\nmessage: #{response.message}"
58
+ case @base_url
59
+ when BASE_URLS[:quote]
60
+ raise InterfaceError, "Error :: Could not get stock data \n\n#{trace}"
61
+ when BASE_URLS[:history]
62
+ raise InterfaceError, "Error :: Could not get historical data \n\n#{trace}"
63
+ when BASE_URLS[:scrip_symbol]
64
+ raise InterfaceError, "Error :: Could not get stock symbol \n\n#{trace}"
65
+ else
66
+ raise InterfaceError, "Error connecting to #{@base_url} \n\n#{trace}"
67
+ end
68
+ end
69
+
70
+ end
71
+ end