class ScrapCbf::Document

This class is responsible for:

Constants

CHAMPIONSHIP_YEARS
SAMPLE_PATH
SERIES
SERIES_PATH
URL

Attributes

load_from_sample[R]
parsed_document[R]
sample_path[R]
serie[R]
year[R]

Public Class Methods

new(year, serie, opts) click to toggle source

@param [Integer] year the Championship year @param [Symbol] serie the Championship serie. see SERIES. @option opts [Boolean] load_from_sample yes or no to load specific

HTML file

@option opts [Symbol] sample_path path to the sample otherwise default

@return [Document] new instance

# File lib/scrap_cbf/document.rb, line 42
def initialize(year, serie, opts)
  @year = year
  @serie = serie
  @load_from_sample = opts.fetch(:load_from_sample) { false }
  @sample_path = opts[:sample_path]

  @parsed_document =
    parse_document(year, serie, @load_from_sample, @sample_path)
end
parse_document(year, serie, opts) click to toggle source
# File lib/scrap_cbf/document.rb, line 24
def parse_document(year, serie, opts)
  new(year, serie, opts).parsed_document
end

Private Instance Methods

build_url(year, serie) click to toggle source
# File lib/scrap_cbf/document.rb, line 73
def build_url(year, serie)
  "#{URL}/#{SERIES_PATH[serie]}/#{year}"
end
parse_document(year, serie, load_from_sample, sample_path) click to toggle source

@param [Integer] year the Championship year @param [Symbol] serie the Championship serie. see SERIES. @option opts [Boolean] load_from_sample yes or no to load specific

HTML file

@option opts [Symbol] sample_path path to the sample otherwise default

@return [Nokogiri::HTML::Document] new instance

# File lib/scrap_cbf/document.rb, line 61
def parse_document(year, serie, load_from_sample, sample_path)
  url = if load_from_sample
          sample_path || SAMPLE_PATH
        else
          raise_year_error if year_out_of_range?(year)
          raise_serie_error if serie_out_of_range?(serie)

          build_url(year, serie)
        end
  Nokogiri::HTML(URI.open(url))
end
raise_serie_error() click to toggle source
# File lib/scrap_cbf/document.rb, line 89
def raise_serie_error
  raise OutOfRangeArgumentError.new(:serie, SERIES)
end
raise_year_error() click to toggle source
# File lib/scrap_cbf/document.rb, line 85
def raise_year_error
  raise OutOfRangeArgumentError.new(:year, CHAMPIONSHIP_YEARS)
end
serie_out_of_range?(serie) click to toggle source
# File lib/scrap_cbf/document.rb, line 81
def serie_out_of_range?(serie)
  !SERIES.include?(serie)
end
year_out_of_range?(year) click to toggle source
# File lib/scrap_cbf/document.rb, line 77
def year_out_of_range?(year)
  !CHAMPIONSHIP_YEARS.include?(year)
end