yafin 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,16 +6,20 @@ Yafin is a gem to pull quotes from Yahoo finance.
6
6
  Usage
7
7
  -----
8
8
 
9
+ Get the latest quotes for an array of stock symbols
9
10
  ```ruby
10
- y = Yafin.new
11
+ Yafin::Fetcher.get_latest_quotes(["goog", "aapl"])
12
+ ```
13
+
14
+ Get historical quotes for a stock symbol
15
+ ```ruby
16
+ Yafin::Fetcher.get_historical_quotes("goog")
11
17
 
12
- quotes = y.get_latest_quotes(["goog", "aapl"])
18
+ Yafin::Fetcher.get_historical_quotes("goog", { start_year: 2011, start_month: 2, start_day: 1 } )
13
19
 
14
- historical_quotes = y.get_historical_quotes("goog", "2011", "2", "1", "2011", "3", "1")
20
+ Yafin::Fetcher.get_historical_quotes("goog", { start_year: 2011, start_month: 2, start_day: 1 }, { to_year: 2011, to_month: 2, to_day: 2 } )
15
21
  ```
16
22
 
17
- Syntax is symbol, start_year=2000, start_month=1, start_day=1, to_year=2012, to_month=1, to_day=1
18
-
19
23
  Quotes is an array of Yafin::Quote and historical_quotes is an array of Yafin::Historical_quotes
20
24
 
21
25
  Contributing to yafin
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,46 +1,47 @@
1
1
  require 'csv'
2
2
  require 'net/http'
3
3
 
4
- class Yafin
5
- def get_latest_quotes symbols
6
- # Enter symbols to fetch at f=
7
- # s = symbol
8
- # n = name
9
- # l1 = last trade (price only)
10
- # c1 = Change
11
- # p2 = Change in percent
12
- # x = Stock exchange
13
- #
14
- # Full list at http://www.gummy-stuff.org/Yahoo-data.htm
15
-
16
- ####### GOOG RESPONSE ##########################################
17
- # s n l1 c1 p2 x
18
- # ["GOOG", "Google Inc.", 585.245, -0.735, "-0.13%", "NasdaqNM"]
19
- ####### GOOG RESPONSE ##########################################
20
-
21
- sym = symbols.join("+")
22
-
23
- resp = Net::HTTP.get_response(URI.parse("http://download.finance.yahoo.com/d/quotes.csv?s=#{sym}&f=snl1c1p2x&e=.csv"))
24
- quotes = resp.body.split(/\r\n/)
25
-
26
- quotes.inject([]) do |sum, q|
27
- sum << Quote.new(CSV.parse(q).flatten)
4
+ module Yafin
5
+ class Fetcher
6
+ def self.get_latest_quotes symbols
7
+ # Enter symbols to fetch at f=
8
+ # s = symbol
9
+ # n = name
10
+ # l1 = last trade (price only)
11
+ # c1 = Change
12
+ # p2 = Change in percent
13
+ # x = Stock exchange
14
+ #
15
+ # Full list at http://www.gummy-stuff.org/Yahoo-data.htm
16
+
17
+ ####### GOOG RESPONSE ##########################################
18
+ # s n l1 c1 p2 x
19
+ # ["GOOG", "Google Inc.", 585.245, -0.735, "-0.13%", "NasdaqNM"]
20
+ ####### GOOG RESPONSE ##########################################
21
+
22
+ sym = symbols.join("+")
23
+
24
+ resp = Net::HTTP.get_response(URI.parse("http://download.finance.yahoo.com/d/quotes.csv?s=#{sym}&f=snl1c1p2x&e=.csv"))
25
+ quotes = resp.body.split(/\r\n/)
26
+
27
+ quotes.inject([]) do |sum, q|
28
+ sum << Quote.new(CSV.parse(q).flatten)
29
+ end
28
30
  end
29
- end
30
31
 
31
- def get_historical_quotes symbol, start_year=2000, start_month=1, start_day=1, to_year=2012, to_month=1, to_day=1
32
+ def self.get_historical_quotes symbol, start_hash={start_year: 2010, start_month: 1, start_day: 1}, end_hash={to_year: 2012, to_month: 1, to_day: 1 }
33
+
34
+ # http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=31&f=2010
35
+ # "Date,Open,High,Low,Close,Volume,Adj Close"
32
36
 
