Sha256: 36d9e589765fd57c92e0d352f62bf247f8b33de9d49c981d012b2ad96992fc07

Contents?: true

Size: 944 Bytes

Versions: 2

Compression:

Stored size: 944 Bytes

Contents

class NikkeiScraper < StockIndex::BaseScraper

  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

  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

  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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stock_index-0.8.8 lib/stock_index/scrapers/nikkei_scraper.rb
stock_index-0.8.7 lib/stock_index/scrapers/nikkei_scraper.rb