Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Environment variables for client app_id and app_secret #26

Merged
merged 1 commit into from

2 participants

@timcraft

So best practice for storing credentials for external services within an app is to use environment variables, as described nicely here: http://www.12factor.net/config

Using this technique to store GoCardless credentials is currently very verbose. This change adds support for defaulting to environment variables if app_id and app_secret options were not specified explicitly.

Before:

GoCardless::Client.new(app_id: ENV['GOCARDLESS_APP_ID'], app_secret: ENV['GOCARDLESS_APP_SECRET'])

After:

GoCardless::Client.new

Alternative naming could be GOCARDLESS_CLIENT_ID and GOCARDLESS_CLIENT_SECRET, not sure what your preferences are on that.

Cheers,
Tim

@hmarr
Owner

Looks good, thanks!

@hmarr hmarr merged commit 942b2fc into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 2 deletions.
  1. +2 −2 lib/gocardless/client.rb
  2. +7 −0 spec/client_spec.rb
View
4 lib/gocardless/client.rb
@@ -30,8 +30,8 @@ def api_url
def initialize(args = {})
Utils.symbolize_keys! args
- @app_id = args[:app_id]
- @app_secret = args[:app_secret]
+ @app_id = args.fetch(:app_id) { ENV['GOCARDLESS_APP_ID'] }
+ @app_secret = args.fetch(:app_secret) { ENV['GOCARDLESS_APP_SECRET'] }
raise ClientError.new("You must provide an app_id") unless @app_id
raise ClientError.new("You must provide an app_secret") unless @app_secret
View
7 spec/client_spec.rb
@@ -37,6 +37,13 @@
end.should raise_exception(GoCardless::ClientError)
end
+ it "does not raise an error if the credentials are provided as environment variables" do
+ ENV.expects(:[]).with('GOCARDLESS_APP_ID').returns(@app_id)
+ ENV.expects(:[]).with('GOCARDLESS_APP_SECRET').returns(@app_secret)
+
+ GoCardless::Client.new
+ end
+
it "sets a merchant id if it's given" do
client = GoCardless::Client.new({
:app_id => @app_id,
Something went wrong with that request. Please try again.