Class: Restcomm::TaskRouter::Capability

Inherits:
Object
  • Object
show all
Defined in:
lib/restcomm-ruby/task_router/capability.rb

Constant Summary collapse

TASK_ROUTER_BASE_URL =
'https://taskrouter.restcomm.com'
TASK_ROUTER_VERSION =
'v1'
TASK_ROUTER_WEBSOCKET_BASE_URL =
'https://event-bridge.restcomm.com/v1/wschannels'
REQUIRED =
{required: true}
OPTIONAL =
{required: false}

Instance Method Summary collapse

Constructor Details

#initialize(account_sid, auth_token, workspace_sid, worker_sid) ⇒ Capability

Returns a new instance of Capability.



12
13
14
15
16
17
18
19
20
# File 'lib/restcomm-ruby/task_router/capability.rb', line 12

def initialize(, auth_token, workspace_sid, worker_sid)
  @account_sid = 
  @auth_token = auth_token
  @workspace_sid = workspace_sid
  @worker_sid = worker_sid
  @policies = []
  allow_websocket_requests
  allow_activity_list_fetch
end

Instance Method Details

#add_policy(url, method, query_filters = nil, post_filters = nil, allowed = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/restcomm-ruby/task_router/capability.rb', line 43

def add_policy(url, method, query_filters = nil, post_filters = nil, allowed = true)
  policy = {
    url: url,
    method: method,
    query_filter: query_filters || {},
    post_filter: post_filters || {},
    allow: allowed
  }

  @policies.push(policy)
end

#allow_task_reservation_updatesObject



38
39
40
41
# File 'lib/restcomm-ruby/task_router/capability.rb', line 38

def allow_task_reservation_updates
  task_url = "#{workspace_url}/Tasks/**"
  add_policy(task_url, "POST", nil, {ReservationStatus: REQUIRED})
end

#allow_worker_activity_updatesObject



30
31
32
# File 'lib/restcomm-ruby/task_router/capability.rb', line 30

def allow_worker_activity_updates
  add_policy(worker_url, "POST", nil, {ActivitySid: REQUIRED})
end

#allow_worker_fetch_attributesObject



34
35
36
# File 'lib/restcomm-ruby/task_router/capability.rb', line 34

def allow_worker_fetch_attributes
  add_policy(worker_url, "GET")
end

#generate_token(ttl = 3600) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/restcomm-ruby/task_router/capability.rb', line 55

def generate_token(ttl = 3600)
  payload = {
    iss: @account_sid,
    exp: (Time.now.to_i + ttl),
    version: @version,
    friendly_name: @worker_sid,
    policies: @policies,
    account_sid: @account_sid,
    worker_sid: @worker_sid,
    channel: @worker_sid,
    workspace_sid: @workspace_sid
  }

  JWT.encode payload, @auth_token
end

#worker_urlObject



26
27
28
# File 'lib/restcomm-ruby/task_router/capability.rb', line 26

def worker_url
  "#{workspace_url}/Workers/#{@worker_sid}"
end

#workspace_urlObject



22
23
24
# File 'lib/restcomm-ruby/task_router/capability.rb', line 22

def workspace_url
  "#{TASK_ROUTER_BASE_URL}/#{TASK_ROUTER_VERSION}/Workspaces/#{@workspace_sid}"
end