Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Last active April 12, 2022 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenharman/7ccdbbcfae4d56fd67814fa8520c80be to your computer and use it in GitHub Desktop.
Save stevenharman/7ccdbbcfae4d56fd67814fa8520c80be to your computer and use it in GitHub Desktop.
An example of an app-specific configuration wrapper around ENV and Rails.env. This consolidates access to config vars, while providing a seam for adding conditional logic, data munging, type coercion, etc...
module Example
class Config
def initialize(host_env: ENV, rails_env: Rails.env)
@host_env = host_env.to_hash
@rails_env = rails_env
end
def admin_qa_tooling_enabled?
coerce_boolean(host_env.fetch("ADMIN_QA_TOOLING", "false"))
end
def app_auth_url(token)
"#{app_url_scheme}://example.com/in-app-auth?authcode=#{token}"
end
def aws_assets_bucket
host_env.fetch("AWS_ASSETS_BUCKET")
end
def aws_access_key_id
host_env.fetch("AWS_ACCESS_KEY_ID")
end
def aws_secret_access_key
host_env.fetch("AWS_SECRET_ACCESS_KEY")
end
def aws_region
host_env.fetch("AWS_REGION")
end
def aws_sns_service_provider_application_arn
host_env.fetch("AWS_SNS_SERVICE_PROVIDER_APPLICATION_ARN")
end
def google_maps_geocoding_api_key
if rails_env.production?
host_env.fetch("GOOGLE_MAPS_GEOCODING_API_KEY")
else
# A nil key results in using the free tier with lower quotas
host_env.fetch("GOOGLE_MAPS_GEOCODING_API_KEY", nil)
end
end
def intercom_access_token
host_env.fetch("INTERCOM_ACCESS_TOKEN")
end
def intercom_app_id
host_env.fetch("INTERCOM_APP_ID")
end
def intercom_enabled?
coerce_boolean(host_env.fetch("INTERCOM_ENABLED", "false"))
end
def intercom_secure_mode_secret_key
host_env.fetch("INTERCOM_SECURE_MODE_SECRET_KEY")
end
def oldest_supported_version
Integer(host_env.fetch("OLDEST_SUPPORTED_VERSION"))
end
def qualified_technician_max_distance_in_miles
Float(host_env.fetch("QUALIFIED_TECHNICIAN_MAX_DISTANCE_IN_MILES", 100))
end
def service_provider_onboarding_enabled?
coerce_boolean(host_env.fetch("SERVICE_PROVIDER_ONBOARDING_ENABLED", "false"))
end
def service_provider_stripe_enabled?
coerce_boolean(host_env.fetch("SERVICE_PROVIDER_STRIPE_ENABLED", "false"))
end
def subscription_stripe_enabled?
coerce_boolean(host_env.fetch("SUBSCRIPTION_STRIPE_ENABLED", "false"))
end
def stripe_connect_client_id
host_env.fetch("STRIPE_CONNECT_CLIENT_ID")
end
def stripe_connect_oauth_redirect_uri
"#{current_uri_scheme}://#{web_domain}#{STRIPE_CONNECT_CALLBACK_PATH}"
end
def stripe_publishable_key
host_env.fetch("STRIPE_PUBLISHABLE_KEY")
end
def stripe_secret_key
host_env.fetch("STRIPE_SECRET_KEY")
end
def web_domain
host = if rails_env.production?
host_env.fetch("WEB_DOMAIN")
else
host_env.fetch("WEB_DOMAIN", "localhost:5000")
end
override_for_heroku_review_app(host)
end
def web_domain_url
"#{current_uri_scheme}://#{web_domain}"
end
def web_push_vapid_contact
host_env.fetch("VAPID_CONTACT", "mailto:developers+webpush@example.com")
end
def web_push_vapid_private_key
host_env.fetch("VAPID_PRIVATE_KEY")
end
def web_push_vapid_public_key
host_env.fetch("VAPID_PUBLIC_KEY")
end
def zip_tax_api_key
host_env.fetch("ZIP_TAX_API_KEY")
end
private
HEROKU_REVIEW_APP_INDICATOR = "this-is-a-heroku-review-app"
STRIPE_CONNECT_CALLBACK_PATH = "/oauth/stripe/connect"
TRUE_VALUES = %w[true t yes y 1].freeze
URI_SCHEME = {true => "https", false => "http"}.freeze
private_constant :HEROKU_REVIEW_APP_INDICATOR, :STRIPE_CONNECT_CALLBACK_PATH, :TRUE_VALUES, :URI_SCHEME
attr_reader :host_env, :rails_env
def app_url_scheme
host_env.fetch("URL_SCHEME")
end
def coerce_boolean(value)
TRUE_VALUES.include?(String(value).downcase)
end
def current_uri_scheme
URI_SCHEME.fetch(rails_env.production?)
end
def heroku_review_app?(host)
host == HEROKU_REVIEW_APP_INDICATOR
end
# A workaround for Heroku Review Apps
# (https://devcenter.heroku.com/articles/github-integration-review-apps).
# Review Apps don't currently have a way to populate one host_env var
# (e.g., WEB_DOMAIN) from another (e.g., HEROKU_APP_NAME).
def override_for_heroku_review_app(host)
if heroku_review_app?(host)
return "#{host_env.fetch("HEROKU_APP_NAME")}.herokuapp.com"
end
host
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment