💎 Don't worry about the environment. (Gem)
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
certs
lib
.gitignore
Gemfile
Gemfile.lock
README.md
Rakefile
heroku-env.gemspec

README.md

heroku-env Gem Version RSS

Don't worry about the environment.

This simple gem makes it easier for you to use different Heroku addons in a plug-and-play fashion. Normally, if you decide to add a Redis provider, you have to customize your code to use that addon's environment variables. The problem with this, however, is that if you want to switch provider, you have to update your code.

What I usually ended up doing was:

ENV["REDIS_URL"] ||= ENV["REDISCLOUD_URL"] || ENV["REDISTOGO_URL"]
$redis = Redis.new

Doing this in every project started to become a little annoying, so I decided to make a gem.

The gem only supports addons with a free plan. The order that the addons are selected depends on how stable and feature-rich they are. So if you are deciding what provider you want to use, a good choice is probably the first one in the lists below.

The gem uses ||= in all the assignments, so you can easily override any variable by manually setting it.

Installation

Add to your Gemfile:

gem "heroku-env", "~> 0.1.0"

If you use dotenv-rails, then you should put this gem lower than it in the Gemfile.

If you use Rails, then you should not need to do anything more, the gem has a Railtie hook which will do the initialization.

If not, then you might have to run this manually:

require "heroku-env"
HerokuEnv.run

This gem is cryptographically signed, you can verify the installation with:

gem cert --add <(curl -Ls https://raw.githubusercontent.com/stefansundin/heroku-env/master/certs/stefansundin.pem)
gem install heroku-env -P HighSecurity

Redis

Nowadays Heroku is offering their own redis solution called Heroku Redis which I recommend unless you have specific requirements. It populates REDIS_URL so this gem is not required.

Supports Redis Cloud and Redis To Go.

$redis = Redis.new

Memcache

Supports MemCachier and Memcached Cloud.

$dc = Dalli::Client.new

Mongo

Supports MongoLab and MongoSoup.

$mongo = Mongo::Client.new ENV["MONGO_URL"]

MySQL

Supports ClearDB and JawsDB.

config = URI.parse ENV["MYSQL_URL"]
$mysql = Mysql2::Client.new(
  host:     config.hostname,
  port:     config.port,
  username: config.user,
  password: config.password,
  database: config.path.sub(%r{^/},""),
  reconnect: true
)

Neo4j

Supports Graph Story and GrapheneDB.

$neo4j = Neo4j::Session.open :server_db, ENV["NEO4J_URL"]

Elasticsearch

Supports Bonsai and SearchBox.

$elasticsearch = Elasticsearch::Client.new

SMTP

Supports Mandrill, SendGrid, Mailgun, Postmark and SparkPost.

Mail.defaults do
  delivery_method :smtp,
    address:              ENV["SMTP_HOST"],
    port:                 ENV["SMTP_PORT"],
    user_name:            ENV["SMTP_USERNAME"],
    password:             ENV["SMTP_PASSWORD"],
    authentication:       "plain",
    enable_starttls_auto: true
end

Then send an email with:

Mail.deliver do
     from "Sender <#{ENV["MAIL_FROM"]}>"
       to "receiver@example.com"
  subject "Test email"
     body "Just testing heroku-env"
end

If you use Postmark, you have to manually set MAIL_FROM.

ActionMailer

If you use Rails, you might want to use this:

ActionMailer::Base.smtp_settings = {
  address:        ENV["SMTP_HOST"],
  port:           ENV["SMTP_PORT"],
  user_name:      ENV["SMTP_USERNAME"],
  password:       ENV["SMTP_PASSWORD"],
  authentication: :plain,
}

Airbrake

Supports Airbrake, Raygun and Rollbar.

Airbrake.configure do |config|
  config.host = ENV["AIRBRAKE_HOST"]
  config.api_key = ENV["AIRBRAKE_KEY"]
  config.secure = true
end

Another Airbrake-compatible provider is AppEnlight, it is free but they don't provide a Heroku addon. You can manually set APPENLIGHT_APIKEY to use it.

Misc notes

  • Postmark sends you some unwanted emails if you add the addon.