class NikkeiScraper

Public Instance Methods

scrape() click to toggle source
# File lib/stock_index/scrapers/nikkei_scraper.rb, line 3
def scrape
  doc = Nokogiri::HTML(open(StockIndex::INDICES['^N225'][:url]))
  @wikipedia_hash = parse_wikipedia_page(Nokogiri::HTML(open(StockIndex::INDICES['^N225'][:wikipedia_url])))
  parse_rows doc.css('div.row.component-list')
end

Private Instance Methods

parse_rows(rows) click to toggle source
# File lib/stock_index/scrapers/nikkei_scraper.rb, line 11
def parse_rows(rows)
  rows.inject([]) do |array, tr|
    symbol = tr.css('div').text
    market = 'XJPX'
    if symbol && market
      component = StockIndex::Component.new(symbol, market, @wikipedia_hash[symbol], :jp)
      array << component.attributes
    end
    array
  end
end
parse_wikipedia_page(wikipedia_doc) click to toggle source
# File lib/stock_index/scrapers/nikkei_scraper.rb, line 23
def parse_wikipedia_page(wikipedia_doc)
  wikipedia_doc.css('#constituents tr').inject({}) do |hash, tr|
    md = tr.text.match(/\n(\d{4})\n/)
    if md
      symbol = md[1]
      link = build_wikipedia_link(tr.css('a').first.attributes['href'].value)
      hash[symbol] = link
    end
    hash
  end
end