ticker_fetcher 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,17 +7,17 @@ class TickerFetcher
7
7
  Name = 1
8
8
  LastSale = 2
9
9
  MarketCap = 3
10
+ IPOYear = 4
10
11
  Sector = 5
11
12
  Industry = 6
12
- attr_reader :exchanges
13
+ attr_reader :exchanges, :exchange_urls
13
14
 
14
- # Passing 1 or more exchange names will retrieve only those exchanges. Default
15
- # is to retrieve all.
15
+ # Passing an exchange name will retrieve only that exchange. Default is to retrieve all.
16
16
  def initialize
17
17
  @exchanges = {}
18
18
  @exchange_urls = {
19
- 'NASD' => 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download',
20
19
  'AMEX' => 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=AMEX&render=download',
20
+ 'NASD' => 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download',
21
21
  'NYSE' => 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download'
22
22
  }
23
23
  end
@@ -42,15 +42,39 @@ class TickerFetcher
42
42
  private
43
43
 
44
44
  def parse_csv(data)
45
- tickers = []
46
- CSV.parse(data) { |row|
47
- name = row[Name]
48
- ticker = row[Symbol].strip.gsub('"', '')
49
- unless(ticker.nil? || ticker.empty? || ticker == 'Symbol')
50
- ticker = ticker.gsub('/', '.').gsub('^', '-') if ticker =~ /\W/
51
- tickers << [ticker, name]
52
- end
53
- }
54
- tickers
45
+ CSV.parse(data, :headers => true).inject([]) do |tickers, row|
46
+ tickers << [ticker(row), name(row), last_sale(row), market_cap(row), ipo_year(row), sector(row), industry(row)]
47
+ end
48
+ end
49
+
50
+ def name(row)
51
+ row[Name]
52
+ end
53
+
54
+ def ticker(row)
55
+ ticker = row[Symbol].strip.gsub('"', '')
56
+ unless(ticker.nil? || ticker.empty?)
57
+ ticker = ticker =~ /\W/ ? ticker.gsub('/', '.').gsub('^', '-') : ticker
58
+ end
59
+ end
60
+
61
+ def last_sale(row)
62
+ row[LastSale]
63
+ end
64
+
65
+ def market_cap(row)
66
+ row[MarketCap]
67
+ end
68
+
69
+ def ipo_year(row)
70
+ row[IPOYear]
71
+ end
72
+
73
+ def sector(row)
74
+ row[Sector]
75
+ end
76
+
77
+ def industry(row)
78
+ row[Industry]
55
79
  end
56
80
  end
data/test/helper.rb CHANGED
@@ -1,10 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
- require 'flexmock/test_unit'
3
+ require 'fakeweb'
4
4
 
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
  require 'ticker_fetcher'
8
-
9
- class Test::Unit::TestCase
10
- end
@@ -2,38 +2,76 @@ require 'helper'
2
2
 
3
3
  class TestTickerFetcher < Test::Unit::TestCase
4
4
  def setup
