Skip to content

Commit e334a6c

Browse files
committed
refactor endpoints to use protocol/hostname when no endpoints set
1 parent 0d038d4 commit e334a6c

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/intercom.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ def self.api_key=(api_key)
4848
def self.target_base_url
4949
raise ArgumentError, "You must set both Intercom.app_id and Intercom.api_key to use this client. See https://github.com/intercom/intercom-ruby for usage examples." if [@app_id, @api_key].any?(&:nil?)
5050
basic_auth_part = "#{@app_id}:#{@api_key}@"
51-
if @endpoints.nil? || @endpoints.empty?
52-
"#{protocol}://#{basic_auth_part}#{hostname}"
53-
else
54-
endpoint = @endpoints[0]
55-
endpoint.gsub(/(https?:\/\/)(.*)/, "\\1#{basic_auth_part}\\2")
56-
end
51+
endpoints[0].gsub(/(https?:\/\/)(.*)/, "\\1#{basic_auth_part}\\2")
5752
end
5853

5954
def self.send_request_to_path(request)
@@ -63,6 +58,7 @@ def self.send_request_to_path(request)
6358
@endpoints = @endpoints.rotate
6459
request.execute(target_base_url)
6560
end
61+
6662
end
6763

6864
def self.post(path, payload_hash)
@@ -111,6 +107,10 @@ def self.endpoints=(endpoints) #nodoc
111107
@endpoints = endpoints
112108
end
113109

110+
def self.endpoints
111+
@endpoints || ["#{@protocol}://#{hostname}"]
112+
end
113+
114114
# Raised when the credentials you provide don't match a valid account on Intercom.
115115
# Check that you have set <b>Intercom.app_id=</b> and <b>Intercom.api_key=</b> correctly.
116116
class AuthenticationError < StandardError;

spec/unit/intercom_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
Intercom.endpoints = ["http://example.com","https://localhost:7654"]
5050
Intercom.target_base_url.must_equal "http://abc123:[email protected]"
5151
end
52+
53+
it "has endpoints" do
54+
Intercom.endpoints.must_equal ["https://api.intercom.io"]
55+
Intercom.endpoints = ["http://example.com","https://localhost:7654"]
56+
Intercom.endpoints.must_equal ["http://example.com","https://localhost:7654"]
57+
end
5258
end
5359
end
5460

0 commit comments

Comments
 (0)