Sha256: 6f232a2a8f8276098e5739923d4a3ea70b73cb8f9b7f5026df03ef250b634056

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

class FtseScraper < StockIndex::BaseScraper

  def scrape
    @wikipedia_hash = parse_wikipedia_page(Nokogiri::HTML(open(StockIndex::INDICES['^FTSE'][:wikipedia_url])))
    (1..6).inject([]) do |array, page|
      doc = Nokogiri::HTML(open(url(page)))
      array += parse_rows(doc.css('table.table_dati tr'))
      array
    end
  end

  private

  def parse_rows(rows)
    rows.inject([]) do |array, tr|
      symbol = symbol(tr)
      market = 'XLON'
      if symbol && market
        component = StockIndex::Component.new(symbol, market, @wikipedia_hash[symbol], :ln)
        array << component.attributes if component.valid?
      end
      array
    end
  end

  def symbol(tr)
    symbol_td = td(tr, 0)
    s = symbol_td ? symbol_td.text : nil
    SymbolParser.new(s).symbol_to_bsym
  end

  def url(page)
    "#{StockIndex::INDICES['^FTSE'][:url]}&page=#{page}"
  end

  def parse_wikipedia_page(wikipedia_doc)
    wikipedia_doc.css('#constituents tr').inject({}) do |hash, tr|
      link_td = tr.css('td')[0]
      symbol_td = tr.css('td')
      if link_td
        hash[symbol_td[1].text] = build_wikipedia_link(link_td.css('a').first.attributes['href'].value)
      end
      hash
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stock_index-0.8.3 lib/stock_index/scrapers/ftse_scraper.rb
stock_index-0.8.2 lib/stock_index/scrapers/ftse_scraper.rb
stock_index-0.8.1 lib/stock_index/scrapers/ftse_scraper.rb
stock_index-0.8.0 lib/stock_index/scrapers/ftse_scraper.rb