Skip to content

Commit

Permalink
Allow day argument to be "0" padded (#34)
Browse files Browse the repository at this point in the history
* normalize day string input

* add tests and update changelog

* fix specs

* Update spec formatting

* Add newline

* Update changelog with tag links

---------

Co-authored-by: Christopher Moore <[email protected]>
Co-authored-by: Jon Pascoe <[email protected]>
  • Loading branch information
3 people authored Dec 10, 2024
1 parent fb6768b commit b89d6a3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- No unreleased changes!

## [0.2.10]
### Changed
- Update API handler to accept zero padded day numbers ([#34](https://github.com/pacso/aoc_rb/pull/34) by [@YozuChris](https://github.com/YozuChris))

## [0.2.9]
### Changed
- Added missing require to `lib/aoc_rb/app.rb` ([#33](https://github.com/pacso/aoc_rb/pull/33) by [@pacso](https://github.com/pacso))
Expand Down Expand Up @@ -74,7 +78,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Initial release.

[Unreleased]: https://github.com/pacso/aoc_rb/compare/v0.2.9...HEAD
[Unreleased]: https://github.com/pacso/aoc_rb/compare/v0.2.10...HEAD
[0.2.10]: https://github.com/pacso/aoc_rb/compare/v0.2.9...v0.2.10
[0.2.9]: https://github.com/pacso/aoc_rb/compare/v0.2.8...v0.2.9
[0.2.8]: https://github.com/pacso/aoc_rb/compare/v0.2.7...v0.2.8
[0.2.7]: https://github.com/pacso/aoc_rb/compare/v0.2.6...v0.2.7
Expand Down
2 changes: 1 addition & 1 deletion lib/aoc_rb/aoc_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def submit_answer(year, day, level, answer)

private
def puzzle_path(year, day)
"/#{year}/day/#{day}"
"/#{year}/day/#{day.to_i}"
end

def input_path(year, day)
Expand Down
2 changes: 1 addition & 1 deletion lib/aoc_rb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AocRb
VERSION = "0.2.9"
VERSION = "0.2.10"
end
69 changes: 69 additions & 0 deletions spec/lib/aoc_rb/api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AocRb::AocApi do
let(:session_token) { "test_session_token" }
let(:year) { 2024 }
let(:level) { 1 }
let(:answer) { "42" }
let(:api) { described_class.new(session_token) }

shared_examples "consistent day path" do |day_input, expected_day|
let(:expected_headers) { { 'Cookie' => "session=#{session_token}" } }

describe "#puzzle_instructions" do
let(:expected_path) { "/2024/day/#{expected_day}" }

before do
allow(api.class).to receive(:get).and_return(double('Response', body: ''))
end

it "sends the request to the correct uri" do
api.puzzle_instructions(year, day_input)
expect(api.class).to have_received(:get).with(expected_path, hash_including(headers: expected_headers))
end
end

describe "#puzzle_input" do
let(:expected_path) { "/2024/day/#{expected_day}/input" }

before do
allow(api.class).to receive(:get).and_return(double('Response', body: ''))
end

it "sends the request to the correct uri" do
api.puzzle_input(year, day_input)
expect(api.class).to have_received(:get).with(expected_path, hash_including(headers: expected_headers))
end
end

describe "#submit_answer" do
let(:expected_path) { "/2024/day/#{expected_day}/answer" }
let(:expected_body) { { level: level.to_s, answer: answer.to_s } }

before do
allow(api.class).to receive(:post).and_return(double('Response', body: ''))
end

it "sends the request to the correct uri" do
api.submit_answer(year, day_input, level, answer)
expect(api.class).to have_received(:post).with(expected_path, hash_including(headers: expected_headers, body: expected_body))
end
end
end

context "when day is an integer" do
include_examples "consistent day path", 1, 1
include_examples "consistent day path", 17, 17
end

context "when day is a two-character string" do
include_examples "consistent day path", "01", 1
include_examples "consistent day path", "22", 22
end

context "when day is a single character string" do
include_examples "consistent day path", "8", 8
end
end
2 changes: 1 addition & 1 deletion spec/lib/aoc_rb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

RSpec.describe AocRb do
it "has the expected version number" do
expect(AocRb::VERSION).to eq "0.2.9"
expect(AocRb::VERSION).to eq "0.2.10"
end
end

0 comments on commit b89d6a3

Please sign in to comment.