Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #8 from yyyc514/master

Implement global settings for API credentials.
  • Loading branch information...
commit 081a9c5972a0ff17e6fb85723102a90afa4e5694 2 parents 8d0e1df + 35db64e
@freerobby authored
View
12 README.md
@@ -37,6 +37,18 @@ Contributing
Big thanks to [James Brennan][1] for refactoring old code and writing the Bounces, InvalidEmails and SpamReports modules.
+Setting your API credentials globally
+-------------------------------------
+
+Setting your API user and key once globally for all API access:
+
+ SendgridToolkit.api_user = "bob"
+ SendgridToolkit.api_key = "x123y"
+
+If you do this when you create individual API objects you will not need to pass the `api_user` or `api_key`
+
+ bounces = SendgridToolkit::Bounces.new
+
Bounces Module
--------------
The bounces module provides access to all of your bounces.
View
9 lib/sendgrid_toolkit.rb
@@ -12,4 +12,13 @@
module SendgridToolkit
BASE_URI = "sendgrid.com/api"
+
+ class << self
+ def api_user=(v); @api_user = v; end
+ def api_user; @api_user; end
+
+ def api_key=(v); @api_key = v; end
+ def api_key; @api_key; end
+ end
+
end
View
4 lib/sendgrid_toolkit/abstract_sendgrid_client.rb
@@ -2,8 +2,8 @@ module SendgridToolkit
class AbstractSendgridClient
def initialize(api_user = nil, api_key = nil)
- @api_user = (api_user.nil?) ? ENV['SMTP_USERNAME'] : api_user
- @api_key = (api_key.nil?) ? ENV['SMTP_PASSWORD'] : api_key
+ @api_user = api_user || SendgridToolkit.api_user || ENV['SMTP_USERNAME']
+ @api_key = api_key || SendgridToolkit.api_key || ENV['SMTP_PASSWORD']
raise SendgridToolkit::NoAPIUserSpecified if @api_user.nil? || @api_user.length == 0
raise SendgridToolkit::NoAPIKeySpecified if @api_key.nil? || @api_key.length == 0
View
15 spec/lib/sendgrid_toolkit/abstract_sendgrid_client_spec.rb
@@ -33,6 +33,10 @@
end
describe "#initialize" do
+ after(:each) do
+ SendgridToolkit.api_user = nil
+ SendgridToolkit.api_key = nil
+ end
it "stores api credentials when passed in" do
ENV['SMTP_USERNAME'] = "env_username"
ENV['SMTP_PASSWORD'] = "env_apikey"
@@ -41,6 +45,17 @@
@obj.instance_variable_get("@api_user").should == "username"
@obj.instance_variable_get("@api_key").should == "apikey"
end
+ it "uses module level user and key if they are set" do
+ SendgridToolkit.api_user = "username"
+ SendgridToolkit.api_key = "apikey"
+
+ SendgridToolkit.api_key.should == "apikey"
+ SendgridToolkit.api_user.should == "username"
+
+ @obj = SendgridToolkit::AbstractSendgridClient.new
+ @obj.instance_variable_get("@api_user").should == "username"
+ @obj.instance_variable_get("@api_key").should == "apikey"
+ end
it "resorts to environment variables when no credentials specified" do
ENV['SMTP_USERNAME'] = "env_username"
ENV['SMTP_PASSWORD'] = "env_apikey"
Please sign in to comment.
Something went wrong with that request. Please try again.