class Restcomm::Util::Capability

Public Class Methods

new(account_sid = nil, auth_token = nil) click to toggle source
   # File lib/restcomm-ruby/util/capability.rb
 7 def initialize(account_sid = nil, auth_token = nil)
 8   @account_sid = account_sid || Restcomm.account_sid
 9   @auth_token = auth_token || Restcomm.auth_token
10   if @account_sid.nil? || @auth_token.nil?
11     raise ArgumentError, 'Account SID and auth token are required'
12   end
13   @capabilities = []
14 end

Public Instance Methods

allow_client_incoming(client_name) click to toggle source
   # File lib/restcomm-ruby/util/capability.rb
16 def allow_client_incoming(client_name)
17   @client_name = client_name # stash for use in outgoing
18   scope_params = { 'clientName' => client_name }
19   @capabilities << scope_uri_for('client', 'incoming', scope_params)
20 end
allow_client_outgoing(app_sid, params = {}) click to toggle source
   # File lib/restcomm-ruby/util/capability.rb
22 def allow_client_outgoing(app_sid, params = {})
23   @allow_client_outgoing = true
24   @outgoing_scope_params = { 'appSid' => app_sid }
25   unless params.empty?
26     @outgoing_scope_params['appParams'] = url_encode params
27   end
28 end
allow_event_stream(filters = {}) click to toggle source
   # File lib/restcomm-ruby/util/capability.rb
30 def allow_event_stream(filters = {})
31   scope_params = { 'path' => '/2010-04-01/Events' }
32   scope_params['params'] = filters unless filters.empty?
33   @capabilities << scope_uri_for('stream', 'subscribe', scope_params)
34 end
generate(ttl = 3600) click to toggle source
   # File lib/restcomm-ruby/util/capability.rb
41 def generate(ttl = 3600)
42 
43   capabilities = @capabilities.clone # we need a local copy to work on
44 
45   # build the outgoing scope lazily so that we can use @client_name
46   if @allow_client_outgoing
47     params = @outgoing_scope_params
48     params.merge!('clientName' => @client_name) if @client_name
49     capabilities << scope_uri_for('client', 'outgoing', params)
50   end
51 
52   payload = {
53     'scope' => capabilities.join(' '),
54     'iss' => @account_sid,
55     'exp' => (Time.now.to_i + ttl),
56   }
57 
58   JWT.encode payload, @auth_token
59 
60 end
scope_uri_for(service, privilege, params = {}) click to toggle source
   # File lib/restcomm-ruby/util/capability.rb
36 def scope_uri_for(service, privilege, params = {})
37   scope_uri = "scope:#{service}:#{privilege}"
38   scope_uri << "?#{url_encode(params)}" unless params.empty?
39 end