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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/bsym/LN.csv +1868 -0
- data/cache/XLON.pstore +0 -0
- data/lib/stock_index/bsym_search.rb +5 -6
- data/lib/stock_index/indices.rb +2 -1
- data/lib/stock_index/scrapers/SP500_scraper.rb +1 -1
- data/lib/stock_index/scrapers/ftse_scraper.rb +47 -0
- data/lib/stock_index/stock_index.rb +2 -0
- data/lib/stock_index/symbol_parser.rb +5 -8
- data/lib/stock_index/version.rb +1 -1
- data/lib/stock_index.rb +2 -1
- data/spec/fixtures/html/ftse_1.html +509 -0
- data/spec/fixtures/html/ftse_2.html +509 -0
- data/spec/fixtures/html/ftse_3.html +509 -0
- data/spec/fixtures/html/ftse_4.html +507 -0
- data/spec/fixtures/html/ftse_5.html +509 -0
- data/spec/fixtures/html/ftse_6.html +53 -0
- data/spec/fixtures/html/ftse_wikipedia.html +695 -0
- data/spec/fixtures/yaml/FTSE.yaml +102 -0
- data/spec/helper.rb +32 -0
- data/spec/stock_index/stock_index_spec.rb +9 -4
- data/spec/stock_index/symbol_parser_spec.rb +1 -1
- metadata +21 -2
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(
|
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
|
data/lib/stock_index/indices.rb
CHANGED
@@ -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
|
@@ -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
|
@@ -4,14 +4,11 @@ class SymbolParser
|
|
4
4
|
@symbol = symbol
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
|
data/lib/stock_index/version.rb
CHANGED
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'
|