yahoo_finance 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rake"
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,74 @@
1
+ require "open-uri"
2
+ require "csv"
3
+ require "ostruct"
4
+
5
+ module YahooFinance
6
+ class Historical
7
+ include Enumerable
8
+
9
+ attr_reader :ticker, :start_date, :end_date, :group
10
+
11
+ BASE_URL = "http://ichart.finance.yahoo.com/table.csv"
12
+
13
+ GROUPS = {
14
+ :daily => "d",
15
+ :weekly => "w",
16
+ :monthly => "m"
17
+ }
18
+
19
+ def self.daily(ticker, options = {})
20
+ new(ticker, options.merge(:group => GROUPS[:daily]))
21
+ end
22
+
23
+ def self.weekly(ticker, options = {})
24
+ new(ticker, options.merge(:group => GROUPS[:weekly]))
25
+ end
26
+
27
+ def self.monthly(ticker, options = {})
28
+ new(ticker, options.merge(:group => GROUPS[:monthly]))
29
+ end
30
+
31
+ def initialize(ticker, options = {})
32
+ @ticker = ticker
33
+ @start_date = options[:from]
34
+ @end_date = options[:to]
35
+ @group = options[:group] || GROUPS[:daily]
36
+ end
37
+
38
+ def each(&block)
39
+ result.reverse_each(&block)
40
+ end
41
+
42
+ private
43
+
44
+ def result
45
+ @result ||= Array.new.tap do |result|
46
+ CSV.parse(raw_data, :return_headers => false, :converters => :all, :headers => [:date, :open, :high, :low, :close, :volume, :adjusted_close]).each_with_index do |line, index|
47
+ next if index.zero?
48
+ line[:date] = Date.parse(line[:date])
49
+ result << OpenStruct.new(line.to_hash)
50
+ end
51
+ end
52
+ end
53
+
54
+ def raw_data
55
+ @raw_data ||= open(url).read
56
+ end
57
+
58
+ def url
59
+ "#{BASE_URL}?s=#{URI.escape(ticker)}&g=#{group}&ignore=.csv".tap do |url|
60
+ if start_date
61
+ url << "&a=#{start_date.month - 1}"
62
+ url << "&b=#{start_date.day}"
63
+ url << "&c=#{start_date.year}"
64
+ end
65
+
66
+ if end_date
67
+ url << "&d=#{end_date.month - 1}"
68
+ url << "&e=#{end_date.day}"
69
+ url << "&f=#{end_date.year}"
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module YahooFinance
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "yahoo_finance/version"
2
+
3
+ module YahooFinance
4
+ autoload :Historical, "yahoo_finance/historical"
5
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe YahooFinance::Historical do
4
+ let(:ticker) { "AAPL" }
5
+ let(:start_date) { Date.new(2011, 11, 11) }
6
+ let(:end_date) { Date.new(2011, 11, 21) }
7
+
8
+ shared_examples_for "a valid instance" do
9
+ its(:ticker) { should == ticker }
10
+ its(:start_date) { should == start_date }
11
+ its(:end_date) { should == end_date }
12
+ its(:group) { should_not be_nil }
13
+ end
14
+
15
+ describe ".daily" do
16
+ subject { described_class.daily ticker, :from => start_date, :to => end_date }
17
+ it_behaves_like "a valid instance"
18
+ its(:group) { should == YahooFinance::Historical::GROUPS[:daily] }
19
+ end
20
+
21
+ describe ".weekly" do
22
+ subject { described_class.weekly ticker, :from => start_date, :to => end_date }
23
+ it_behaves_like "a valid instance"
24
+ its(:group) { should == YahooFinance::Historical::GROUPS[:weekly] }
25
+ end
26
+
27
+ describe ".monthly" do
28
+ subject { described_class.monthly ticker, :from => start_date, :to => end_date }
29
+ it_behaves_like "a valid instance"
30
+ its(:group) { should == YahooFinance::Historical::GROUPS[:monthly] }
31
+ end
32
+
33
+ describe "#each" do
34
+ subject { described_class.daily ticker, :from => start_date, :to => end_date }
35
+
36
+ it "iterates by ascending date order" do
37
+ subject.to_a.first.date.should < subject.to_a.last.date
38
+ end
39
+
40
+ context "when a period is specified" do
41
+ it "returns only records after the start date" do
42
+ subject.all? { |data| data.date >= start_date }.should be_true
43
+ end
44
+
45
+ it "returns only records until the end date" do
46
+ subject.all? { |data| data.date <= end_date }.should be_true
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1 @@
1
+ require "yahoo_finance"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "yahoo_finance/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "yahoo_finance"
7
+ s.version = YahooFinance::VERSION
8
+ s.authors = ["Rodrigo Navarro"]
9
+ s.email = ["reu@rnavarro.com.br"]
10
+ s.homepage = ""
11
+ s.summary = %q{Simple wrapper for yahoo finance API}
12
+ s.description = %q{Simple wrapper for yahoo finance API}
13
+
14
+ s.rubyforge_project = "yahoo_finance"
15
+
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
+ s.add_development_dependency "rspec"
22
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yahoo_finance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rodrigo Navarro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2152396840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152396840
25
+ description: Simple wrapper for yahoo finance API
26
+ email:
27
+ - reu@rnavarro.com.br
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - lib/yahoo_finance.rb
36
+ - lib/yahoo_finance/historical.rb
37
+ - lib/yahoo_finance/version.rb
38
+ - spec/historical_spec.rb
39
+ - spec/spec_helper.rb
40
+ - yahoo_finance.gemspec
41
+ homepage: ''
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ hash: -2453551953507377743
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ segments:
63
+ - 0
64
+ hash: -2453551953507377743
65
+ requirements: []
66
+ rubyforge_project: yahoo_finance
67
+ rubygems_version: 1.8.6
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Simple wrapper for yahoo finance API
71
+ test_files:
72
+ - spec/historical_spec.rb
73
+ - spec/spec_helper.rb