class CircuitClient::Client
client for accessing circuit API
Attributes
The authentication method to use (currently only :client_credentials supported)
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
The base path of the API Default: /rest/v2
The client_id
for authentication
The client_secret
for authentication
Set the hostname of the circuit system Default: eu.yourcircuit.com
The protocol to use 'http' or 'https' Default: 'https'
Timeout for http requests Default: 60
Enable tracing (outputs http requests to STDOUT) Default: false (disabled)
Public Class Methods
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
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
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
Return URI with path elements
# File lib/circuit_client/client.rb, line 105 def base_uri URI("#{@protocol}://#{@host}") end
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
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
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 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 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 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
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
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 the profile of the connections user
# File lib/circuit_client/client.rb, line 191 def get_user_profile call(:get, '/users/profile') end
Get profile of a user
# File lib/circuit_client/client.rb, line 201 def get_users(id) call(:get, "/users/#{id}") end
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
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 all conversation of the user
# File lib/circuit_client/client.rb, line 153 def list_conversations call(:get, '/conversations') end
Private Instance Methods
# 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