Fetch your environment variables. Raise errors or set defaults when a variable does not exist.
Add this line to your application's Gemfile:
gem 'duffel'
And then execute:
$ bundle
Or install it yourself as:
$ gem install duffel
Duffel is a simple gem for reading things out of your environment.
You have something in your environment named SUPER_SECRET_PASSWORD
? No problem. To get it out, just do the following:
Duffel.super_secret_password
Simple, right? But why add a layer of abstraction here? Well, there are a few reasons:
Most people get their environment variables like so: ENV['SUPER_SECRET_PASSWORD']
. This will return nil if the variable is not found. Duffel will return a KeyError, so you are told about missing things.
That's fine too, you can specify a fallback for Duffel in case something isn't found. Example:
Duffel.super_secret_password(fallback: 'another-password')
No problem, we can set our fallback to nil:
Duffel.super_secret_password(fallback: nil)
I actually started writing this because I noticed was using a combination of ENV['SOME_VARIABLE_NAME_HERE']
and Settings.some_method_name
. I wanted to just use the Settings.method_name
pattern everywhere, so I wrote Duffel.
So while Duffel by default gets things out of your environment, you can use it as a general purpose settings drawer.
Let's say you wanted to actually have a constant called Settings but still take advantage of Duffel. You could do something like this:
class Settings < Duffel
def self.some_other_awesome_setting
'awesome setting return value'
end
end
Calling Settings.env_variable_name
will still return you your environment variable, should it exist.
I know.
Because I probably don't know the obviously better way. Feel free to submit a pull request or open an issue and I'll look into it!
- Fork it ( https://github.com/ericroberts/duffel/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request