securities 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -1
- data/lib/securities/scraper.rb +22 -4
- data/lib/securities/stock.rb +9 -9
- data/lib/securities/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Securities
|
2
2
|
|
3
3
|
Financial information scraper gem.
|
4
|
-
Uses Yahoo Finance API.
|
4
|
+
Uses Yahoo Finance API. Current functionality demo: http://strangemuseum.heroku.com
|
5
5
|
|
6
6
|
[![Build Status](https://secure.travis-ci.org/Nedomas/securities.png)](http://travis-ci.org/Nedomas/securities)[![Build Status](https://gemnasium.com/Nedomas/securities.png)](https://gemnasium.com/Nedomas/securities)
|
7
7
|
|
@@ -74,6 +74,10 @@ Results are returned in a hash:
|
|
74
74
|
|
75
75
|
## To do:
|
76
76
|
|
77
|
+
* Fix monthly+ and dividends+
|
78
|
+
* Fix exception messages for friendlier displaying to end-user+ (and invalid symbol error=>"Illegal quoting in line 1.")+.
|
79
|
+
* Rescue empty results error.+
|
80
|
+
* Let it raise exception on one symbol, but continue on other and send the message.
|
77
81
|
* Write specs.
|
78
82
|
* Add quote info (P/E, P/S, etc.)
|
79
83
|
* Add symbol from name lookup.
|
data/lib/securities/scraper.rb
CHANGED
@@ -37,12 +37,30 @@ module Securities
|
|
37
37
|
end
|
38
38
|
|
39
39
|
# Skip first line because it contains headers with Date,Open,High,Low,Close,Volume,Adj Close
|
40
|
-
|
40
|
+
# Check for errors during CSV parsing.
|
41
|
+
begin
|
42
|
+
csv = CSV.parse(get, :headers => true)
|
43
|
+
rescue => error
|
44
|
+
# PROBABLY an invalid symbol specified or there was some other way the parser couldn't read a CSV.
|
45
|
+
# FIXME: Let it raise exception on one symbol, but continue on other and send the message.
|
46
|
+
raise ScraperException, "Invalid symbol '#{symbol}' specified."
|
47
|
+
end
|
41
48
|
|
42
49
|
data = Array.new
|
43
|
-
csv.each_with_index
|
44
|
-
|
45
|
-
|
50
|
+
csv.each_with_index do |row, index|
|
51
|
+
line = Hash.new
|
52
|
+
csv.headers.each_with_index do |header, i|
|
53
|
+
# Set headers as keys for data hash.
|
54
|
+
line[header.parameterize.underscore.to_sym] = row[i]
|
55
|
+
data[index] = line
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# FIXME: Let it raise exception on one symbol, but continue on other and send the message.
|
60
|
+
if data.empty?
|
61
|
+
raise ScraperException, 'There were no results for these parameters.'
|
62
|
+
end
|
63
|
+
|
46
64
|
@results[symbol] = data
|
47
65
|
end
|
48
66
|
return @results
|
data/lib/securities/stock.rb
CHANGED
@@ -11,7 +11,7 @@ module Securities
|
|
11
11
|
attr_reader :symbols
|
12
12
|
# REGEX for YYYY-MM-DD
|
13
13
|
DATE_REGEX = /^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/
|
14
|
-
PERIODS_ARRAY = [:daily, :weekly, :
|
14
|
+
PERIODS_ARRAY = [:daily, :weekly, :monthly, :dividends]
|
15
15
|
|
16
16
|
# Error handling
|
17
17
|
class StockException < StandardError
|
@@ -93,7 +93,7 @@ module Securities
|
|
93
93
|
@symbols = parameters.reject(&:empty?)
|
94
94
|
|
95
95
|
unless !@symbols.empty?
|
96
|
-
raise StockException, 'You must specify stock
|
96
|
+
raise StockException, 'You must specify at least one stock symbol.'
|
97
97
|
end
|
98
98
|
|
99
99
|
# FIXME: A kinda hacky way to check if parameters are a nested array (when accepting an array as a symbols argument).
|
@@ -101,7 +101,7 @@ module Securities
|
|
101
101
|
@symbols[0].is_a?(Array) ? @symbols = @symbols[0] : nil
|
102
102
|
|
103
103
|
unless @symbols.uniq.length == @symbols.length
|
104
|
-
raise StockException, 'Duplicate symbols given.'
|
104
|
+
raise StockException, 'Duplicate stock symbols given.'
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
@@ -111,30 +111,30 @@ module Securities
|
|
111
111
|
end
|
112
112
|
|
113
113
|
unless parameters.has_keys?(:start_date, :end_date)
|
114
|
-
raise StockException, 'You must specify
|
114
|
+
raise StockException, 'You must specify start date and end date.'
|
115
115
|
end
|
116
116
|
|
117
117
|
unless DATE_REGEX.match(parameters[:start_date])
|
118
|
-
raise StockException, 'Invalid
|
118
|
+
raise StockException, 'Invalid start date specified. Format YYYY-MM-DD.'
|
119
119
|
end
|
120
120
|
|
121
121
|
unless DATE_REGEX.match(parameters[:end_date])
|
122
|
-
raise StockException, 'Invalid
|
122
|
+
raise StockException, 'Invalid end date specified. Format YYYY-MM-DD.'
|
123
123
|
end
|
124
124
|
|
125
125
|
unless parameters[:start_date].to_date < parameters[:end_date].to_date
|
126
|
-
raise StockException, '
|
126
|
+
raise StockException, 'End date must be greater than the start date.'
|
127
127
|
end
|
128
128
|
|
129
129
|
unless parameters[:start_date].to_date < Date.today
|
130
|
-
raise StockException, '
|
130
|
+
raise StockException, 'Start date must not be in the future.'
|
131
131
|
end
|
132
132
|
|
133
133
|
# Set to default :periods if key isn't specified.
|
134
134
|
parameters[:periods] = :daily if !parameters.has_key?(:periods)
|
135
135
|
|
136
136
|
unless PERIODS_ARRAY.include?(parameters[:periods])
|
137
|
-
raise StockException, 'Invalid
|
137
|
+
raise StockException, 'Invalid periods value specified.'
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
data/lib/securities/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: securities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
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-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -78,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
78
|
version: '0'
|
79
79
|
segments:
|
80
80
|
- 0
|
81
|
-
hash:
|
81
|
+
hash: -2122445770564870703
|
82
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
87
|
version: '0'
|
88
88
|
segments:
|
89
89
|
- 0
|
90
|
-
hash:
|
90
|
+
hash: -2122445770564870703
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
93
|
rubygems_version: 1.8.23
|