securities 0.1.2 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  # Securities
2
2
 
3
3
  Financial information scraper gem.
4
- Uses Yahoo Finance API. Current functionality demo: http://strangemuseum.heroku.com
4
+ Uses Yahoo Finance API. Current functionality demo of this gem, working in synergy with gem ta: 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
 
@@ -35,11 +35,15 @@ You can get stock information with commands:
35
35
 
36
36
  Optional parameter :periods accepts :daily, :weekly, :monthly, :dividend. If not specified, it defaults to :daily.
37
37
 
38
+ :end_date defaults to Date.today if not specified.
39
+
38
40
  You can access hash for a single stock with:
39
41
 
40
- my_data.results["yhoo"]
42
+ my_data["yhoo"]
43
+
44
+ or my_stocks.output["yhoo"]
41
45
 
42
- Results are returned in a hash:
46
+ Output is returned in a hash:
43
47
 
44
48
  {"aapl"=>
45
49
  [{:date=>"2012-01-04",
@@ -72,18 +76,31 @@ Results are returned in a hash:
72
76
  :volume=>"19708600",
73
77
  :adj_close=>"16.29"}]}
74
78
 
75
- ## To do:
79
+ # Version 1.0.0
80
+
81
+ Results are returned in a reversed manner from 0.1.2. Array begins from the oldest data points.
82
+
83
+ Only Stock class initializes an object so you can do:
76
84
 
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
- * BUG: Let it raise exception on one symbol, but continue on other and send the message. <= Now it just returns an empty array if the exception wasn't fatal.+
85
+ my_stocks = Securities::Stock.new(["aapl", "yhoo"])
86
+ my_stocks.history(:start_date => '2012-01-01', :end_date => '2012-02-01', :periods => :weekly)
87
+ ^ adds @ouput variable to the my_stocks object. ^
88
+
89
+ And get output with:
90
+ my_stocks.output or my_stocks.output["aapl"]
91
+
92
+ Given symbols, dates and periods with:
93
+ my_stocks.symbols
94
+ my_stocks.start_date
95
+ my_stocks.end_date
96
+ my_stocks.periods
97
+
98
+ ## To do:
81
99
 
82
100
  * Write specs.
83
101
  * Add quote info (P/E, P/S, etc.)
84
102
  * Add symbol from name lookup.
85
103
  * Add options support.
86
- * Add technical analysis module (or a seperate gem which works in synergy with this).
87
104
 
88
105
  ## Contributing
89
106
 
@@ -9,22 +9,23 @@ module Securities
9
9
  #
10
10
  class Scraper
11
11
 
12
- attr_reader :results
13
12
  # Error handling
14
13
  class ScraperException < StandardError
15
14
  end
16
15
 
17
- def initialize type, parameters
18
- @results = Hash.new
16
+ def self.get type, parameters
19
17
 
20
18
  # Manage different type requests.
21
19
  case type
22
- when :history then @results = scrape_history(parameters)
20
+ when :history then results = scrape_history(parameters)
23
21
  else raise ScraperException, 'Cannot determine request type.'
24
22
  end
23
+
24
+ return results
25
25
  end
26
26
 
27
- def scrape_history parameters
27
+ def self.scrape_history parameters
28
+ results = Hash.new
28
29
  parameters.each do |symbol, url|
29
30
 
30
31
  uri = URI.parse(url)
@@ -42,11 +43,10 @@ module Securities
42
43
  csv = CSV.parse(get, :headers => true)
43
44
  rescue => error
44
45
  # 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
46
  # It will no longer raise exception because it causes a bug.
47
47
  # bug => if exception occurs in one symbol, it will not give any results for any other.
48
48
  # raise ScraperException, "Invalid symbol '#{symbol}' specified."
49
- @results[symbol] = []
49
+ results[symbol] = []
50
50
  next
51
51
  end
52
52
 
@@ -60,16 +60,15 @@ module Securities
60
60
  end
61
61
  end
62
62
 
63
- # FIXME: Let it raise exception on one symbol, but continue on other and send the message.
64
63
  # It will no longer raise exception if data is empty, because it causes a
65
64
  # bug => if exception occurs in one symbol, it will not give any results for any other.
66
65
  # if data.empty?
