Sha256: e2000aac0abbcba9b652c6aa99185768a6d78907161f28f692d3b8930731a963

Contents?: true

Size: 871 Bytes

Versions: 2

Compression:

Stored size: 871 Bytes

Contents

require 'open-uri'
require 'nokogiri'

module NpbHeadline
  class Base
    SIZE = 10

    SOURCE = 'http://baseball.yahoo.co.jp/npb/headlines/?team=%d'

    def initialize(team_id)
      @team_id = team_id
    end

    def articles
      _articles = []

      doc.css('#NpbNewsSearch li').each do |li|
        article = {}

        li.css('a').each do |a|
          article[:link]  = a.attribute('href').value
          article[:title] = a.content
        end

        li.css('em').each do |em|
          m = em.content.match /^((?<publisher>.+))\s(?<date>.+)$/
          article[:publisher] = m[:publisher]
          article[:date]      = m[:date]
        end

        _articles << article
      end

      _articles.shift(SIZE)
    end

    private

    def doc
      Nokogiri::HTML(open(url))
    end

    def url
      sprintf(SOURCE, @team_id)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
npb_headline-0.0.2 lib/npb_headline/base.rb
npb_headline-0.0.1 lib/npb_headline/base.rb