Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading a datetime value #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 6.1.0 - 2024-06-27

- Add support for loading a datetime value.

# 6.0.0 - 2024-01-09

- Drop support for Ruby 2.6 and 2.7.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source "https://rubygems.org"
gemspec

group :development do
gem "gc_ruboconfig", "~> 4.4"
gem "gc_ruboconfig", "~> 5.0"
gem 'pry'
gem "rspec", "~> 3.12"
gem "rspec-github", "~> 2.4.0"
Expand Down
18 changes: 14 additions & 4 deletions lib/prius/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def initialize(env)
def load(name, env_var: nil, type: :string, required: true)
env_var = name.to_s.upcase if env_var.nil?
@registry[name] = case type
when :string then load_string(env_var, required)
when :int then load_int(env_var, required)
when :bool then load_bool(env_var, required)
when :date then load_date(env_var, required)
when :string then load_string(env_var, required)
when :int then load_int(env_var, required)
when :bool then load_bool(env_var, required)
when :date then load_date(env_var, required)
when :datetime then load_datetime(env_var, required)
else raise ArgumentError, "Invalid type #{type}"
end
end
Expand Down Expand Up @@ -73,5 +74,14 @@ def load_date(name, required)
rescue ArgumentError
raise TypeMismatchError, "'#{name}' value '#{value}' is not a date"
end

def load_datetime(name, required)
value = load_string(name, required)
return nil if value.nil?

DateTime.parse(value)
Copy link
Contributor

@ameykusurkar ameykusurkar Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the documentation:

This method does not function as a validator. If the input string does not match valid formats strictly, you may get a cryptic result. Should consider to use DateTime.strptime instead of this method as possible.

Should we use a standardised format (eg. DateTime.rfc3339(...)) to avoid ambiguity?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally, we can call the arguument :datetime_rfc3339 if we want.

Copy link

@benk-gc benk-gc Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Petition to use ActiveSupport::TimeWithZone. It has proper timezone support, and we can safely (de)serialize values with the iso8601 method which is our general standard elsewhere (e.g. in Pub/Sub messages).

Copy link

@kevinrobayna kevinrobayna Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came here to talk about the above.

Date.parse or DateTime.parse are Russian roulettes so we def need to use a specific format and agree on it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ActiveSupport::TimeWithZone

that would mean adding rails as a dependency, not sure we should do that.

Copy link

@benk-gc benk-gc Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinrobayna I think we can just add active_support as a dependency and require the minimal active_support requirements and active_support/time_with_zone like so:

require "active_support"
require "active_support/time_with_zone"

See https://guides.rubyonrails.org/active_support_core_extensions.html#stand-alone-active-support

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is "rails" 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I just found this:

DateTime class is considered deprecated. Use Time class.

I'm in two minds about adding a big dependency to a very minimal gem as well. Granted that TimeWithZone comes with a bunch of goodies, but looks like Time does handle the basics of timezones correctly:

irb(main):001> require "time"
=> true
irb(main):002> t = Time.iso8601("2023-10-07T21:15:30+11:00")
=> 2023-10-07 21:15:30 +1100
irb(main):003> t.utc
=> 2023-10-07 10:15:30 UTC

Plus if this gem is used in the context of a Rails app, then we can just do t.in_time_zone to get a full-fledged ActiveSupport::TimeWithZone, so probably not too bad?

rescue ArgumentError
raise TypeMismatchError, "'#{name}' value '#{value}' is not a datetime"
end
end
end
2 changes: 1 addition & 1 deletion lib/prius/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Prius
VERSION = "6.0.0"
VERSION = "6.1.0"
end
30 changes: 30 additions & 0 deletions spec/prius/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"ALIVE" => "Yes",
"BORN" => "2022-09-02",
"INVALID_DATE" => "2022-02-99",
"HELIUM_RELEASE_DATETIME" => "2024-01-31 00:00 UTC",
"INVALID_RELEASE_DATETIME" => "2024-01-31 00:99 UTC",
}
end
let(:registry) { described_class.new(env) }
Expand Down Expand Up @@ -115,6 +117,34 @@
end
end
end

context "when specifying :datetime as the type" do
context "given a datetime value" do
it "doesn't blow up" do
expect { registry.load(:helium_release_datetime, type: :datetime) }.to_not raise_error
end

it "stores a datetime" do
registry.load(:helium_release_datetime, type: :datetime)
expect(registry.get(:helium_release_datetime)).to be_a(DateTime)
expect(registry.get(:helium_release_datetime)).to eq(DateTime.parse(env["HELIUM_RELEASE_DATETIME"]))
end
end

context "given an invalid date value" do
it "blows up" do
expect { registry.load(:invalid_release_datetime, type: :datetime) }.
to raise_error(Prius::TypeMismatchError)
end
end

context "given a non-date value" do
it "blows up" do
expect { registry.load(:name, type: :datetime) }.
to raise_error(Prius::TypeMismatchError)
end
end
end
end

describe "#get" do
Expand Down