Class: Pubnub::GrantToken

Inherits:
SingleEvent show all
Includes:
Concurrent::Async, Validator::GrantToken
Defined in:
lib/pubnub/events/grant_token.rb

Overview

Holds grant token functionality

Instance Attribute Summary

Attributes inherited from Event

#callback, #channel, #channel_group, #fresh_clone, #given_options, #group, #idle_timeout, #open_timeout, #origin, #presence_callback, #read_timeout, #ssl, #state, #wildcard_channel, #with_presence

Instance Method Summary collapse

Methods included from Validator::GrantToken

#validate!

Methods included from Validator::CommonValidator

#validate_origin, #validate_publish_key, #validate_subscribe_key, #validate_user_id

Methods inherited from Event

#finalized?, #send_request, #sync?, #uri

Methods included from Event::EFormatter

#format_channels, #format_envelopes, #format_group

Constructor Details

#initialize(options, app) ⇒ GrantToken

Returns a new instance of GrantToken.



7
8
9
10
11
12
13
14
15
16
# File 'lib/pubnub/events/grant_token.rb', line 7

def initialize(options, app)
  @event = current_operation
  @telemetry_name = :l_pamv3
  @uuids = options[:uuids] || {}
  options[:channels] = options[:channels] || {}
  options[:channel_groups] = options[:channel_groups] || {}
  @spaces_permissions = options[:spaces_permissions] || {}
  @users_permissions = options[:users_permissions] || {}
  super
end

Instance Method Details

#fireObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pubnub/events/grant_token.rb', line 18

def fire
  Pubnub.logger.debug('Pubnub::GrantToken') { "Fired event #{self.class}" }
  if @authorized_user_id != nil
    uuid = @authorized_user_id
  elsif @authorized_uuid != nil
    uuid = @authorized_uuid
  else
    uuid = nil
  end

  raw_body = {
    ttl: @ttl,
    permissions: {
      meta: @meta,
      uuid: uuid,
      resources: prepare_permissions(:resource, @channels, @channel_groups, @uuids, @spaces_permissions, @users_permissions),
      patterns: prepare_permissions(:pattern, @channels, @channel_groups, @uuids, @spaces_permissions, @users_permissions)
    }.select { |_, v| v }
  }
  body = Formatter.format_message(raw_body, nil, false)
  response = send_request(body, { "Content-Type": "application/json" })

  envelopes = fire_callbacks(handle(response, uri))
  finalize_event(envelopes)
  envelopes
end