Skip to content

Commit 7cd38b3

Browse files
committed
Merge pull request #159 from intercom/jo/client-based-intercom-ruby
version 3 - client based intercom ruby
2 parents e10dc56 + 86af5dc commit 7cd38b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+901
-1059
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
language: ruby
22
rvm:
3-
- 1.9.3
4-
- 2.0.0
53
- 2.1.0
64
- 2.2.0

README.md

Lines changed: 74 additions & 121 deletions
Large diffs are not rendered by default.

changes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
3.0.0
2+
- New version, client-based access.
3+
14
2.5.4
25
- Acquire support
36

intercom.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency "fakeweb", ["~> 1.3"]
2626

2727
spec.add_dependency 'json', '~> 1.8'
28-
spec.required_ruby_version = '>= 1.9.3'
28+
spec.required_ruby_version = '>= 2.1.0'
2929
end

lib/intercom.rb

Lines changed: 13 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
require "intercom/version"
2+
require 'intercom/service/admin'
3+
require 'intercom/service/company'
4+
require 'intercom/service/contact'
5+
require 'intercom/service/conversation'
6+
require 'intercom/service/event'
7+
require 'intercom/service/message'
8+
require 'intercom/service/note'
9+
require 'intercom/service/subscription'
10+
require 'intercom/service/segment'
11+
require 'intercom/service/tag'
12+
require 'intercom/service/user'
13+
require 'intercom/options'
14+
require 'intercom/client'
215
require "intercom/contact"
316
require "intercom/user"
417
require "intercom/company"
@@ -9,10 +22,8 @@
922
require "intercom/conversation"
1023
require "intercom/message"
1124
require "intercom/admin"
12-
require "intercom/count"
1325
require "intercom/request"
1426
require "intercom/subscription"
15-
require "intercom/notification"
1627
require "intercom/utils"
1728
require "intercom/errors"
1829
require "json"
@@ -21,156 +32,5 @@
2132
# Intercom is a customer relationship management and messaging tool for web app owners
2233
#
2334
# This library provides ruby bindings for the Intercom API (https://api.intercom.io)
24-
#
25-
# == Basic Usage
26-
# === Configure Intercom with your access credentials
27-
# Intercom.app_id = "my_app_id"
28-
# Intercom.app_api_key = "my_api_key"
29-
# === Make requests to the API
30-
# Intercom::User.find(:email => "[email protected]")
31-
#
3235
module Intercom
33-
@hostname = "api.intercom.io"
34-
@protocol = "https"
35-
36-
@endpoints = nil
37-
@current_endpoint = nil
38-
@app_id = nil
39-
@app_api_key = nil
40-
@rate_limit_details = {}
41-
42-
def self.app_id=(app_id)
43-
@app_id = app_id
44-
end
45-
46-
def self.app_id
47-
@app_id
48-
end
49-
50-
def self.app_api_key=(app_api_key)
51-
@app_api_key = app_api_key
52-
end
53-
54-
def self.app_api_key
55-
@app_api_key
56-
end
57-
58-
def self.rate_limit_details=(rate_limit_details)
59-
@rate_limit_details = rate_limit_details
60-
end
61-
62-
def self.rate_limit_details
63-
@rate_limit_details
64-
end
65-
66-
# This method is obsolete and used to warn of backwards incompatible changes on upgrading
67-
def self.api_key=(val)
68-
raise ArgumentError, "#{compatibility_warning_text} #{compatibility_workaround_text} #{related_docs_text}"
69-
end
70-
71-
private
72-
73-
def self.target_base_url
74-
raise ArgumentError, "#{configuration_required_text} #{related_docs_text}" if [@app_id, @app_api_key].any?(&:nil?)
75-
basic_auth_part = "#{@app_id}:#{@app_api_key}@"
76-
current_endpoint.gsub(/(https?:\/\/)(.*)/, "\\1#{basic_auth_part}\\2")
77-
end
78-
79-
def self.related_docs_text
80-
"See https://github.com/intercom/intercom-ruby for usage examples."
81-
end
82-
83-
def self.compatibility_warning_text
84-
"It looks like you are upgrading from an older version of the intercom-ruby gem. Please note that this new version (#{Intercom::VERSION}) is not backwards compatible. "
85-
end
86-
87-
def self.compatibility_workaround_text
88-
"To get rid of this error please set Intercom.app_api_key and don't set Intercom.api_key."
89-
end
90-
91-
def self.configuration_required_text
92-
"You must set both Intercom.app_id and Intercom.app_api_key to use this client."
93-
end
94-
95-
def self.send_request_to_path(request)
96-
request.execute(target_base_url)
97-
rescue Intercom::ServiceUnavailableError => e
98-
if endpoints.length > 1
99-
retry_on_alternative_endpoint(request)
100-
else
101-
raise e
102-
end
103-
end
104-
105-
def self.retry_on_alternative_endpoint(request)
106-
@current_endpoint = alternative_random_endpoint
107-
request.execute(target_base_url)
108-
end
109-
110-
def self.current_endpoint
111-
return @current_endpoint if @current_endpoint && @endpoint_randomized_at > (Time.now - (60 * 5))
112-
@endpoint_randomized_at = Time.now
113-
@current_endpoint = random_endpoint
114-
end
115-
116-
def self.random_endpoint
117-
endpoints.shuffle.first
118-
end
119-
120-
def self.alternative_random_endpoint
121-
(endpoints.shuffle - [@current_endpoint]).first
122-
end
123-
124-
def self.post(path, payload_hash)
125-
send_request_to_path(Intercom::Request.post(path, payload_hash))
126-
end
127-
128-
def self.delete(path, payload_hash)
129-
send_request_to_path(Intercom::Request.delete(path, payload_hash))
130-
end
131-
132-
def self.put(path, payload_hash)
133-
send_request_to_path(Intercom::Request.put(path, payload_hash))
134-
end
135-
136-
def self.get(path, params)
137-
send_request_to_path(Intercom::Request.get(path, params))
138-
end
139-
140-
def self.check_required_params(params, path=nil) #nodoc
141-
return if path.eql?("users")
142-
raise ArgumentError.new("Expected params Hash, got #{params.class}") unless params.is_a?(Hash)
143-
raise ArgumentError.new("Either email or user_id must be specified") unless params.keys.any? { |key| %W(email user_id).include?(key.to_s) }
144-
end
145-
146-
def self.protocol #nodoc
147-
@protocol
148-
end
149-
150-
def self.protocol=(override) #nodoc
151-
@protocol = override
152-
end
153-
154-
def self.hostname #nodoc
155-
@hostname
156-
end
157-
158-
def self.hostname=(override) #nodoc
159-
@hostname = override
160-
end
161-
162-
def self.endpoint=(endpoint) #nodoc
163-
self.endpoints = [endpoint]
164-
@current_endpoint = nil
165-
end
166-
167-
def self.endpoints=(endpoints) #nodoc
168-
@endpoints = endpoints
169-
@current_endpoint = nil
170-
end
171-
172-
def self.endpoints
173-
@endpoints || ["#{@protocol}://#{hostname}"]
174-
end
175-
17636
end

