Class: SelfSDK::Services::Messaging

Inherits:
Object
  • Object
show all
Defined in:
lib/services/messaging.rb

Overview

Input class to interact with self network messaging.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SelfSDK::Services::Messaging

Creates a new messaging service. Messaging service basically allows you to subscribe to certain types of messages, and manage who can send you messages or not.

Parameters:

  • client (SelfSDK::Messaging)

    messaging object.



22
23
24
# File 'lib/services/messaging.rb', line 22

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

TODO : we should try to remove this accessor.



13
14
15
# File 'lib/services/messaging.rb', line 13

def client
  @client
end

Instance Method Details

#device_idString

Gets the device id for the authenticated app.

Returns:

  • (String)

    device_id of the running app.



38
39
40
# File 'lib/services/messaging.rb', line 38

def device_id
  @client.device_id
end

#notify(recipient, message) ⇒ Object



65
66
67
68
69
70
# File 'lib/services/messaging.rb', line 65

def notify(recipient, message)
  send recipient, {
      typ: 'identities.notify',
      description: message
    }
end

#observer(cid) ⇒ Object

Get the observer by uuid

Parameters:

  • cid (String)

    conversation id



45
46
47
# File 'lib/services/messaging.rb', line 45

def observer(cid)
  @client.uuid_observer[cid]
end

#send(recipients, request) ⇒ Object

Send custom mmessage

Parameters:

  • recipients (String|array)

    recipient for the message

  • type (string)

    message type

  • request (hash)

    message to be sent



54
55
56
57
58
59
60
61
62
63
# File 'lib/services/messaging.rb', line 54

def send(recipients, request)
  request[:jti] = SecureRandom.uuid unless request.include?(:jti)
  request[:iss] = @client.jwt.id
  request[:iat] = SelfSDK::Time.now.strftime('%FT%TZ')
  request[:exp] = (SelfSDK::Time.now + 300).strftime('%FT%TZ')        
  request[:cid] = SecureRandom.uuid unless request.include?(:cid)

  @client.send_custom(recipients, request)
  request
end

#subscribe(type) {|SelfSDK::Messages::Message| ... } ⇒ Object

Subscribes to a specific message type and attaches the given observer which will be executed when a meeting criteria message is received.

Parameters:

  • type (String)

    message type (ex: SelfSDK::Messages::FactRequest.MSG_TYPE

Yields:

  • (SelfSDK::Messages::Message)

    receives incoming message.



31
32
33
# File 'lib/services/messaging.rb', line 31

def subscribe(type, &block)
  @client.subscribe(type, &block)
end