5
- @t = TickerFetcher.new
6
- @t_mock = flexmock(@t)
7
- @t_mock.should_receive(:parse_exchange).with('NYSE', 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download').and_return(@t_mock.send(:parse_csv, File.open("test/nyse_test_data.csv").read))
8
- @t_mock.should_receive(:parse_exchange).with('NASD', 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download').and_return(@t_mock.send(:parse_csv, File.open("test/nasd_test_data.csv").read))
9
- @t_mock.should_receive(:parse_exchange).with('AMEX', 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=AMEX&render=download').and_return(@t_mock.send(:parse_csv, File.open("test/amex_test_data.csv").read))
10
- # @t_mock.should_receive(:retrieve).with_no_args.and_return({'NYSE' => File.open("test/nyse_test_data.csv").read, 'NASD' => File.open("test/nasd_test_data.csv").read, 'AMEX' => File.open("test/amex_test_data.csv").read})
11
- # @t_mock.should_receive(:retrieve).with('NYSE').and_return({'NYSE' => File.open("test/nyse_test_data.csv").read})
12
- # @t_mock.should_receive(:retrieve).with('NYSE', 'NASD').and_return({'NYSE' => File.open("test/nyse_test_data.csv").read, 'NASD' => File.open("test/nasd_test_data.csv").read})
13
- end
14
-
15
- def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_the_correct_data
16
- @t_mock.retrieve('NYSE')
17
- assert_not_nil(@t_mock.exchanges['NYSE'])
18
- assert_nil(@t_mock.exchanges['NASD'])
19
- assert_nil(@t_mock.exchanges['AMEX'])
20
- assert_equal('SC', @t_mock.exchanges['NYSE'][0][0])
21
- assert_equal('AC-B', @t_mock.exchanges['NYSE'][1][0])
22
- assert_equal('C.A', @t_mock.exchanges['NYSE'][2][0])
23
- end
24
-
25
- def test_retrieve_with_two_exchanges_returns_a_ticker_fetcher_with_the_correct_data
26
- @t_mock.retrieve('NYSE')
27
- @t_mock.retrieve('NASD')
28
- assert_not_nil(@t_mock.exchanges['NYSE'])
29
- assert_not_nil(@t_mock.exchanges['NASD'])
30
- assert_nil(@t_mock.exchanges['AMEX'])
31
- end
32
-
33
- def test_retrieve_with_all_exchanges_returns_a_ticker_fetcher_with_the_correct_data
34
- @t_mock.retrieve
35
- assert_not_nil(@t_mock.exchanges['NYSE'])
36
- assert_not_nil(@t_mock.exchanges['NASD'])
37
- assert_not_nil(@t_mock.exchanges['AMEX'])
5
+ @tf = TickerFetcher.new
6
+ @tf.exchange_urls.each do |exchange, url|
7
+ FakeWeb.register_uri(
8
+ :get,
9
+ url,
10
+ :body => File.open("test/#{exchange.downcase}_test_data.csv").read)
11
+ end
12
+ end
13
+
14
+ def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_one_exchange
15
+ @tf.retrieve('NYSE')
16
+ assert_not_nil(@tf.exchanges['NYSE'])
17
+ assert_nil(@tf.exchanges['NASD'])
18
+ assert_nil(@tf.exchanges['AMEX'])
19
+ end
20
+
21
+ def test_retrieve_with_two_exchanges_returns_a_ticker_fetcher_with_two_exchanges
22
+ @tf.retrieve('NYSE')
23
+ @tf.retrieve('NASD')
24
+ assert_not_nil(@tf.exchanges['NYSE'])
25
+ assert_not_nil(@tf.exchanges['NASD'])
26
+ assert_nil(@tf.exchanges['AMEX'])
27
+ end
28
+
29
+ def test_retrieve_with_all_exchanges_returns_a_ticker_fetcher_with_all_exchanges
30
+ @tf.retrieve
31
+ @tf.exchanges.keys.each do |exchange|
32
+ assert_not_nil(@tf.exchanges[exchange])
33
+ end
34
+ end
35
+
36
+ def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_correct_tickers
37
+ @tf.retrieve('NYSE')
38
+ assert_equal('SC', @tf.exchanges['NYSE'].first[TickerFetcher::Symbol])
39
+ assert_equal('ZGEN', @tf.exchanges['NYSE'].last[TickerFetcher::Symbol])
40
+ end
41
+
42
+ def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_correct_company_names
43
+ @tf.retrieve('NYSE')
44
+ assert_equal('StupidCompany, Inc.', @tf.exchanges['NYSE'].first[TickerFetcher::Name])
45
+ assert_equal('ZymoGenetics, Inc.', @tf.exchanges['NYSE'].last[TickerFetcher::Name])
46
+ end
47
+
48
+ def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_correct_last_sale_price
49
+ @tf.retrieve('NYSE')
50
+ assert_equal('2.71', @tf.exchanges['NYSE'].first[TickerFetcher::LastSale])
51
+ assert_equal('25', @tf.exchanges['NYSE'].last[TickerFetcher::LastSale])
52
+ end
53
+
54
+ def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_correct_market_cap
55
+ @tf.retrieve('NYSE')
56
+ assert_equal('72858350', @tf.exchanges['NYSE'].first[TickerFetcher::MarketCap])
57
+ assert_equal('85473000', @tf.exchanges['NYSE'].last[TickerFetcher::MarketCap])
58
+ end
59
+
60
+ def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_correct_ipo_year
61
+ @tf.retrieve('NYSE')
62
+ assert_equal('1999', @tf.exchanges['NYSE'].first[TickerFetcher::IPOYear])
63
+ assert_equal('2015', @tf.exchanges['NYSE'].last[TickerFetcher::IPOYear])
64
+ end
65
+
66
+ def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_correct_sector
67
+ @tf.retrieve('NYSE')
68
+ assert_equal('Consumer Services', @tf.exchanges['NYSE'].first[TickerFetcher::Sector])
69
+ assert_equal('Technology', @tf.exchanges['NYSE'].last[TickerFetcher::Sector])
70
+ end
71
+
72
+ def test_retrieve_with_one_exchange_returns_a_ticker_fetcher_with_correct_industry
73
+ @tf.retrieve('NYSE')
74
+ assert_equal('Other Specialty Stores', @tf.exchanges['NYSE'].first[TickerFetcher::Industry])
75
+ assert_equal('Semiconductors', @tf.exchanges['NYSE'].last[TickerFetcher::Industry])
38
76
  end
39
77
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "ticker_fetcher"
5
- s.version = "0.2.0"
5
+ s.version = "0.3.0"
6
6
  s.authors = ["Matt White"]
7
7
  s.email = %q{mattw922@gmail.com}
8
8
  s.homepage = %q{http://github.com/whitethunder/ticker_fetcher}
@@ -12,6 +12,6 @@ Gem::Specification.new do |s|
12
12
  s.test_files = `git ls-files -- {test}/*`.split("\n")
13
13
  s.require_paths = ["lib"]
14
14
 
15
- s.add_development_dependency(%q<flexmock>, [">= 0.8.2"])
15
+ s.add_development_dependency(%q{fakeweb}, [">= 1.3.0"])
16
16
  end
17
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ticker_fetcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-05 00:00:00.000000000Z
12
+ date: 2011-08-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: flexmock
16
- requirement: &81809110 !ruby/object:Gem::Requirement
15
+ name: fakeweb
16
+ requirement: &71675170 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.8.2
21
+ version: 1.3.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *81809110
24
+ version_requirements: *71675170
25
25
  description: Retrieves tickers, and names for all securities listed on the 3 major
26
26
  US exchanges (NYSE, NASDAQ, AMEX).
27
27
  email: mattw922@gmail.com