lib/intercom/admin.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
require 'intercom/api_operations/list'
21
require 'intercom/traits/api_resource'
32

43
module Intercom
54
class Admin
6-
include ApiOperations::List
75
include Traits::ApiResource
86
end
97
end

lib/intercom/api_operations/convert.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
module Intercom
44
module ApiOperations
55
module Convert
6-
def convert(user)
7-
from_response(
8-
Intercom.post(
6+
def convert(contact, user)
7+
Intercom::User.new.from_response(
8+
@client.post(
99
"/contacts/convert",
1010
{
11-
contact: { user_id: user_id },
12-
user: user.identity_hash
11+
contact: { user_id: contact.user_id },
12+
user: identity_hash(user)
1313
}
1414
)
1515
)

lib/intercom/api_operations/count.rb

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/intercom/api_operations/delete.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
module Intercom
44
module ApiOperations
55
module Delete
6-
7-
def delete
8-
collection_name = Utils.resource_class_to_collection_name(self.class)
9-
Intercom.delete("/#{collection_name}/#{id}", {})
10-
self
6+
def delete(object)
7+
collection_name = Utils.resource_class_to_collection_name(collection_class)
8+
@client.delete("/#{collection_name}/#{object.id}", {})
9+
object
1110
end
12-
1311
end
1412
end
1513
end

lib/intercom/api_operations/find.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
module Intercom
22
module ApiOperations
33
module Find
4-
module ClassMethods
5-
def find(params)
6-
raise BadRequestError, "#{self}#find takes a hash as its parameter but you supplied #{params.inspect}" unless params.is_a? Hash
7-
collection_name = Utils.resource_class_to_collection_name(self)
8-
if params[:id]
9-
response = Intercom.get("/#{collection_name}/#{params[:id]}", {})
10-
else
11-
response = Intercom.get("/#{collection_name}", params)
12-
end
13-
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
14-
from_api(response)
4+
def find(params)
5+
raise BadRequestError, "#{self}#find takes a hash as its parameter but you supplied #{params.inspect}" unless params.is_a? Hash
6+
collection_name = Utils.resource_class_to_collection_name(collection_class)
7+
if params[:id]
8+
response = @client.get("/#{collection_name}/#{params[:id]}", {})
9+
else
10+
response = @client.get("/#{collection_name}", params)
1511
end
16-
end
17-
18-
def self.included(base)
19-
base.extend(ClassMethods)
12+
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
13+
from_api(response)
2014
end
2115
end
2216
end

0 commit comments

Comments
 (0)