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:



46
47
48
49
50
51
52
53
54
# File 'lib/circuit_client/client.rb', line 46

def initialize
  @host = 'eu.yourcircuit.com'
  @base_path = '/rest/v2'
  @protocol = 'https'
  @auth_method = :client_credentials
  @timeout = 60
  @trace = false
  yield self
end

Instance Attribute Details

#auth_methodObject

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



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

def auth_method
  @auth_method
end

#base_pathObject

The base path of the API



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

def base_path
  @base_path
end

#client_idObject

The client_id for authentication



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

def client_id
  @client_id
end

#client_secretObject

The client_secret for authentication



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

def client_secret
  @client_secret
end

#hostObject

Set the hostname of the circuit system



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

def host
  @host
end

#protocolObject

The protocol to use ‘http’ or ‘https’



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

def protocol
  @protocol
end

#timeoutObject

Timeout for http requests



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

def timeout
  @timeout
end

#traceObject

Enable tracing (outputs http requests to STDOUT)



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

def trace
  @trace
end

Instance Method Details

#access_tokenObject

The token used for authentication



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

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



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

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: 'ALL',
  } )
  data = JSON.parse(response.body)
  data['access_token']
end

#base_uriObject

Return URI with path elements



91
92
93
# File 'lib/circuit_client/client.rb', line 91

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



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

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



96
97
98
99
100
# File 'lib/circuit_client/client.rb', line 96

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

#connectionObject

The faraday http connection object



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

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



150
151
152
153
154
# File 'lib/circuit_client/client.rb', line 150

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

#create_group_conversation(participants, topic) ⇒ Object

Create a new group conversation



142
143
144
145
146
147
# File 'lib/circuit_client/client.rb', line 142

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


125
126
127
128
129
130
131
132
133
134
# File 'lib/circuit_client/client.rb', line 125

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



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

def current_user
  @current_user ||= 
end

#delete_group_conversation_participants(conv, participants) ⇒ Object

Remove participants from a conversation



157
158
159
160
161
# File 'lib/circuit_client/client.rb', line 157

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



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

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

#get_users(id) ⇒ Object

Get profile of a user



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

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

#get_users_presence(id) ⇒ Object

Get presence information of a user



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

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

#leave_group_conversation(conv) ⇒ Object

Remove the current_user from a conversation



164
165
166
# File 'lib/circuit_client/client.rb', line 164

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

#list_conversationsObject

List all conversation of the user



137
138
139
# File 'lib/circuit_client/client.rb', line 137

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