Skip to content
Merged

v2 #16

Show file tree
Hide file tree
Changes from 9 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
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
ruby-version: 3.3
- name: Bundle install
run: bundle install
Comment thread
enjaku4 marked this conversation as resolved.
- name: Run RuboCop
Expand All @@ -33,7 +33,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
ruby-version: 3.3
- name: Install grepfruit
run: gem install grepfruit
- name: Run grepfruit
Expand All @@ -48,12 +48,10 @@ jobs:
strategy:
matrix:
ruby:
- "3.2"
- "3.3"
- "3.4"
- "4.0"
rails:
- "~> 7.1.0"
- "~> 7.2.0"
- "~> 8.0.0"
- "~> 8.1.0"
Expand Down
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ plugins:
- rubocop-thread_safety

AllCops:
TargetRailsVersion: 7.1
TargetRubyVersion: 3.2
TargetRailsVersion: 7.2
TargetRubyVersion: 3.3
NewCops: enable

Layout/IndentationConsistency:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v2.0.0

- Dropped support for Ruby 3.2
- Dropped support for Rails 7.1
- Improved error messages and input validation
Comment thread
enjaku4 marked this conversation as resolved.

## v1.1.7

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

gemspec

rails_version = ENV.fetch("RAILS_VERSION", "~> 7.1")
rails_version = ENV.fetch("RAILS_VERSION", "~> 7.2")

gem "byebug"
gem "rails", rails_version
Expand Down
4 changes: 2 additions & 2 deletions kreds.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ Gem::Specification.new do |spec|
spec.summary = "The missing shorthand for Rails credentials"
spec.description = "Simpler and safer Rails credentials access with blank value detection and clear error messages"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.2", "< 4.1"
spec.required_ruby_version = ">= 3.3", "< 4.1"

spec.files = [
"kreds.gemspec", "README.md", "CHANGELOG.md", "LICENSE.txt"
] + Dir.glob("lib/**/*")

spec.require_paths = ["lib"]

spec.add_dependency "rails", ">= 7.1", "< 8.2"
spec.add_dependency "rails", ">= 7.2", "< 8.2"
end
8 changes: 4 additions & 4 deletions lib/kreds/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def fetch!(*keys, var: nil, &)
end

def env_fetch!(*keys, var: nil, &)
fetch!(Rails.env, *keys, var:, &)
fetch!(Rails.env, *Kreds::Inputs.process(keys, as: :symbol_array), var:, &)
end

def var!(var, &)
Expand All @@ -33,15 +33,15 @@ def fetch_key(hash, key, path, keys)
value = hash.fetch(key)

raise Kreds::BlankCredentialsError, "Blank value in credentials: #{path_to_s(path)}" if value.blank?
raise Kreds::UnknownCredentialsError, "Credentials key not found: #{path_to_s(path)}[:#{keys[path.size]}]" unless value.is_a?(Hash) || keys == path
raise Kreds::UnknownCredentialsError, "Credentials key not found: #{path_to_s(path.append(keys[path.size]))}" unless value.is_a?(Hash) || keys == path

value
rescue KeyError
raise Kreds::UnknownCredentialsError, "Credentials key not found: #{path_to_s(path)}"
end

def fallback_to_var(error, var, &)
return raise_or_yield(error, &) if var.blank?
return raise_or_yield(error, &) if var.nil?

var!(var, &)
rescue Kreds::BlankEnvironmentVariableError, Kreds::UnknownEnvironmentVariableError => e
Expand All @@ -53,7 +53,7 @@ def raise_or_yield(error, &)
end

def path_to_s(path)
"[:#{path.join("][:")}]"
path.map(&:inspect).join(" => ")
end
end
end
2 changes: 1 addition & 1 deletion lib/kreds/show.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Kreds
module Show
def show
Rails.application.credentials.as_json.deep_symbolize_keys
Rails.application.credentials.config.to_h
Comment thread
enjaku4 marked this conversation as resolved.
end
end
end
2 changes: 1 addition & 1 deletion lib/kreds/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Kreds
VERSION = "1.1.7".freeze
VERSION = "2.0.0".freeze
end
17 changes: 9 additions & 8 deletions spec/kreds_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@

it "raises error for missing end key" do
expect { described_class.fetch!(:foo, :bar, :bad) }
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: [:foo][:bar][:bad]")
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: :foo => :bar => :bad")
end

it "raises error for missing middle key" do
expect { described_class.fetch!(:foo, :bad, :baz) }
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: [:foo][:bad]")
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: :foo => :bad")
end

it "raises error for blank value" do
expect { described_class.fetch!(:bad) }
.to raise_error(Kreds::BlankCredentialsError, "Blank value in credentials: [:bad]")
.to raise_error(Kreds::BlankCredentialsError, "Blank value in credentials: :bad")
end
end

Expand Down Expand Up @@ -116,7 +116,7 @@
context "when handling edge cases" do
it "raises error when trying to access non-hash as hash" do
expect { described_class.fetch!(:foo, :bar, :baz, :extra) }
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: [:foo][:bar][:baz][:extra]")
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: :foo => :bar => :baz => :extra")
end

it "works with single key" do
Expand All @@ -128,7 +128,7 @@
describe ".env_fetch!" do
it "raises error for invalid key types" do
expect { described_class.env_fetch!(:foo, 42) }
.to raise_error(Kreds::InvalidArgumentError, "Expected an array of symbols or strings, got `[\"test\", :foo, 42]`")
.to raise_error(Kreds::InvalidArgumentError, "Expected an array of symbols or strings, got `[:foo, 42]`")
end

it "raises error for invalid var type when fallback is needed" do
Expand All @@ -137,8 +137,9 @@
end

context "without var" do
it "returns entire test environment hash when no keys provided" do
expect(described_class.env_fetch!).to eq({ foo: [1, 2, 3] })
it "raises error for empty keys" do
expect { described_class.env_fetch! }
.to raise_error(Kreds::InvalidArgumentError, "Expected an array of symbols or strings, got `[]`")
Comment thread
enjaku4 marked this conversation as resolved.
end

it "returns specific value from test environment" do
Expand All @@ -147,7 +148,7 @@

it "raises error for missing key in test environment" do
expect { described_class.env_fetch!(:foo, :bar, :bad) }
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: [:test][:foo][:bar]")
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: :test => :foo => :bar")
end
end

Expand Down
Loading