stocktracker 0.0.2 → 0.0.3

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 ADDED
@@ -0,0 +1,2 @@
1
+ A simple Yahoo Finance wrapper to return current and historic stock quotes.
2
+
@@ -1,3 +1,3 @@
1
1
  module Stocktracker
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,11 +1,132 @@
1
+ require 'open-uri'
2
+ require 'csv'
3
+
4
+
5
+ # modified version of YahooFinance gem by Nicholas Rahn <nick at transparentech.com>
6
+
1
7
  module YahooFinance
2
- class StandardQuote
3
- def values
4
- ret = {}
5
- @formathash.each_value do |val|
6
- ret[val[0]] = send( val[0] ) unless send( val[0] ) == nil
8
+
9
+ class CurrentQuote
10
+
11
+ attr_accessor :symbol, :results
12
+
13
+ CURRENT_FORMAT = {
14
+ "s" => :symbol,
15
+ "n" => :name,
16
+ "l1" => [ :last_trade, "to_f" ],
17
+ "d1" => :date,
18
+ "t1" => :time,
19
+ "c" => :change,
20
+ "c1" => [ :change_points, "to_f" ],
21
+ "p2" => [ :change_percent, "to_f" ],
22
+ "p" => [ :previous_close, "to_f" ],
23
+ "o" => [ :open, "to_f" ],
24
+ "h" => [ :day_high, "to_f" ],
25
+ "g" => [ :day_low, "to_f" ],
26
+ "v" => [ :volume, "to_i" ],
27
+ "m" => :day_range,
28
+ "l" => :last_trade_with_time,
29
+ "t7" => :ticker_trend,
30
+ "a2" => [ :average_daily_volume, "to_i" ],
31
+ "b" => [ :bid, "to_f" ],
32
+ "a" => [ :ask, "to_f" ]
33
+ }
34
+
35
+ def initialize(symbol)
36
+ self.symbol= symbol
37
+ self.results= get_current_quote
38
+ end
39
+
40
+ def get_current_quote
41
+ current_map(yahoo_quote)
42
+ end
43
+
44
+ def yahoo_quote
45
+ open(yahoo_current_url).read
46
+ end
47
+
48
+ def yahoo_current_url
49
+ "http://download.finance.yahoo.com/d/quotes.csv?s=#{self.symbol}&f=#{current_format}"
50
+ end
51
+
52
+ def current_format
53
+ CURRENT_FORMAT.keys.join
54
+ end
55
+
56
+ def current_values(quote)
57
+ CSV.parse(yahoo_quote)[0]
58
+ end
59
+
60
+ def current_map(quote)
61
+ map = {}
62
+ values = current_values(quote)
63
+ CURRENT_FORMAT.each_with_index do |v,i|
64
+ if v[1].is_a?(Array)
65
+ map[v[1][0]] = values[i].send(v[1][1])
66
+ else
67
+ map[v[1]] = values[i]
68
+ end
69
+ end
70
+ map
71
+ end
72
+ end
73
+
74
+ class PastQuote
75
+
76
+ attr_accessor :symbol, :results, :start_date, :end_date
77
+
78
+ PAST_FORMAT = [
79
+ :date,
80
+ [:open,"to_f"],
81
+ [:high,"to_f"],
82
+ [:low,"to_f"],
83
+ [:close,"to_f"],
84
+ [:volume,"to_f"],
85
+ [:adj_close,"to_f"]
86
+ ]
87
+
88
+ def initialize(symbol, date)
89
+ self.symbol= symbol
90
+ self.start_date= Date.parse(date)
91
+ self.end_date= Date.parse(date)
92
+ self.results= get_past_quote
93
+ end
94
+
95
+ def get_past_quote
96
+ past_map(yahoo_quote)
97
+ end
98
+
99
+ def yahoo_quote
100
+ open(yahoo_past_url).read
101
+ end
102
+
103
+ def yahoo_past_url
104
+ "http://itable.finance.yahoo.com/table.csv?"+
105
+ "s=#{symbol}&g=d&"+
106
+ "a=#{start_date.month-1}&"+
107
+ "b=#{start_date.mday}&"+
108
+ "c=#{start_date.year}&"+
109
+ "d=#{end_date.month-1}&"+
110
+ "e=#{end_date.mday}&"+
111
+ "f=#{end_date.year}"
112
+ end
113
+
114
+
115
+ def past_values(quote)
116
+ CSV.parse(yahoo_quote)[1]
117
+ end
118
+
119
+ def past_map(quote)
120
+ map = {}
121
+ values = past_values(quote)
122
+ PAST_FORMAT.each_with_index do |v,i|
123
+ if v.is_a?(Array)
124
+ map[v[0]] = values[i].send(v[1])
125
+ else
126
+ map[v] = values[i]
127
+ end
7
128
  end
