Skip to content

Commit

Permalink
Add integration for Hanami 2
Browse files Browse the repository at this point in the history
  • Loading branch information
svoop committed Mar 5, 2024
1 parent 20bdedc commit a808b45
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Main

Nothing so far
## 0.2.1

* Add square brackets setter for settings
* Describe integration into [Hanami 2](https://hanamirb.org)

## 0.2.0

Expand Down
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ And then install the bundle:
bundle install --trust-policy MediumSecurity
```

See [Integrations](#integrations) below for how to integrate Dry::Credentials into frameworks.

## Usage

Extend any class with `Dry::Credentials` to use the [default settings](#defaults):
Expand Down Expand Up @@ -154,6 +156,69 @@ Setting | Default | Description
`digest` | `"sha256"` | sign digest used if the cipher doesn't support AEAD
`serializer` | `Marshal` | serializer responding to `dump` and `load`

## Integrations

### Hanami 2

To use credentials in a [Hanami 2](https//hanami.org) app, first add this gem to the gemfile of the app and then create a provider `config/providers/credentials.rb`:

```ruby
# frozen_string_literal: true

Hanami.app.register_provider :credentials do
prepare do
require "dry-credentials"

Dry::Credentials::Extension.new.then do |credentials|
credentials[:env] = Hanami.env
credentials.load!
register "credentials", credentials
end
end
end
```

You might want to add a Rake task `lib/tasks/credentials.rake` as well:

```ruby
namespace :credentials do
desc "Edit (or create) the encrypted credentials file"
task :edit, [:env] => [:environment] do |_, args|
Hanami.app.prepare(:credentials)
Hanami.app['credentials'].edit! args[:env]
end
end
```

(As of Hanami 2.1, you have to [explicitly load such tasks in the Rakefile](https://github.com/hanami/hanami/issues/1375) yourself.)

You can now create a new credentials file for the development or any other environment:

```
rake credentials:edit
rake "credentials:edit[test]"
```

The credentials are then available anywhere you inject them:

```
module MyHanamiApp
class ApiKeyPrinter
include Deps[
"credentials"
]
def call
puts credentials.api_key
end
end
end
```

### Rodbot

Dry::Credentials is integrated into [Rodbot](https://github.com/svoop/rodbot) out of the box, see [the README for more](https://github.com/svoop/rodbot/blob/main/README.md#credentials).

## Development

To install the development dependencies and then run the test suite:
Expand Down
8 changes: 8 additions & 0 deletions lib/dry/credentials/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def [](setting)
@settings.send(setting)
end

# Change settings
#
# @param setting [String] name of the setting
# @param value [Object] new value of the setting
def []=(setting, value)
@settings.send(setting, value)
end

end
end
end
7 changes: 7 additions & 0 deletions spec/lib/dry/credentials/extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
end
end

describe :[]= do
it "writes the settings" do
subject.credentials[:serializer] = String
_(subject.credentials[:serializer]).must_equal String
end
end

describe :one_root do
it "reads the credentials" do
_(subject.credentials.one_root).must_equal 'ONE ROOT'
Expand Down

0 comments on commit a808b45

Please sign in to comment.