Class: RelayGrid::Resources::ChannelTokens

Inherits:
Object
  • Object
show all
Defined in:
lib/relaygrid/resources/channel_tokens.rb

Overview

Mints the short-lived token an end user's device presents when subscribing to the realtime channel.

A send already returns one (SendResult#push_token); this is how to get a fresh one once the hour is up, or without sending anything.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ChannelTokens

Returns a new instance of ChannelTokens.



11
12
13
# File 'lib/relaygrid/resources/channel_tokens.rb', line 11

def initialize(client)
  @client = client
end

Instance Method Details

#create(user_id:) ⇒ Hash

Returns { token:, expires_in:, expires_at: }.

Parameters:

  • user_id (String)

    your identifier for the user (external_user_id)

Returns:

  • (Hash)

    { token:, expires_in:, expires_at: }

Raises:



18
19
20
21
22
23
24
25
26
27
# File 'lib/relaygrid/resources/channel_tokens.rb', line 18

def create(user_id:)
  body = @client.post("/api/v1/channel_tokens", body: { external_user_id: user_id.to_s })
  expires_in = body["expires_in"]

  {
    token: body["token"],
    expires_in: expires_in,
    expires_at: expires_in ? Time.now + expires_in.to_i : nil,
  }
end

#token_for(user_id) ⇒ String

Returns just the token.

Returns:

  • (String)

    just the token



30
31
32
# File 'lib/relaygrid/resources/channel_tokens.rb', line 30

def token_for(user_id)
  create(user_id: user_id)[:token]
end