class CircuitClient::Client

client for accessing circuit API

Attributes

auth_method[RW]

The authentication method to use (currently only :client_credentials supported)

auth_scope[RW]

Comma-delimited set of permissions that the application requests ALL, READ_USER_PROFILE, WRITE_USER_PROFILE, READ_CONVERSATIONS, WRITE_CONVERSATIONS, READ_USER, CALLS Default: ALL

base_path[RW]

The base path of the API Default: /rest/v2

client_id[RW]

The client_id for authentication

client_secret[RW]

The client_secret for authentication

host[RW]

Set the hostname of the circuit system Default: eu.yourcircuit.com

protocol[RW]

The protocol to use 'http' or 'https' Default: 'https'

timeout[RW]

Timeout for http requests Default: 60

trace[RW]

Enable tracing (outputs http requests to STDOUT) Default: false (disabled)

Public Class Methods

new() { |self| ... } click to toggle source

Initialize a new client

Examples

CircuitClient::Client.new do |c|
  c.client_id = '4de34a3...'
  c.client_secret = '234df2...'
end

Returns a new CircuitClient::Client

# File lib/circuit_client/client.rb, line 54
def initialize
  @host = 'eu.yourcircuit.com'
  @base_path = '/rest/v2'
  @protocol = 'https'
  @auth_method = :client_credentials
  @auth_scope = 'ALL'
  @timeout = 60
  @trace = false
  yield self
end

Public Instance Methods

access_token() click to toggle source

The token used for authentication

# File lib/circuit_client/client.rb, line 75
def access_token
  return @access_token unless @access_token.nil?
  case @auth_method
  when :client_credentials
    auth_client_credentials
  else
    raise "Unknown auth_method: #{@auth_method}"
  end
end
auth_client_credentials() click to toggle source

Authenticate using client_credentials method

# File lib/circuit_client/client.rb, line 86
def auth_client_credentials
  raise 'client_id parameter required' if @client_id.nil?
  raise 'client_secret parameter required' if @client_secret.nil?

  response = connection.post(build_uri('/oauth/token')) do |req|
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = URI.encode_www_form(
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      scope: @auth_scope
    )
  end

  data = JSON.parse(response.body)
  data['access_token']
end
base_uri() click to toggle source

Return URI with path elements

# File lib/circuit_client/client.rb, line 105
def base_uri
  URI("#{@protocol}://#{@host}")
end
build_api_uri(path) click to toggle source

Returns an URI and with a path relative to the base_path of the API

# File lib/circuit_client/client.rb, line 117
def build_api_uri(path)
  build_uri("#{@base_path}#{path}")
end
build_uri(path) click to toggle source

Returns an URI with the base_uri and the supplied path

# File lib/circuit_client/client.rb, line 110
def build_uri(path)
  uri = base_uri
  uri.path = path
  uri.to_s
end
connection() click to toggle source

The faraday http connection object

# File lib/circuit_client/client.rb, line 66
def connection
  @connection ||= Faraday.new(url: base_uri.to_s) do |c|
    c.response :logger if @trace
    c.use CircuitClient::ErrorMiddleware
    c.adapter Faraday.default_adapter
  end
end
create_direct_conversation(participant) click to toggle source

Create a new 1:1 conversation

# File lib/circuit_client/client.rb, line 168
def create_direct_conversation(participant)
  call(
    :post,
    '/conversations/direct',
    participant: participant
  )
end
create_group_conversation(participants, topic) click to toggle source

Create a new group conversation

# File lib/circuit_client/client.rb, line 158
def create_group_conversation(participants, topic)
  call(
    :post,
    '/conversations/group',
    participants: participants,
    topic: topic
  )
end
create_message(conv, text, **options) click to toggle source

Create a new message in a existing conversation

Examples

client.create_message(
  '<convId>',
  'my message text...',
  subject: 'Todays meeting'
)

To send to an existing message item use :item_id parameter:

client.create_message(
  '<convId>',
  'my message text...',
  item_id: 'itemId'
)
# File lib/circuit_client/client.rb, line 139
def create_message(conv, text, **options)
  item_id = options[:item_id]
  path = "/conversations/#{conv}/messages"
  path += "/#{item_id}" unless item_id.nil?
  options.delete(:item_id)
  call(
    :post,
    path,
    content: text,
    **options
  )
end
current_user() click to toggle source

A cached version of the current connections user profile

# File lib/circuit_client/client.rb, line 196
def current_user
  @current_user ||= get_user_profile
end
delete_group_conversation_participants(conv, participants) click to toggle source

Remove participants from a conversation

# File lib/circuit_client/client.rb, line 177
def delete_group_conversation_participants(conv, participants)
  call(
    :delete,
    "/conversations/group/#{conv}/participants",
    participants: participants
  )
end
get_user_profile() click to toggle source

Get the profile of the connections user

# File lib/circuit_client/client.rb, line 191
def get_user_profile
  call(:get, '/users/profile')
end
get_users(id) click to toggle source

Get profile of a user

# File lib/circuit_client/client.rb, line 201
def get_users(id)
  call(:get, "/users/#{id}")
end
get_users_presence(id) click to toggle source

Get presence information of a user

# File lib/circuit_client/client.rb, line 206
def get_users_presence(id)
  call(:get, "/users/#{id}/presence")
end
leave_group_conversation(conv) click to toggle source

Remove the current_user from a conversation

# File lib/circuit_client/client.rb, line 186
def leave_group_conversation(conv)
  delete_group_conversation_participants(conv, [current_user['userId']])
end
list_conversations() click to toggle source

List all conversation of the user

# File lib/circuit_client/client.rb, line 153
def list_conversations
  call(:get, '/conversations')
end

Private Instance Methods

call(method, path, payload = {}, headers = {}) click to toggle source
# File lib/circuit_client/client.rb, line 212
def call(method, path, payload = {}, headers = {})
  response = connection.send(method) do |req|
    req.url build_api_uri(path)
    req.options.timeout = @timeout
    req.headers['Accept'] = 'application/json'
    req.headers['Authorization'] = "Bearer #{access_token}"
    case method
    when :post
      req.body = payload.to_json
      req.headers['Content-Type'] = 'application/json'
    when :get, :delete
      req.params = payload
    end
  end
  return JSON.parse(response.body) if response.success?
end