Use fetch method to access ENV variables in Ruby
It's better for at least a couple of reasons, eloquently stated in this blog post by Michal Orman
...we can set default values or handle - providing a block - gracefully missing keys. Also using
fetch
with unknown key will raiseKeyError
that will tell us which exactly key is missing. That is in fact the behavior we are expecting from the app. Without required settings is just not working and complaining about missing setting and not about some random nil references.
Got that? Instead of this:
AWS.config(
access_key_id: ENV['S3_ACCESS_KEY'],
secret_access_key: ENV['S3_SECRET_KEY'],
region: ENV['S3_REGION']
)
Do this:
AWS.config(
access_key_id: ENV.fetch('S3_ACCESS_KEY'),
secret_access_key: ENV.fetch('S3_SECRET_KEY'),
region: ENV.fetch('S3_REGION')
)