Skip to content

Commit

Permalink
Add auth via OAuth access tokens (fixes #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Dec 12, 2014
1 parent dba8d90 commit 76a51ac
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,27 @@ Or install it yourself as:

## Usage

To begin making requests, spin up a new Drip client:
Your account ID can be found [here](https://www.getdrip.com/settings/site).

For private integrations, you may use your personal API key (found
[here](https://www.getdrip.com/user/edit)) via the `api_key` setting:

```ruby
client = Drip::Client.new do |c|
c.api_key = "YOUR_API_TOKEN"
c.api_key = "YOUR_API_KEY"
c.account_id = "YOUR_ACCOUNT_ID"
end
```

You can find your API key [here](https://www.getdrip.com/settings/general)
and your account ID [here](https://www.getdrip.com/settings/site).
For public integrations, pass in the user's OAuth token via the `access_token`
setting:

```ruby
client = Drip::Client.new do |c|
c.access_token = "YOUR_ACCESS_TOKEN"
c.account_id = "YOUR_ACCOUNT_ID"
end
```

Since the Drip client is a flat API client, most API actions are available
as methods on the client object. The following methods are currently available:
Expand Down
10 changes: 5 additions & 5 deletions drip-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "rake"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "shoulda-context", "~> 1.0"
spec.add_development_dependency "mocha"
spec.add_development_dependency "mocha", "~> 1.1"

spec.add_runtime_dependency "faraday"
spec.add_runtime_dependency "faraday_middleware"
spec.add_runtime_dependency "json"
spec.add_runtime_dependency "faraday", "~> 0.9"
spec.add_runtime_dependency "faraday_middleware", "~> 0.9"
spec.add_runtime_dependency "json", "~> 1.8"
end
10 changes: 8 additions & 2 deletions lib/drip/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Client
include Tags
include Events

attr_accessor :api_key, :account_id
attr_accessor :access_token, :api_key, :account_id

def initialize
yield(self) if block_given?
Expand Down Expand Up @@ -74,7 +74,13 @@ def connection
f.headers['User-Agent'] = "Drip Ruby v#{Drip::VERSION}"
f.headers['Content-Type'] = content_type
f.headers['Accept'] = "*/*"
f.basic_auth api_key, ""

if access_token
f.headers['Authorization'] = "Bearer #{access_token}"
else
f.basic_auth api_key, ""
end

f.response :json, :content_type => /\bjson$/
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/drip/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Drip
VERSION = "0.0.1"
VERSION = "0.0.2"
end
37 changes: 37 additions & 0 deletions test/drip/client_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/../test_helper.rb'
require "base64"

class Drip::ClientTest < Drip::TestCase
context "initialization" do
Expand All @@ -10,6 +11,14 @@ class Drip::ClientTest < Drip::TestCase
assert_equal "aaaa", client.api_key
end

should "accept access token" do
client = Drip::Client.new do |config|
config.access_token = "aaaa"
end

assert_equal "aaaa", client.access_token
end

should "accept default account id" do
client = Drip::Client.new do |config|
config.account_id = "1234567"
Expand All @@ -30,4 +39,32 @@ class Drip::ClientTest < Drip::TestCase
assert_equal({ @key => [@data] }, @client.generate_resource(@key, @data))
end
end

context "given a personal api key" do
setup do
@key = "aaaa"
@client = Drip::Client.new do |config|
config.api_key = @key
end
end

should "use Basic authentication" do
header = "Basic #{Base64.encode64(@key + ":")}".strip
assert_equal header, @client.connection.headers["Authorization"]
end
end

context "given a OAuth access token" do
setup do
@key = "aaaa"
@client = Drip::Client.new do |config|
config.access_token = @key
end
end

should "use Bearer token authentication" do
header = "Bearer #{@key}"
assert_equal header, @client.connection.headers["Authorization"]
end
end
end

0 comments on commit 76a51ac

Please sign in to comment.