stock_index 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/cache/XLON.pstore ADDED
Binary file
@@ -4,11 +4,6 @@ class StockIndex
4
4
 
5
5
  require 'csv'
6
6
 
7
- FILES = {
8
- us: 'bsym/US.csv',
9
- jp: 'bsym/JP.csv',
10
- }
11
-
12
7
  EXCEPTIONS = {
13
8
  'BIDU' => {name: 'BAIDU INC', bbgid: 'BBG000QXWHD1'},
14
9
  'LVNTA' => {name: 'LIBERTY VENTURES', bbgid: 'BBG0038K9G41'},
@@ -20,11 +15,15 @@ class StockIndex
20
15
 
21
16
  def find(symbol, pricing_source)
22
17
  return {name: EXCEPTIONS[symbol][:name], bbgid: EXCEPTIONS[symbol][:bbgid]} if EXCEPTIONS.keys.include? symbol
23
- CSV.foreach(FILES[pricing_source]) do |row|
18
+ CSV.foreach(files(pricing_source)) do |row|
24
19
  return {name: row[1], bbgid: row[2]} if row[0] == symbol
25
20
  end
26
21
  end
27
22
 
23
+ def files(pricing_source)
24
+ "bsym/#{pricing_source.upcase}.csv"
25
+ end
26
+
28
27
  end
29
28
 
30
29
  end
@@ -23,7 +23,8 @@ class StockIndex
23
23
  },
24
24
  '^FTSE' => {
25
25
  name: 'FTSE 100',
26
- url: 'http://www.londonstockexchange.com/exchange/prices-and-markets/stocks/indices/summary/summary-indices-constituents.html?index=UKX'
26
+ url: 'http://www.londonstockexchange.com/exchange/prices-and-markets/stocks/indices/summary/summary-indices-constituents.html?index=UKX',
27
+ wikipedia_url: 'http://en.wikipedia.org/wiki/FTSE_100_Index#Current_constituents'
27
28
  }
28
29
  }
29
30
  end
@@ -10,7 +10,7 @@ class SP500Scraper < StockIndex::BaseScraper
10
10
  def symbol(tr)
11
11
  symbol_td = td(tr, 0)
12
12
  s = symbol_td ? symbol_td.css('a').first.text : nil
13
- SymbolParser.new(s).sp500_to_bsym
13
+ SymbolParser.new(s).symbol_to_bsym
14
14
  end
15
15
 
16
16
  def market(tr)
@@ -0,0 +1,47 @@
1
+ class FtseScraper < StockIndex::BaseScraper
2
+
3
+ def scrape
4
+ @wikipedia_hash = parse_wikipedia_page(Nokogiri::HTML(open(StockIndex::INDICES['^FTSE'][:wikipedia_url])))
5
+ (1..6).inject([]) do |array, page|
6
+ doc = Nokogiri::HTML(open(url(page)))
7
+ array += parse_rows(doc.css('table.table_dati tr'))
8
+ array
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def parse_rows(rows)
15
+ rows.inject([]) do |array, tr|
16
+ symbol = symbol(tr)
17
+ market = 'XLON'
18
+ if symbol && market
19
+ component = StockIndex::Component.new(symbol, market, @wikipedia_hash[symbol], :ln)
20
+ array << component.attributes if component.valid?
21
+ end
22
+ array
23
+ end
24
+ end
25
+
26
+ def symbol(tr)
27
+ symbol_td = td(tr, 0)
28
+ s = symbol_td ? symbol_td.text : nil
29
+ SymbolParser.new(s).symbol_to_bsym
30
+ end
31
+
32
+ def url(page)
33
+ "#{StockIndex::INDICES['^FTSE'][:url]}&page=#{page}"
34
+ end
35
+
36
+ def parse_wikipedia_page(wikipedia_doc)
37
+ wikipedia_doc.css('#constituents tr').inject({}) do |hash, tr|
38
+ link_td = tr.css('td')[0]
39
+ symbol_td = tr.css('td')
40
+ if link_td
41
+ hash[symbol_td[1].text] = build_wikipedia_link(link_td.css('a').first.attributes['href'].value)
42
+ end
43
+ hash
44
+ end
45
+ end
46
+
47
+ end
@@ -14,6 +14,8 @@ class StockIndex
14
14
  NasdaqScraper.new.scrape
15
15
  when '^N225'
16
16
  NikkeiScraper.new.scrape
17
+ when '^FTSE'
18
+ FtseScraper.new.scrape
17
19
  else
18
20
  []
19
21
  end
@@ -4,14 +4,11 @@ class SymbolParser
4
4
  @symbol = symbol
5
5
  end
6
6
 
7
- def sp500_to_bsym
8
- case @symbol
9
- # BRK.B => BRK/B (Berkshire Hathaway Inc)
10
- # BF.B => BF/B (Brown-Forman Corp)
11
- when /(\w+)\.B/
12
- "#{$1}/B"
13
- else
14
- @symbol
7
+ def symbol_to_bsym
8
+ if @symbol
9
+ @symbol.sub('.', '/')
10
+ else
11
+ nil
15
12
  end
16
13
  end
17
14
 
@@ -1,3 +1,3 @@
1
1
  class StockIndex
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/stock_index.rb CHANGED
@@ -10,4 +10,5 @@ require 'stock_index/scrapers/base_scraper'
10
10
  require 'stock_index/scrapers/dji_scraper'
11
11
  require 'stock_index/scrapers/SP500_scraper'
12
12
  require 'stock_index/scrapers/nasdaq_scraper'
13
- require 'stock_index/scrapers/nikkei_scraper'
13
+ require 'stock_index/scrapers/nikkei_scraper'
14
+ require 'stock_index/scrapers/ftse_scraper'