Class: CircuitClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/circuit_client/client.rb

Overview

client for accessing circuit API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Client

Initialize a new client

Examples

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

Returns a new CircuitClient::Client

Yields:

  • (_self)

Yield Parameters:



56
57
58
59
60
61
62
63
64
65
# File 'lib/circuit_client/client.rb', line 56

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

Instance Attribute Details

#auth_methodObject

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



35
36
37
# File 'lib/circuit_client/client.rb', line 35

def auth_method
  @auth_method
end

#auth_scopeObject

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



40
41
42
# File 'lib/circuit_client/client.rb', line 40

def auth_scope
  @auth_scope
end

#base_pathObject

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



18
19
20
# File 'lib/circuit_client/client.rb', line 18

def base_path
  @base_path
end

#client_idObject

The client_id for authentication



29
30
31
# File 'lib/circuit_client/client.rb', line 29

def client_id
  @client_id
end

#client_secretObject

The client_secret for authentication



32
33
34
# File 'lib/circuit_client/client.rb', line 32

def client_secret
  @client_secret
end

#hostObject

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



14
15
16
# File 'lib/circuit_client/client.rb', line 14

def host
  @host
end

#protocolObject

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



22
23
24
# File 'lib/circuit_client/client.rb', line 22

def protocol
  @protocol
end

#timeoutObject

Timeout for http requests Default: 60



26
27
28
# File 'lib/circuit_client/client.rb', line 26

def timeout
  @timeout
end

#traceObject

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



44
45
46
# File 'lib/circuit_client/client.rb', line 44

def trace
  @trace
end

Instance Method Details

#access_tokenObject

The token used for authentication



77
78
79
80
81
82
83
84
85
# File 'lib/circuit_client/client.rb', line 77

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_credentialsObject

Authenticate using client_credentials method



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/circuit_client/client.rb', line 88

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'), {
    client_id: @client_id,
    client_secret: @client_secret,
    grant_type: 'client_credentials',
    scope: @auth_scope,
  } )
  data = JSON.parse(response.body)
  data['access_token']
end

#base_uriObject

Return URI with path elements



102
103
104
# File 'lib/circuit_client/client.rb', line 102

def base_uri
  URI("#{@protocol}://#{@host}")
end

#build_api_uri(path) ⇒ Object

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



114
115
116
# File 'lib/circuit_client/client.rb', line 114

def build_api_uri(path)
  build_uri("#{@base_path}#{path}")
end

#build_uri(path) ⇒ Object

Returns an URI with the base_uri and the supplied path



107
108
109
110
111
# File 'lib/circuit_client/client.rb', line 107

def build_uri(path)
  uri = base_uri
  uri.path = path
  uri.to_s
end

#connectionObject

The faraday http connection object



68
69
70
71
72
73
74
# File 'lib/circuit_client/client.rb', line 68

def connection
  @connection ||= Faraday.new(url: base_uri.to_s) do |faraday|
    faraday.response :logger if @trace
    faraday.use CircuitClient::ErrorMiddleware
    faraday.adapter :typhoeus
  end
end

#create_direct_conversation(participant) ⇒ Object

Create a new 1:1 conversation



161
162
163
164
165
# File 'lib/circuit_client/client.rb', line 161

def create_direct_conversation(participant)
  call(:post, '/conversations/direct', {
    participant: participant,
  } )
end

#create_group_conversation(participants, topic) ⇒ Object

Create a new group conversation



153
154
155
156
157
158
# File 'lib/circuit_client/client.rb', line 153

def create_group_conversation(participants, topic)
  call(:post, '/conversations/group', {
    participants: participants,
    topic: topic,
  } )
end

#create_message(conv, text, **options) ⇒ Object

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'
)


136
137
138
139
140
141
142
143
144
145
# File 'lib/circuit_client/client.rb', line 136

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_userObject

A cached version of the current connections user profile



185
186
187
# File 'lib/circuit_client/client.rb', line 185

def current_user
  @current_user ||= 
end

#delete_group_conversation_participants(conv, participants) ⇒ Object

Remove participants from a conversation



168
169
170
171
172
# File 'lib/circuit_client/client.rb', line 168

def delete_group_conversation_participants(conv, participants)
  call(:delete, "/conversations/group/#{conv}/participants", {
    participants: participants,
  } )
end

#get_user_profileObject

Get the profile of the connections user



180
181
182
# File 'lib/circuit_client/client.rb', line 180

def 
  call(:get, "/users/profile")
end

#get_users(id) ⇒ Object

Get profile of a user



190
191
192
# File 'lib/circuit_client/client.rb', line 190

def get_users(id)
  call(:get, "/users/#{id}")
end

#get_users_presence(id) ⇒ Object

Get presence information of a user



195
196
197
# File 'lib/circuit_client/client.rb', line 195

def get_users_presence(id)
  call(:get, "/users/#{id}/presence")
end

#leave_group_conversation(conv) ⇒ Object

Remove the current_user from a conversation



175
176
177
# File 'lib/circuit_client/client.rb', line 175

def leave_group_conversation(conv)
  delete_group_conversation_participants(conv, [current_user['userId']])
end

#list_conversationsObject

List all conversation of the user



148
149
150
# File 'lib/circuit_client/client.rb', line 148

def list_conversations
  call(:get, "/conversations")
end