Class: BroadcastHub::StreamKeyContext

Inherits:
Object
  • Object
show all
Defined in:
app/services/broadcast_hub/stream_key_context.rb

Overview

Immutable context object shared by stream key authorization and resolution.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_name:, tenant_id:, current_user:, session_id:, params: {}) ⇒ StreamKeyContext

Returns a new instance of StreamKeyContext.

Parameters:

  • resource_name (String, Symbol, nil)

    resource requested by the client

  • tenant_id (Object)

    tenant identity for scoped streams

  • current_user (Object)

    authenticated user on the connection

  • session_id (String, nil)

    optional session identifier

  • params (Hash) (defaults to: {})

    normalized channel params



30
31
32
33
34
35
36
37
# File 'app/services/broadcast_hub/stream_key_context.rb', line 30

def initialize(resource_name:, tenant_id:, current_user:, session_id:, params: {})
  @resource_name = resource_name
  @tenant_id = tenant_id
  @current_user = current_user
  @session_id = session_id
  @params = (params || {}).dup.freeze
  freeze
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



6
7
8
# File 'app/services/broadcast_hub/stream_key_context.rb', line 6

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'app/services/broadcast_hub/stream_key_context.rb', line 6

def params
  @params
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



6
7
8
# File 'app/services/broadcast_hub/stream_key_context.rb', line 6

def resource_name
  @resource_name
end

#session_idObject (readonly)

Returns the value of attribute session_id.



6
7
8
# File 'app/services/broadcast_hub/stream_key_context.rb', line 6

def session_id
  @session_id
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



6
7
8
# File 'app/services/broadcast_hub/stream_key_context.rb', line 6

def tenant_id
  @tenant_id
end

Class Method Details

.from_connection(connection:, params: {}) ⇒ BroadcastHub::StreamKeyContext

Builds a context from an Action Cable connection and channel params.

Parameters:

  • connection (Object)

    Action Cable connection

  • params (#to_h, #to_unsafe_h) (defaults to: {})

    channel subscription params

Returns:



13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/broadcast_hub/stream_key_context.rb', line 13

def self.from_connection(connection:, params: {})
  normalized_params = normalize_params(params)

  new(
    resource_name: normalized_params[:resource],
    tenant_id: normalized_params[:tenant],
    current_user: connection_value(connection, :current_user),
    session_id: connection_value(connection, :session_id) || normalized_params[:session_id],
    params: normalized_params
  )
end