Skip to content

Commit 76a51ac

Browse files
committed
Add auth via OAuth access tokens (fixes #12)
1 parent dba8d90 commit 76a51ac

5 files changed

Lines changed: 65 additions & 12 deletions

File tree

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,27 @@ Or install it yourself as:
1818

1919
## Usage
2020

21-
To begin making requests, spin up a new Drip client:
21+
Your account ID can be found [here](https://www.getdrip.com/settings/site).
22+
23+
For private integrations, you may use your personal API key (found
24+
[here](https://www.getdrip.com/user/edit)) via the `api_key` setting:
2225

2326
```ruby
2427
client = Drip::Client.new do |c|
25-
c.api_key = "YOUR_API_TOKEN"
28+
c.api_key = "YOUR_API_KEY"
2629
c.account_id = "YOUR_ACCOUNT_ID"
2730
end
2831
```
2932

30-
You can find your API key [here](https://www.getdrip.com/settings/general)
31-
and your account ID [here](https://www.getdrip.com/settings/site).
33+
For public integrations, pass in the user's OAuth token via the `access_token`
34+
setting:
35+
36+
```ruby
37+
client = Drip::Client.new do |c|
38+
c.access_token = "YOUR_ACCESS_TOKEN"
39+
c.account_id = "YOUR_ACCOUNT_ID"
40+
end
41+
```
3242

3343
Since the Drip client is a flat API client, most API actions are available
3444
as methods on the client object. The following methods are currently available:

drip-ruby.gemspec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Gem::Specification.new do |spec|
1919
spec.require_paths = ["lib"]
2020

2121
spec.add_development_dependency "bundler", "~> 1.6"
22-
spec.add_development_dependency "rake"
22+
spec.add_development_dependency "rake", "~> 10.0"
2323
spec.add_development_dependency "shoulda-context", "~> 1.0"
24-
spec.add_development_dependency "mocha"
24+
spec.add_development_dependency "mocha", "~> 1.1"
2525

26-
spec.add_runtime_dependency "faraday"
27-
spec.add_runtime_dependency "faraday_middleware"
28-
spec.add_runtime_dependency "json"
26+
spec.add_runtime_dependency "faraday", "~> 0.9"
27+
spec.add_runtime_dependency "faraday_middleware", "~> 0.9"
28+
spec.add_runtime_dependency "json", "~> 1.8"
2929
end

lib/drip/client.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Client
1212
include Tags
1313
include Events
1414

15-
attr_accessor :api_key, :account_id
15+
attr_accessor :access_token, :api_key, :account_id
1616

1717
def initialize
1818
yield(self) if block_given?
@@ -74,7 +74,13 @@ def connection
7474
f.headers['User-Agent'] = "Drip Ruby v#{Drip::VERSION}"
7575
f.headers['Content-Type'] = content_type
7676
f.headers['Accept'] = "*/*"
77-
f.basic_auth api_key, ""
77+
78+
if access_token
79+
f.headers['Authorization'] = "Bearer #{access_token}"
80+
else
81+
f.basic_auth api_key, ""
82+
end
83+
7884
f.response :json, :content_type => /\bjson$/
7985
end
8086
end

lib/drip/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Drip
2-
VERSION = "0.0.1"
2+
VERSION = "0.0.2"
33
end

test/drip/client_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require File.dirname(__FILE__) + '/../test_helper.rb'
2+
require "base64"
23

34
class Drip::ClientTest < Drip::TestCase
45
context "initialization" do
@@ -10,6 +11,14 @@ class Drip::ClientTest < Drip::TestCase
1011
assert_equal "aaaa", client.api_key
1112
end
1213

14+
should "accept access token" do
15+
client = Drip::Client.new do |config|
16+
config.access_token = "aaaa"
17+
end
18+
19+
assert_equal "aaaa", client.access_token
20+
end
21+
1322
should "accept default account id" do
1423
client = Drip::Client.new do |config|
1524
config.account_id = "1234567"
@@ -30,4 +39,32 @@ class Drip::ClientTest < Drip::TestCase
3039
assert_equal({ @key => [@data] }, @client.generate_resource(@key, @data))
3140
end
3241
end
42+
43+
context "given a personal api key" do
44+
setup do
45+
@key = "aaaa"
46+
@client = Drip::Client.new do |config|
47+
config.api_key = @key
48+
end
49+
end
50+
51+
should "use Basic authentication" do
52+
header = "Basic #{Base64.encode64(@key + ":")}".strip
53+
assert_equal header, @client.connection.headers["Authorization"]
54+
end
55+
end
56+
57+
context "given a OAuth access token" do
58+
setup do
59+
@key = "aaaa"
60+
@client = Drip::Client.new do |config|
61+
config.access_token = @key
62+
end
63+
end
64+
65+
should "use Bearer token authentication" do
66+
header = "Bearer #{@key}"
67+
assert_equal header, @client.connection.headers["Authorization"]
68+
end
69+
end
3370
end

0 commit comments

Comments
 (0)