67
66
  # raise ScraperException, "There were no results for #{symbol}."
68
67
  # end
69
68
 
70
- @results[symbol] = data
69
+ results[symbol] = data.reverse
71
70
  end
72
- return @results
71
+ return results
73
72
  end
74
73
 
75
74
  end
@@ -4,11 +4,11 @@ module Securities
4
4
  # :periods accepts :daily, :weekly, :monthly, :dividend. If not specified, it defaults to :daily.
5
5
  #
6
6
  # You can access hash for a single stock with:
7
- # my_stocks.results["yhoo"]
7
+ # my_stocks["yhoo"]
8
8
 
9
9
  class Stock
10
10
 
11
- attr_reader :symbols
11
+ attr_accessor :symbols, :output, :start_date, :end_date, :periods
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
14
  PERIODS_ARRAY = [:daily, :weekly, :monthly, :dividends]
@@ -26,11 +26,15 @@ module Securities
26
26
  parameters[:symbols] = @symbols
27
27
  validate_history(parameters)
28
28
  urls = generate_history_url(parameters)
29
- Securities::Scraper.new(type, urls)
29
+ @start_date = parameters[:start_date]
30
+ @end_date = parameters[:end_date]
31
+ @periods = parameters[:periods]
32
+ @output = Securities::Scraper.get(type, urls)
30
33
  end
31
34
 
32
35
  private
33
36
 
37
+ #
34
38
  # History URL generator
35
39
  #
36
40
  # Generating URL for various types of requests within stocks.
@@ -92,7 +96,7 @@ module Securities
92
96
  # Reject empty symbol hashes.
93
97
  @symbols = parameters.reject(&:empty?)
94
98
 
95
- unless !@symbols.empty?
99
+ if @symbols.nil?
96
100
  raise StockException, 'You must specify at least one stock symbol.'
97
101
  end
98
102
 
@@ -110,10 +114,14 @@ module Securities
110
114
  raise StockException, 'Given parameters have to be a hash.'
111
115
  end
112
116
 
113
- unless parameters.has_keys?(:start_date, :end_date)
114
- raise StockException, 'You must specify start date and end date.'
117
+ unless parameters.has_key?(:end_date)
118
+ parameters[:end_date] = Date.today.strftime("%Y-%m-%d")
119
+ end
120
+
121
+ unless parameters.has_key?(:start_date)
122
+ raise StockException, 'Start date must be specified.'
115
123
  end
116
-
124
+
117
125
  unless DATE_REGEX.match(parameters[:start_date])
118
126
  raise StockException, 'Invalid start date specified. Format YYYY-MM-DD.'
119
127
  end
@@ -1,3 +1,3 @@
1
1
  module Securities
2
- VERSION = "0.1.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Securities::Stock do
4
+ describe ".new" do
5
+
6
+ it "should raise error when no parameter is passed" do
7
+ expect { Securities::Stock.new }.should raise_error
8
+ end
9
+
10
+ end
11
+ end
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.2
4
+ version: 1.0.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-27 00:00:00.000000000 Z
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -62,8 +62,8 @@ files:
62
62
  - lib/securities/stock.rb
63
63
  - lib/securities/version.rb
64
64
  - securities.gemspec
65
- - spec/securities_spec.rb
66
65
  - spec/spec_helper.rb
66
+ - spec/stock_spec.rb
67
67
  homepage: https://github.com/Nedomas/securities
68
68
  licenses: []
69
69
  post_install_message:
@@ -78,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
78
  version: '0'
79
79
  segments:
80
80
  - 0
81
- hash: 254096546998731436
81
+ hash: -3860670281301913454
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: 254096546998731436
90
+ hash: -3860670281301913454
91
91
  requirements: []
92
92
  rubyforge_project:
93
93
  rubygems_version: 1.8.24
@@ -95,5 +95,5 @@ signing_key:
95
95
  specification_version: 3
96
96
  summary: Financial information scraper gem. Uses Yahoo Finance API.
97
97
  test_files:
98
- - spec/securities_spec.rb
99
98
  - spec/spec_helper.rb
99
+ - spec/stock_spec.rb
@@ -1,5 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Securities do
4
- it 'requires tests'
5
- end