|
1 | 1 | 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' |
2 | 15 | require "intercom/contact" |
3 | 16 | require "intercom/user" |
4 | 17 | require "intercom/company" |
|
9 | 22 | require "intercom/conversation" |
10 | 23 | require "intercom/message" |
11 | 24 | require "intercom/admin" |
12 | | -require "intercom/count" |
13 | 25 | require "intercom/request" |
14 | 26 | require "intercom/subscription" |
15 | | -require "intercom/notification" |
16 | 27 | require "intercom/utils" |
17 | 28 | require "intercom/errors" |
18 | 29 | require "json" |
|
21 | 32 | # Intercom is a customer relationship management and messaging tool for web app owners |
22 | 33 | # |
23 | 34 | # 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 | | -# |
32 | 35 | 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 | | - |
176 | 36 | end |
0 commit comments