Skip to content

Commit 172945c

Browse files
committed
Merge pull request #226 from intercom/BL/oauth
Support token keyword for oauth clients
2 parents 437ef52 + 384a8f8 commit 172945c

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Using bundler:
3030

3131
```ruby
3232
intercom = Intercom::Client.new(app_id: 'my_app_id', api_key: 'my_api_key')
33+
34+
# With an OAuth token:
35+
intercom = Intercom::Client.new(token: 'my_token')
3336
```
3437

3538
You can get your `app_id` from the URL when you're logged into Intercom (it's the alphanumeric just after `/apps/`) and your API key from the API keys integration settings page (under your app settings - integrations in Intercom).

lib/intercom/client.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Intercom
22
class MisconfiguredClientError < StandardError; end
33
class Client
44
include Options
5-
attr_reader :base_url, :rate_limit_details
5+
attr_reader :base_url, :rate_limit_details, :username_part, :password_part
66

77
class << self
88
def set_base_url(base_url)
@@ -14,9 +14,14 @@ def set_base_url(base_url)
1414
end
1515
end
1616

17-
def initialize(app_id: 'my_app_id', api_key: 'my_api_key')
18-
@app_id = app_id
19-
@api_key = api_key
17+
def initialize(app_id: 'my_app_id', api_key: 'my_api_key', token: nil)
18+
if token
19+
@username_part = token
20+
@password_part = ""
21+
else
22+
@username_part = app_id
23+
@password_part = api_key
24+
end
2025
validate_credentials!
2126

2227
@base_url = 'https://api.intercom.io'
@@ -95,11 +100,11 @@ def delete(path, payload_hash)
95100

96101
def validate_credentials!
97102
error = MisconfiguredClientError.new("app_id and api_key must not be nil")
98-
fail error if @app_id.nil?
103+
fail error if @username_part.nil?
99104
end
100105

101106
def execute_request(request)
102-
result = request.execute(@base_url, username: @app_id, secret: @api_key)
107+
result = request.execute(@base_url, username: @username_part, secret: @password_part)
103108
@rate_limit_details = request.rate_limit_details
104109
result
105110
end

spec/unit/intercom/client_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,13 @@ module Intercom
2020
it 'should raise on nil credentials' do
2121
proc { Client.new(app_id: nil, api_key: nil) }.must_raise MisconfiguredClientError
2222
end
23+
24+
describe 'OAuth clients' do
25+
it 'supports "token"' do
26+
client = Client.new(token: 'foo')
27+
client.username_part.must_equal('foo')
28+
client.password_part.must_equal('')
29+
end
30+
end
2331
end
2432
end

0 commit comments

Comments
 (0)