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:


54
55
56
57
58
59
60
61
62
63
# 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

Instance Attribute Details

#auth_methodObject

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


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

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


38
39
40
# File 'lib/circuit_client/client.rb', line 38

def auth_scope
  @auth_scope
end

#base_pathObject

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


16
17
18
# File 'lib/circuit_client/client.rb', line 16

def base_path
  @base_path
end

#client_idObject

The client_id for authentication


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

def client_id
  @client_id
end

#client_secretObject

The client_secret for authentication


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

def client_secret
  @client_secret
end

#hostObject

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


12
13
14
# File 'lib/circuit_client/client.rb', line 12

def host
  @host
end

#protocolObject

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


20
21
22
# File 'lib/circuit_client/client.rb', line 20

def protocol
  @protocol
end

#timeoutObject

Timeout for http requests Default: 60


24
25
26
# File 'lib/circuit_client/client.rb', line 24

def timeout
  @timeout
end

#traceObject

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


42
43
44
# File 'lib/circuit_client/client.rb', line 42

def trace
  @trace
end

Instance Method Details

#access_tokenObject

The token used for authentication


75
76
77
78
79
80
81
82
83
# 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_credentialsObject

Authenticate using client_credentials method


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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_uriObject

Return URI with path elements


105
106
107
# File 'lib/circuit_client/client.rb', line 105

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


117
118
119
# File 'lib/circuit_client/client.rb', line 117

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


110
111
112
113
114
# File 'lib/circuit_client/client.rb', line 110

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

#connectionObject

The faraday http connection object


66
67
68
69
70
71
72
# 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) ⇒ Object

Create a new 1:1 conversation


168
169
170
171
172
173
174
# 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) ⇒ Object

Create a new group conversation


158
159
160
161
162
163
164
165
# 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) ⇒ 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'
)

139
140
141
142
143
144
145
146
147
148
149
150
# 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_userObject

A cached version of the current connections user profile


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

def current_user
  @current_user ||= 
end

#delete_group_conversation_participants(conv, participants) ⇒ Object

Remove participants from a conversation


177
178
179
180
181
182
183
# 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_profileObject

Get the profile of the connections user


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

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

#get_users(id) ⇒ Object

Get profile of a user


201
202
203
# File 'lib/circuit_client/client.rb', line 201

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

#get_users_presence(id) ⇒ Object

Get presence information of a user


206
207
208
# File 'lib/circuit_client/client.rb', line 206

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

#leave_group_conversation(conv) ⇒ Object

Remove the current_user from a conversation


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

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

#list_conversationsObject

List all conversation of the user


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

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