Sha256: eb2ca846f4f5805392291bd9cd29f95002a5cfd8d2ec839016786cd56faced36

Contents?: true

Size: 755 Bytes

Versions: 1

Compression:

Stored size: 755 Bytes

Contents

# frozen_string_literal: true

require "open-uri"

module Acclir
  module AtCoder
    # AtCoder contest
    class Contest
      attr_reader :id

      def initialize(id)
        @id = id
      end

      def tasks
        @tasks ||= extract_task_ids.map { |task_id| Task.new(id, task_id) }
      end

      private

      def url
        @url ||= ATCODER_TASKS_URL_PROC.call(id)
      end

      def document
        @document ||= Nokogiri::HTML(URI.parse(url).open)
      end

      def extract_task_ids
        hrefs = document.xpath("//a[@href]").map do |element|
          element.attribute("href").content
        end

        hrefs.uniq.map { |href| href.match(ATCODER_TASK_PATH_REGEX_PROC.call(id))&.[](:task) }.compact
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acclir-0.1.0 lib/acclir/at_coder/contest.rb