yfin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d3a87369df7029e00883ae4b594e640166f835a
4
+ data.tar.gz: 0d6c21445a8c6897004e1a5292811140a21eec74
5
+ SHA512:
6
+ metadata.gz: c056b546d9de0a080e81b57df8b790a683a014d42fa587bb50ee62860ac330326b6fb071d6c49b3fcd9aa636626ca14ff0f97c7b35f40c4c59328fc3d0cbaf61
7
+ data.tar.gz: da00a1970241835023ea50666987affe81f5a0cebd379a69085dad309287e1b422844cb7a41bd0002442a3267ad127e93cf2be4336e76b6b45d03f50c29f8ade
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Christopher Moeller
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Yfin
2
+
3
+ Yfin provides a ruby interface to some parts of Yahoo! Finance. Currently, it
4
+ only retrieves historical data.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'yfin'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install yfin
19
+
20
+ ## Usage
21
+
22
+ Create a new Yfin::History object:
23
+
24
+ Yfin::History.new('aapl')
25
+
26
+ Available options are :start\_date, :end\_date and :type. :start\_date and
27
+ :end\_date are both Dates while :type is a symbol. All Dates must be in the
28
+ past to be valid. :start\_date defaults to 30 days ago and :end\_date defaults
29
+ to Date.today. Valid values for :type are :daily (default), :weekly, :monthly
30
+ and :dividend.
31
+
32
+ Here is an example using all of the options:
33
+
34
+ Yfin::History.new('aapl', start_date: Date.new(1983, 7, 25), end_date: Date.today, type: :monthly)
35
+
36
+ After creating a Yfin::History instance, you can call `to_csv` to get a
37
+ CSV::Table of the data.
38
+
39
+ history = Yfin::History.new('aapl')
40
+ history.to_csv #=> #<CSV::Table mode:col_or_row row_count:22>
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << ['lib/yfin', 'test']
7
+ t.test_files = FileList['test/**/*_spec.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,5 @@
1
+ require 'net/http'
2
+ require 'date'
3
+ require 'csv'
4
+ require 'yfin/history'
5
+ require 'yfin/version'
@@ -0,0 +1,86 @@
1
+ module Yfin
2
+ class History
3
+ attr_reader :symbol, :start_date, :end_date, :type
4
+
5
+ def initialize(symbol, options={})
6
+ validate_options(options)
7
+ @symbol = symbol
8
+ @start_date = options[:start_date] || Date.today - 30
9
+ @end_date = options[:end_date] || Date.today
10
+ @type = options[:type] || :daily
11
+ end
12
+
13
+ def to_csv
14
+ CSV.parse(data_from_yahoo, headers: :first_line, header_converters: :symbol, converters: [:date, :numeric])
15
+ end
16
+
17
+ private
18
+
19
+ def data_from_yahoo
20
+ tries ||= 3
21
+ Net::HTTP.get(uri)
22
+ rescue Errno::ECONNRESET => e
23
+ retry unless (tries -= 1).zero?
24
+ end
25
+
26
+ def uri
27
+ URI.parse("#{uri_base}#{uri_params}")
28
+ end
29
+
30
+ def uri_base
31
+ "http://ichart.finance.yahoo.com/table.csv"
32
+ end
33
+
34
+ def uri_params
35
+ params_array = ['?']
36
+ params.each { |k, v| params_array << "#{k}=#{v}" }
37
+ params_array.join('&')
38
+ end
39
+
40
+ def params
41
+ {
42
+ s: @symbol,
43
+ a: sprintf("%02d", @start_date.month-1),
44
+ b: @start_date.day,
45
+ c: @start_date.year,
46
+ d: sprintf("%02d", @end_date.month-1),
47
+ e: @end_date.day,
48
+ f: @end_date.year,
49
+ g: type_param,
50
+ ignore: '.csv'
51
+ }
52
+ end
53
+
54
+ def type_param
55
+ case @type
56
+ when :daily then 'd'
57
+ when :weekly then 'w'
58
+ when :monthly then 'm'
59
+ when :dividend then 'v'
60
+ end
61
+ end
62
+
63
+ VALID_KEYS = [:start_date, :end_date, :type]
64
+ VALID_TYPES = [:daily, :weekly, :monthly, :dividend]
65
+
66
+ def validate_options(options)
67
+ validate_keys(options.keys)
68
+ validate_types(options)
69
+ end
70
+
71
+ def validate_keys(*keys)
72
+ keys.flatten!
73
+ keys.each { |k| raise ArgumentError.new("Unknown key: #{k}") if !VALID_KEYS.include?(k) }
74
+ end
75
+
76
+ def validate_types(options)
77
+ options.each do |o|
78
+ if o[0] == :type
79
+ raise(ArgumentError, "#{o[0]} must be a Symbol") if !o[1].is_a?(Symbol)
80
+ else
81
+ raise(ArgumentError, "#{o[0]} must be a Date") if !o[1].is_a?(Date)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module Yfin
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ describe Yfin::History do
4
+ let(:symbol) { 'aapl' }
5
+ let(:start_date) { Date.today - 180 }
6
+ let(:end_date) { Date.today - 1 }
7
+ let(:type) { :weekly }
8
+
9
+ describe '#new' do
10
+ describe 'without options' do
11
+ subject { Yfin::History.new(symbol) }
12
+
13
+ specify { subject.must_be_kind_of(Yfin::History) }
14
+ specify { subject.symbol.must_equal(symbol) }
15
+ specify { subject.start_date.must_equal(Date.today - 30) }
16
+ specify { subject.end_date.must_equal(Date.today) }
17
+ specify { subject.type.must_equal(:daily) }
18
+ end
19
+
20
+ describe 'with' do
21
+ describe ':start_date' do
22
+ subject { Yfin::History.new(symbol, start_date: start_date) }
23
+
24
+ specify { subject.must_be_kind_of(Yfin::History) }
25
+ specify { subject.symbol.must_equal(symbol) }
26
+ specify { subject.start_date.must_equal(start_date) }
27
+ specify { subject.end_date.must_equal(Date.today) }
28
+ specify { subject.type.must_equal(:daily) }
29
+ end
30
+
31
+ describe ':start_date, :end_date' do
32
+ subject { Yfin::History.new(symbol, start_date: start_date, end_date: end_date) }
33
+
34
+ specify { subject.must_be_kind_of(Yfin::History) }
35
+ specify { subject.symbol.must_equal(symbol) }
36
+ specify { subject.start_date.must_equal(start_date) }
37
+ specify { subject.end_date.must_equal(end_date) }
38
+ specify { subject.type.must_equal(:daily) }
39
+ end
40
+
41
+ describe ':start_date, :end_date, :type' do
42
+ subject { Yfin::History.new(symbol, start_date: start_date, end_date: end_date, type: type) }
43
+
44
+ specify { subject.must_be_kind_of(Yfin::History) }
45
+ specify { subject.symbol.must_equal(symbol) }
46
+ specify { subject.start_date.must_equal(start_date) }
47
+ specify { subject.end_date.must_equal(end_date) }
48
+ specify { subject.type.must_equal(type) }
49
+ end
50
+ end
51
+
52
+ describe 'invalid calls' do
53
+ specify { proc{ Yfin::History.new }.must_raise(ArgumentError) }
54
+ specify { proc{ Yfin::History.new(symbol, unknown_key: 'Unknown') }.must_raise(ArgumentError) }
55
+
56
+ describe 'data types' do
57
+ specify { proc{ Yfin::History.new(symbol, start_date: Object.new) }.must_raise(ArgumentError) }
58
+ specify { proc{ Yfin::History.new(symbol, end_date: Object.new) }.must_raise(ArgumentError) }
59
+ specify { proc{ Yfin::History.new(symbol, type: Object.new) }.must_raise(ArgumentError) }
60
+ end
61
+ end
62
+ end
63
+
64
+ describe '#to_csv' do
65
+ subject { Yfin::History.new(symbol) }
66
+ specify { subject.must_respond_to(:to_csv) }
67
+ specify { subject.to_csv.must_be_kind_of(CSV::Table) }
68
+
69
+ describe 'row' do
70
+ subject { Yfin::History.new(symbol).to_csv.first }
71
+
72
+ [:date, :open, :high, :low, :close, :volume, :adj_close].each { |e|
73
+ specify { subject.must_include(e) }
74
+ }
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/yfin.rb', __FILE__)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yfin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yfin"
8
+ spec.version = Yfin::VERSION
9
+ spec.authors = ["Christopher Moeller"]
10
+ spec.email = ["cmoel@me.com"]
11
+ spec.description = %q{Retrieves data from Yahoo! Finance}
12
+ spec.summary = %q{Retrieves data from Yahoo! Finance}
13
+ spec.homepage = "https://github.com/cmoel/yfin"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake", "~> 10.0.3"
23
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yfin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Moeller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 10.0.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 10.0.3
41
+ description: Retrieves data from Yahoo! Finance
42
+ email:
43
+ - cmoel@me.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/yfin.rb
54
+ - lib/yfin/history.rb
55
+ - lib/yfin/version.rb
56
+ - test/lib/yfin/history_spec.rb
57
+ - test/test_helper.rb
58
+ - yfin.gemspec
59
+ homepage: https://github.com/cmoel/yfin
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Retrieves data from Yahoo! Finance
83
+ test_files:
84
+ - test/lib/yfin/history_spec.rb
85
+ - test/test_helper.rb