ticker 0.1

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.
@@ -0,0 +1,46 @@
1
+ #ticker
2
+
3
+ * a simple gem for gathering and streaming stock data
4
+
5
+ ##Installation
6
+
7
+ If you don't have gemcutter
8
+
9
+ gem install gemcutter
10
+ gem tumble
11
+
12
+ Otherwise
13
+
14
+ gem install ticker
15
+
16
+ Or
17
+
18
+ gem install ticker -s http://gemcutter.org
19
+
20
+
21
+ ##Docs
22
+
23
+ See RDoc
24
+
25
+ ##Quick Example
26
+
27
+ require 'rubygems'
28
+ require 'ticker'
29
+
30
+ quote = Ticker::Quote.new("GOOG")
31
+ quote.update
32
+
33
+ #print all data
34
+ quote.print
35
+
36
+ #print specific things
37
+ p quote.ask
38
+ p quote.bid
39
+
40
+
41
+ #only gather a few things
42
+ quote = Ticker::Quote.new("GOOG", :only => [:ask, :bid, :pe_ratio, :ask_size])
43
+ quote.update
44
+
45
+ # print Ask, Bid, P/E Ratio, and Ask Size
46
+ quote.print
@@ -0,0 +1,16 @@
1
+ $:.unshift File.dirname(__FILE__) + "/../lib"
2
+
3
+ require "rubygems"
4
+ require "ticker"
5
+
6
+ quote = Ticker::Quote.new("AAPL")
7
+ quote.update
8
+ quote.print
9
+
10
+ p "--------------------"
11
+
12
+ quote = Ticker::Quote.new("GOOG", :only => [:ask, :bid])
13
+ quote.update
14
+ quote.print
15
+
16
+ p "--------------------"
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+ require "open-uri"
3
+ require "csv"
4
+
5
+ module Ticker
6
+ end
7
+
8
+ require "ticker/quote.rb"
@@ -0,0 +1,197 @@
1
+ # Author:: Dan Simpson
2
+ # Copyright:: Copyright (c) 2010 Dan Simpson
3
+ # License:: Distributes under the same terms as Ruby
4
+
5
+
6
+ module Ticker
7
+
8
+ # A representation of a stock quote
9
+ class Quote
10
+
11
+ # available tags or bits of information for a
12
+ # given stock
13
+ @@tag_titles = {
14
+ :a => "Ask",
15
+ :a2 => "Average Daily Volume",
16
+ :a5 => "Ask Size",
17
+ :b => "Bid",
18
+ :b2 => "Ask (Real-time)",
19
+ :b3 => "Bid (Real-time)",
20
+ :b4 => "Book Value",
21
+ :b6 => "Bid Size",
22
+ :c => "Change & Percent Change",
23
+ :c1 => "Change",
24
+ :c3 => "Commission",
25
+ :c6 => "Change (Real-time)",
26
+ :c8 => "After Hours Change (Real-time)",
27
+ :d => "Dividend/Share",
28
+ :d1 => "Last Trade Date",
29
+ :d2 => "Trade Date",
30
+ :e => "Earnings/Share",
31
+ :e1 => "Error Indication",
32
+ :e7 => "EPS Estimate Current Year",
33
+ :e8 => "EPS Estimate Next Year",
34
+ :e9 => "EPS Estimate Next Quarter",
35
+ :f6 => "Float Shares",
36
+ :g => "Day's Low",
37
+ :h => "Day's High",
38
+ :j => "Fifty two week Low",
39
+ :k => "Fifty two week High",
40
+ :g1 => "Holdings Gain Percent",
41
+ :g3 => "Annualized Gain",
42
+ :g4 => "Holdings Gain",
43
+ :g5 => "Holdings Gain Percent (Real-time)",
44
+ :g6 => "Holdings Gain (Real-time)",
45
+ :i => "More Info",
46
+ :i5 => "Order Book (Real-time)",
47
+ :j1 => "Market Capitalization",
48
+ :j3 => "Market Cap (Real-time)",
49
+ :j4 => "EBITDA",
50
+ :j5 => "Change From 52-week Low",
51
+ :j6 => "Percent Change From 52-week Low",
52
+ :k1 => "Last Trade (Real-time) With Time",
53
+ :k2 => "Change Percent (Real-time)",
54
+ :k3 => "Last Trade Size",
55
+ :k4 => "Change From 52-week High",
56
+ :k5 => "Percebt Change From 52-week High",
57
+ :l => "Last Trade (With Time)",
58
+ :l1 => "Last Trade (Price Only)",
59
+ :l2 => "High Limit",
60
+ :l3 => "Low Limit",
61
+ :m => "Day's Range",
62
+ :m2 => "Day's Range (Real-time)",
63
+ :m3 => "Fifty day Moving Average",
64
+ :m4 => "Two hundred day Moving Average",
65
+ :m5 => "Change From 200-day Moving Average",
66
+ :m6 => "Percent Change From 200-day Moving Average",
67
+ :m7 => "Change From 50-day Moving Average",
68
+ :m8 => "Percent Change From 50-day Moving Average",
69
+ :n => "Name",
70
+ :n4 => "Notes",
71
+ :o => "Open",
72
+ :p => "Previous Close",
73
+ :p1 => "Price Paid",
74
+ :p2 => "Change in Percent",
75
+ :p5 => "Price/Sales",
76
+ :p6 => "Price/Book",
77
+ :q => "Ex-Dividend Date",
78
+ :r => "P/E Ratio",
79
+ :r1 => "Dividend Pay Date",
80
+ :r2 => "P/E Ratio (Real-time)",
81
+ :r5 => "PEG Ratio",
82
+ :r6 => "Price/EPS Estimate Current Year",
83
+ :r7 => "Price/EPS Estimate Next Year",
84
+ :s => "Symbol",
85
+ :s1 => "Shares Owned",
86
+ :s7 => "Short Ratio",
87
+ :t1 => "Last Trade Time",
88
+ :t7 => "Ticker Trend",
89
+ :t8 => "One yr Target Price",
90
+ :v => "Volume",
91
+ :v1 => "Holdings Value",
92
+ :v7 => "Holdings Value (Real-time)",
93
+ :w => "Fifty two week Range",
94
+ :w1 => "Day's Value Change",
95
+ :w4 => "Day's Value Change (Real-time)",
96
+ :x => "Stock Exchange",
97
+ :y => "Dividend Yield"
98
+ }
99
+
100
+ # tags and their associated method names
101
+ @@tag_methods = {}
102
+
103
+ @@tag_titles.each do |key, value|
104
+ @@tag_methods[value.gsub(/[\s|-]+/, "_").gsub(/[^\w]/, "").downcase.to_sym] = key
105
+ end
106
+
107
+ attr_accessor :symbol, :values
108
+
109
+ # takes a symbol, eg: GOOG and some options
110
+ # valid options:
111
+ # * :except - Array of tags to exclude
112
+ # * :only - Array of tags to include
113
+ def initialize symbol, options={}
114
+ @symbol = symbol
115
+ @options = options
116
+ @values = {}
117
+ end
118
+
119
+ # request the data from the yahoo api, and populate
120
+ # the current object with financial data
121
+ def update
122
+ CSV.parse(open(url).read) do |row|
123
+ populate row
124
+ end
125
+ end
126
+
127
+ # the tags to request from yahoo in compressed form
128
+ def tags
129
+ return @tags if @tags
130
+
131
+ if @options.has_key? :only
132
+ @tags = convert_methods(@options[:only])
133
+ elsif @options.has_key? :except
134
+ @tags = @@tag_titles.keys - convert_methods(@options[:except])
135
+ else
136
+ @tags = @@tag_titles.keys
137
+ end
138
+
139
+ @tags
140
+ end
141
+
142
+ # examples:
143
+ # * quote.average_daily_volume
144
+ # * quote.ask
145
+ def method_missing sym, *args
146
+ if @@tag_methods.has_key?(sym) or @values.has_key?(sym)
147
+ value(sym)
148
+ else
149
+ super
150
+ end
151
+ end
152
+
153
+ # print the list of tags along with the gathered values
154
+ def print
155
+ tags.each do |tag|
156
+ puts "#{@@tag_titles[tag]} -> #{value(tag)}"
157
+ end
158
+ end
159
+
160
+ protected
161
+
162
+ def convert_methods syms
163
+ syms.collect do |sym|
164
+ @@tag_methods.has_key?(sym) ? @@tag_methods[sym] : sym
165
+ end
166
+ end
167
+
168
+ # set the underlying tag values for the symbol
169
+ def populate values
170
+ @tags.each_with_index do |tag,idx|
171
+ @values[tag] = parse_value(values[idx])
172
+ end
173
+ end
174
+
175
+ # convert the value to a number if it needs it
176
+ def parse_value value
177
+ case value
178
+ when /\d+/
179
+ value.to_f
180
+ else
181
+ value
182
+ end
183
+ end
184
+
185
+ # fetch a value from the underlying data hash
186
+ def value sym
187
+ @@tag_methods.has_key?(sym) ? value(@@tag_methods[sym]) : @values[sym]
188
+ end
189
+
190
+ # the yahoo api endpoint for stock data
191
+ def url
192
+ "http://quote.yahoo.com/d/quotes.csv?s=#{symbol}&f=#{tags.join("")}&e=.csv"
193
+ end
194
+
195
+ end
196
+
197
+ end
@@ -0,0 +1,20 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = 'ticker'
3
+ s.version = '0.1'
4
+ s.date = '2010-03-07'
5
+ s.summary = 'Quick and easy stock quote tool'
6
+ s.email = "dan.simpson@gmail.com"
7
+ s.homepage = "http://github.com/dansimpson/ticker"
8
+ s.description = "A simple financial data tool for gathering and streaming stock data"
9
+ s.has_rdoc = true
10
+
11
+ s.authors = ["Dan Simpson"]
12
+
13
+ s.files = [
14
+ "README.markdown",
15
+ "ticker.gemspec",
16
+ "examples/test.rb",
17
+ "lib/ticker.rb",
18
+ "lib/ticker/quote.rb"
19
+ ]
20
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ticker
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Dan Simpson
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-03-07 00:00:00 -08:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: A simple financial data tool for gathering and streaming stock data
21
+ email: dan.simpson@gmail.com
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - README.markdown
30
+ - ticker.gemspec
31
+ - examples/test.rb
32
+ - lib/ticker.rb
33
+ - lib/ticker/quote.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/dansimpson/ticker
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Quick and easy stock quote tool
64
+ test_files: []
65
+