Class: CircuitClient::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ 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:



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

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)



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

def auth_method
  @auth_method
end

#base_pathObject

The base path of the API



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

def base_path
  @base_path
end

#client_idObject

The client_id for authentication



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

def client_id
  @client_id
end

#client_secretObject

The client_secret for authentication



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

def client_secret
  @client_secret
end

#hostObject

Set the hostname of the circuit system



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

def host
  @host
end

#protocolObject

The protocol to use ‘http’ or ‘https’



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

def protocol
  @protocol
end

#timeoutObject

Timeout for http requests



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

def timeout
  @timeout
end

#traceObject

Enable tracing (outputs http requests to STDOUT)



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

def trace
  @trace
end

Instance Method Details

#access_tokenObject

The token used for authentication



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

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



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

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



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

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



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

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



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

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

#connectionObject

The faraday http connection object



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

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



133
134
135
136
137
# File 'lib/circuit_client/client.rb', line 133

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

#create_group_conversation(participants, topic) ⇒ Object

Create a new group conversation



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

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

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

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



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

def create_message(conv, text, **options)
  call(:post, "/conversations/#{conv}/messages", {
    'content' => text,
    **options,
  } )
end

#current_userObject

A cached version of the current connections user profile



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

def current_user
  @current_user ||= 
end

#delete_group_conversation_participants(conv, participants) ⇒ Object

Remove participants from a conversation



140
141
142
143
144
# File 'lib/circuit_client/client.rb', line 140

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



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

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

#get_users(id) ⇒ Object

Get profile of a user



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

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

#get_users_presence(id) ⇒ Object

Get presence information of a user



167
168
169
# File 'lib/circuit_client/client.rb', line 167

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

#leave_group_conversation(conv) ⇒ Object

Remove the current_user from a conversation



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

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

#list_conversationsObject

List all conversation of the user



120
121
122
# File 'lib/circuit_client/client.rb', line 120

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