8
- ret
129
+ map
9
130
  end
10
131
  end
11
- end
132
+ end
data/lib/stocktracker.rb CHANGED
@@ -1,47 +1,38 @@
1
1
  require "stocktracker/version"
2
- require "yahoofinance"
3
2
  require "stocktracker/yahoofinance"
4
3
 
5
4
  module StockTracker
6
5
  class CurrentQuote
6
+ include YahooFinance
7
7
 
8
8
 
9
9
  attr_accessor :symbol, :results
10
10
  def initialize(symbol)
11
- self.symbol = symbol
11
+ self.symbol = symbol.upcase
12
12
  self.results = yahoo_quote
13
13
  end
14
14
 
15
15
  private
16
16
 
17
17
  def yahoo_quote
18
- YahooFinance::get_quotes(YahooFinance::StandardQuote, symbol)[symbol].values
18
+ YahooFinance::CurrentQuote.new(symbol).results
19
19
  end
20
20
  end
21
21
 
22
- class HistoricalQuote
23
- HISTORIC_ROWS = ["Date","Open","High","Low","Close","Volume","Adjusted Close"]
22
+ class PastQuote
23
+ include YahooFinance
24
24
 
25
- attr_accessor :symbol, :start_date, :end_date, :results
26
- def initialize(symbol, start_date, end_date)
25
+ attr_accessor :symbol, :date, :results
26
+ def initialize(symbol, date)
27
27
  self.symbol = symbol
28
- self.start_date = start_date
29
- self.end_date = end_date
28
+ self.date = date
30
29
  self.results = yahoo_historical_quote
31
30
  end
32
31
 
33
32
  private
34
33
 
35
34
  def yahoo_historical_quote
36
- map_rows(YahooFinance::get_historical_quotes(symbol, start_date, end_date))
37
- end
38
-
39
- def map_rows(results)
40
- map = {}
41
- results[0].each_with_index do |r, i|
42
- map[HISTORIC_ROWS[i]] = r.to_f
43
- end
44
- map
35
+ YahooFinance::PastQuote.new(symbol, date).results
45
36
  end
46
37
 
47
38
  end
@@ -8,24 +8,24 @@ describe StockTracker do
8
8
  end
9
9
  it "will return a current quote for Google" do
10
10
  @quote.results.should be
11
- @quote.results["symbol"].should == "GOOG"
11
+ @quote.results[:symbol].should == "GOOG"
12
12
  end
13
13
 
14
14
  it 'will have a close greater than 0' do
15
- @quote.results["lastTrade"].should > 0
15
+ @quote.results[:last_trade].should > 0
16
16
  end
17
17
  end
18
18
 
19
- context :historical do
19
+ context :past do
20
20
  before(:each) do
21
- @quote = StockTracker::HistoricalQuote.new("GOOG", Date.parse('01/10/2010'), Date.parse('01/10/2010'))
21
+ @quote = StockTracker::PastQuote.new("GOOG", '01/10/2010')
22
22
  end
23
23
  it "will return a current quote for Google" do
24
24
  @quote.results.should be
25
25
  end
26
26
 
27
27
  it 'will have a close greater than 0' do
28
- @quote.results["Close"].should > 0
28
+ @quote.results[:close].should > 0
29
29
  end
30
30
  end
31
31
  end
data/stocktracker.gemspec CHANGED
@@ -21,5 +21,4 @@ Gem::Specification.new do |s|
21
21
  # specify any dependencies here; for example:
22
22
  s.add_development_dependency 'rake'
23
23
  s.add_development_dependency 'rspec'
24
- s.add_runtime_dependency "yahoofinance"
25
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stocktracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &2158448960 !ruby/object:Gem::Requirement
16
+ requirement: &2166372700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2158448960
24
+ version_requirements: *2166372700
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2158448540 !ruby/object:Gem::Requirement
27
+ requirement: &2166372280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,18 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2158448540
36
- - !ruby/object:Gem::Dependency
37
- name: yahoofinance
38
- requirement: &2158448120 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
44
- type: :runtime
45
- prerelease: false
46
- version_requirements: *2158448120
35
+ version_requirements: *2166372280
47
36
  description: Returns Yahoo Finance Quotes for stock symbols
48
37
  email:
49
38
  - aaronworsham@gmail.com
@@ -53,6 +42,7 @@ extra_rdoc_files: []
53
42
  files:
54
43
  - .gitignore
55
44
  - Gemfile
45
+ - README
56
46
  - Rakefile
57
47
  - lib/stocktracker.rb
58
48
  - lib/stocktracker/version.rb