Sha256: 427378df0dff2e1396772bb0ccd519de1a508565bc6e6d9435dab6780ccd9196

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

require("faraday")

module Aocli
  module AdventOfCode

    Response = Struct.new(:status, :should_retry, :body, keyword_init: true) do
      def should_retry?
        self.should_retry
      end

      def ok?
        status == 200
      end
    end

    class Client
      attr_reader :conn
      def initialize
        @cookie = Aocli::CookieRetriever.call
        @conn = initialise_conn_object
        raise(StandardError, "No cookie is set") unless @cookie
      end

      def get_problem_inputs(year:, day:)
        res = conn.get("/#{year}/day/#{day}/input")
        case res.status
        when 200
          Response.new(
            status: res.status,
            body: res.body,
            should_retry: false,
          )
        else
          Response.new(
            status: res.status,
            body: res.body,
            should_retry: false,
          )
        end
      end

      def get_problem_description(year:, day:)
        res = conn.get("/#{year}/day/#{day}")
        case res.status
        when 200
          Response.new(status: res.status, body: res.body, should_retry: false)
        when 404
          Response.new(
            status: res.status,
            body: res.body,
            should_retry: res.body.include?("Please don't repeatedly request this endpoint before it unlocks!"),
          )
        else
          Response.new(
            status: res.status,
            body: res.body,
            should_retry: false,
          )
        end
      end

      private

      def initialise_conn_object
        Faraday.new(
          url: "https://adventofcode.com/",
          headers: {'Cookie' => @cookie },
        )
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aocli-1.4.5 lib/aocli/advent_of_code/client.rb
aocli-1.4.4 lib/aocli/advent_of_code/client.rb
aocli-1.4.3 lib/aocli/advent_of_code/client.rb
aocli-1.4.2 lib/aocli/advent_of_code/client.rb
aocli-1.4.1 lib/aocli/advent_of_code/client.rb
aocli-1.3.0 lib/aocli/advent_of_code/client.rb