33
- # http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=31&f=2010
34
- # "Date,Open,High,Low,Close,Volume,Adj Close"
35
-
36
- resp = Net::HTTP.get_response(URI.parse("http://ichart.yahoo.com/table.csv?s=#{symbol}&a=#{start_month.to_i - 1}&b=#{start_day}&c=#{start_year}&d=#{to_month.to_i - 1}&e=#{to_day}&f=#{to_year}"))
37
+ resp = Net::HTTP.get_response(URI.parse("http://ichart.yahoo.com/table.csv?s=#{symbol}&a=#{start_hash[:start_month].to_i - 1}&b=#{start_hash[:start_day]}&c=#{start_hash[:start_year]}&d=#{end_hash[:to_month].to_i - 1}&e=#{end_hash[:to_day]}&f=#{end_hash[:to_year]}"))
37
38
 
38
- quotes = resp.body.split(/\n/)
39
+ quotes = resp.body.split(/\n/)
39
40
 
40
- quotes.delete_at 0 #Strip headings from response
41
+ quotes.delete_at 0 #Strip headings from response
41
42
 
42
- sum = quotes.inject([]) { |sum, q| sum << HistoricalQuote.new(CSV.parse(q).flatten, symbol) }
43
-
43
+ sum = quotes.inject([]) { |sum, q| sum << HistoricalQuote.new(CSV.parse(q).flatten, symbol) }
44
+ end
44
45
  end
45
46
  end
46
47
 
@@ -3,16 +3,16 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe Yafin do
4
4
  describe "#get_latest_quotes" do
5
5
  it "responds to #get_latest_quotes" do
6
- subject.should respond_to(:get_latest_quotes)
6
+ Yafin::Fetcher.should respond_to(:get_latest_quotes)
7
7
  end
8
8
 
9
9
  it "should return quotes for stock symbol" do
10
- goog_quote = subject.get_latest_quotes(["goog"])
10
+ goog_quote = Yafin::Fetcher.get_latest_quotes(["goog"])
11
11
  goog_quote.first.symbol.should == "GOOG"
12
12
  end
13
13
 
14
14
  it "should return quotes for stock symbols" do
15
- quotes = subject.get_latest_quotes(["goog", "aapl"])
15
+ quotes = Yafin::Fetcher.get_latest_quotes(["goog", "aapl"])
16
16
  quotes.first.symbol.should == "GOOG"
17
17
  quotes.last.symbol.should == "AAPL"
18
18
  end
@@ -20,15 +20,22 @@ describe Yafin do
20
20
 
21
21
  describe "#get_historical_quotes" do
22
22
  it "responds to #get_historical_quotes" do
23
- subject.should respond_to(:get_historical_quotes)
23
+ Yafin::Fetcher.should respond_to(:get_historical_quotes)
24
24
  end
25
25
 
26
- before(:each) do
27
- @quotes = subject.get_historical_quotes("goog", "2011", "2", "1", "2011", "3", "1")
26
+ it "gets historical quotes based on default values for a single stock when not given a timespan" do
27
+ quotes = Yafin::Fetcher.get_historical_quotes("goog")
28
+ quotes.count.should == 504
28
29
  end
29
30
 
30
- it "gets historical quotes for a single stock given a timespan" do
31
- @quotes.count.should == 20
31
+ it "gets historical quotes when given a hash of start dates" do
32
+ quotes = Yafin::Fetcher.get_historical_quotes("goog", { start_year: 2011, start_month: 2, start_day: 1 } )
33
+ quotes.count.should == 232
34
+ end
35
+
36
+ it "gets historical quotes when given a hash of start dates and a hash of end dates" do
37
+ quotes = Yafin::Fetcher.get_historical_quotes("goog", { start_year: 2011, start_month: 2, start_day: 1 }, { to_year: 2011, to_month: 2, to_day: 2 } )
38
+ quotes.count.should == 2
32
39
  end
33
40
  end
34
41
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "yafin"
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Patrik Bj\u{f6}rklund"]
12
- s.date = "2012-08-04"
12
+ s.date = "2012-08-06"
13
13
  s.description = "Pulls current and historical data from Yahoo Finance. Requires net/http and csv"
14
14
  s.email = "p.bjorklund@gmail.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yafin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-04 00:00:00.000000000 Z
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -129,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  segments:
131
131
  - 0
132
- hash: 1815973857855434609
132
+ hash: -3737714781941844121
133
133
  required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  none: false
135
135
  requirements: