class Intercom::Client

Attributes

api_version[R]
base_url[RW]
handle_rate_limit[R]
rate_limit_details[R]
timeouts[R]
token[R]

Public Class Methods

new(token: nil, base_url: 'https://api.intercom.io', handle_rate_limit: false, api_version: nil) click to toggle source
# File lib/intercom/client.rb, line 31
def initialize(token: nil, base_url: 'https://api.intercom.io', handle_rate_limit: false, api_version: nil)
  @token = token
  validate_credentials!

  @api_version = api_version
  validate_api_version!

  @base_url = base_url
  @rate_limit_details = {}
  @handle_rate_limit = handle_rate_limit
  @timeouts = {
    open_timeout: 30,
    read_timeout: 90
  }
end
set_base_url(base_url) click to toggle source
# File lib/intercom/client.rb, line 11
def set_base_url(base_url)
  proc do |o|
    old_url = o.base_url
    o.send(:base_url=, base_url)
    proc { |_obj| set_base_url(old_url).call(o) }
  end
end
set_timeouts(open_timeout: nil, read_timeout: nil) click to toggle source
# File lib/intercom/client.rb, line 19
def set_timeouts(open_timeout: nil, read_timeout: nil)
  proc do |o|
    old_timeouts = o.timeouts
    timeouts = {}
    timeouts[:open_timeout] = open_timeout if open_timeout
    timeouts[:read_timeout] = read_timeout if read_timeout
    o.send(:timeouts=, timeouts)
    proc { |_obj| set_timeouts(old_timeouts).call(o) }
  end
end

Public Instance Methods

admins() click to toggle source
# File lib/intercom/client.rb, line 47
def admins
  Intercom::Service::Admin.new(self)
end
articles() click to toggle source
# File lib/intercom/client.rb, line 51
def articles
  Intercom::Service::Article.new(self)
end
collections() click to toggle source
# File lib/intercom/client.rb, line 123
def collections
  Intercom::Service::Collection.new(self)
end
companies() click to toggle source
# File lib/intercom/client.rb, line 55
def companies
  Intercom::Service::Company.new(self)
end
contacts() click to toggle source
# File lib/intercom/client.rb, line 59
def contacts
  Intercom::Service::Contact.new(self)
end
conversations() click to toggle source
# File lib/intercom/client.rb, line 63
def conversations
  Intercom::Service::Conversation.new(self)
end
counts() click to toggle source
# File lib/intercom/client.rb, line 67
def counts
  Intercom::Service::Counts.new(self)
end
data_attributes() click to toggle source
# File lib/intercom/client.rb, line 119
def data_attributes
  Intercom::Service::DataAttribute.new(self)
end
delete(path, payload_hash) click to toggle source
# File lib/intercom/client.rb, line 139
def delete(path, payload_hash)
  execute_request Intercom::Request.delete(path, payload_hash)
end
events() click to toggle source
# File lib/intercom/client.rb, line 71
def events
  Intercom::Service::Event.new(self)
end
get(path, params) click to toggle source
# File lib/intercom/client.rb, line 127
def get(path, params)
  execute_request Intercom::Request.get(path, params)
end
jobs() click to toggle source
# File lib/intercom/client.rb, line 115
def jobs
  Intercom::Service::Job.new(self)
end
leads() click to toggle source
# File lib/intercom/client.rb, line 107
def leads
  Intercom::Service::Lead.new(self)
end
messages() click to toggle source
# File lib/intercom/client.rb, line 75
def messages
  Intercom::Service::Message.new(self)
end
notes() click to toggle source
# File lib/intercom/client.rb, line 79
def notes
  Intercom::Service::Note.new(self)
end
post(path, payload_hash) click to toggle source
# File lib/intercom/client.rb, line 131
def post(path, payload_hash)
  execute_request Intercom::Request.post(path, payload_hash)
end
put(path, payload_hash) click to toggle source
# File lib/intercom/client.rb, line 135
def put(path, payload_hash)
  execute_request Intercom::Request.put(path, payload_hash)
end
sections() click to toggle source
# File lib/intercom/client.rb, line 91
def sections
  Intercom::Service::Section.new(self)
end
segments() click to toggle source
# File lib/intercom/client.rb, line 87
def segments
  Intercom::Service::Segment.new(self)
end
subscriptions() click to toggle source
# File lib/intercom/client.rb, line 83
def subscriptions
  Intercom::Service::Subscription.new(self)
end
tags() click to toggle source
# File lib/intercom/client.rb, line 95
def tags
  Intercom::Service::Tag.new(self)
end
teams() click to toggle source
# File lib/intercom/client.rb, line 99
def teams
  Intercom::Service::Team.new(self)
end
users() click to toggle source
# File lib/intercom/client.rb, line 103
def users
  Intercom::Service::User.new(self)
end
visitors() click to toggle source
# File lib/intercom/client.rb, line 111
def visitors
  Intercom::Service::Visitor.new(self)
end

Private Instance Methods

execute_request(request) click to toggle source
# File lib/intercom/client.rb, line 155
def execute_request(request)
  request.handle_rate_limit = handle_rate_limit
  request.execute(@base_url, token: @token, api_version: @api_version, **timeouts)
ensure
  @rate_limit_details = request.rate_limit_details
end
timeouts=(timeouts) click to toggle source
# File lib/intercom/client.rb, line 164
def timeouts=(timeouts)
  @timeouts = @timeouts.merge(timeouts)
end
validate_api_version!() click to toggle source
# File lib/intercom/client.rb, line 150
def validate_api_version!
  error = MisconfiguredClientError.new('api_version must be either nil or a valid API version')
  raise error if @api_version && @api_version != 'Unstable' && Gem::Version.new(@api_version) < Gem::Version.new('1.0')
end
validate_credentials!() click to toggle source
# File lib/intercom/client.rb, line 145
def validate_credentials!
  error = MisconfiguredClientError.new('an access token must be provided')
  raise error if @token.nil?
end