Class: DownloadSolutions::Api::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/download_solutions/api/github.rb,
lib/download_solutions/api/github/repos.rb

Constant Summary collapse

UnexpectedResponseError =
Class.new(StandardError)
NotFoundResponseError =
Class.new(StandardError)
REPOS =
{
  "ahorner" => {
    repo: "advent-of-code",
    year_directory: "lib/%<year>d",
    both_parts: "%<day>.2d.rb"
  },
  "eregon" => {
    repo: "adventofcode",
    part_1: ->(day:) {
      /\A#{day}(?:a.*)?\.rb\z/
    },
    part_2: ->(day:) {
      /\A#{day}(?:b.*|\D*)\.rb\z/
    }
  },
  "erikw" => {
    repo: "advent-of-code-solutions",
    day_directory: "%<day>.2d",
    part_1: "part1.rb",
    part_2: "part2.rb",
    edits: ->(file_contents) {
      # Remove the first 3 lines (boilerplate).
      file_contents.lines[3..].join
    }
  },
  "gchan" => {
    repo: "advent-of-code-ruby",
    day_directory: "day-%<day>.2d",
    part_1: "day-%<day>.2d-part-1.rb",
    part_2: "day-%<day>.2d-part-2.rb",
    # rarely, e.g. https://github.com/gchan/advent-of-code-ruby/tree/main/2023/day-17
    both_parts: "day-%<day>.2d-part-1-and-2.rb",
    edits: ->(file_contents) {
      # Remove the first 5 lines (boilerplate).
      file_contents.lines[5..].join
    }
  },
  "ZogStriP" => {
    repo: "adventofcode-old",
    both_parts: ->(day:) {
      /\A#{day.to_s.rjust(2, "0")}_.+\.rb\z/
    },
    edits: ->(file_contents) {
      # Remove input at the end of the file.
      file_contents.split("\n__END__").first
    }
  }
}

Instance Method Summary collapse

Constructor Details

#initialize(auth_token:) ⇒ Github

Returns a new instance of Github.

Parameters:

  • auth_token (String)


10
11
12
13
# File 'lib/download_solutions/api/github.rb', line 10

def initialize(auth_token:)
  @user_agent = "AdventOfRubyScript/#{Arb::VERSION} by fpsvogel"
  @auth_token = auth_token
end

Instance Method Details

#get_solutions(author:, year:, input_day: nil, max_day: 25, force: false, existing_solutions: []) ⇒ Array<Hash>?

Returns nil if the year directory doesn’t exist for the author.

Parameters:

  • author (String)
  • year (Integer)
  • day (Integer)
  • max_day (Integer) (defaults to: 25)
  • force (Boolean) (defaults to: false)
  • existing_solutions (Array<Array(Integer, Integer)>) (defaults to: [])

Returns:

  • (Array<Hash>, nil)

    nil if the year directory doesn’t exist for the author.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/download_solutions/api/github.rb', line 22

def get_solutions(author:, year:, input_day: nil, max_day: 25, force: false, existing_solutions: [])
  solutions = {new: {}, skipped: [], not_found: []}

  year_directory = (REPOS[author][:year_directory] || "%<year>d") % {year:}
  year_files = get_year_files!(solutions:, year_directory:, author:, year:) || (return solutions)

  (input_day || 1).upto(input_day || max_day) do |day|
    if REPOS[author][:day_directory]
      day_files = get_day_files!(solutions:, year_directory:, author:, year:, day:, force:, existing_solutions:) || next
    end

    1.upto(2) do |part|
      next if day == 25 && part == 2

      if !force && existing_solutions.include?([day, part])
        solutions[:skipped] << [day, part]
      else
        solution = get_part(author:, year:, day:, part:, year_files:, day_files:)

        if solution.empty?
          solutions[:not_found] << [day, part]
        else
          solutions[:new][[day, part]] = solution
        end
      end
    end
  end

  solutions
end