yafin 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +9 -5
- data/VERSION +1 -1
- data/lib/yafin.rb +35 -34
- data/spec/yafin_spec.rb +15 -8
- data/yafin.gemspec +2 -2
- metadata +3 -3
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
|
-
|
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
|
-
|
18
|
+
Yafin::Fetcher.get_historical_quotes("goog", { start_year: 2011, start_month: 2, start_day: 1 } )
|
13
19
|
|
14
|
-
|
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.2.0
|
data/lib/yafin.rb
CHANGED
@@ -1,46 +1,47 @@
|
|
1
1
|
require 'csv'
|
2
2
|
require 'net/http'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
39
|
+
quotes = resp.body.split(/\n/)
|
39
40
|
|
40
|
-
|
41
|
+
quotes.delete_at 0 #Strip headings from response
|
41
42
|
|
42
|
-
|
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
|
|
data/spec/yafin_spec.rb
CHANGED
@@ -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
|
-
|
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 =
|
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 =
|
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
|
-
|
23
|
+
Yafin::Fetcher.should respond_to(:get_historical_quotes)
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
|
-
|
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
|
31
|
-
|
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
|
data/yafin.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "yafin"
|
8
|
-
s.version = "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-
|
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.
|
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-
|
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:
|
132
|
+
hash: -3737714781941844121
|
133
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
134
|
none: false
|
135
135
|
requirements:
|