class SelfSDK::Services::Messaging

Input class to interact with self network messaging.

Attributes

client[RW]

TODO : we should try to remove this accessor. @attr_accessor [SelfSDK::Messaging] internal messaging client.

Public Class Methods

new(client) click to toggle source

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.

@param client [SelfSDK::Messaging] messaging object.

@return [SelfSDK::Services::Messaging] authentication service.

# File lib/services/messaging.rb, line 22
def initialize(client)
  @client = client
end

Public Instance Methods

allowed_connections() click to toggle source

Lists app allowed connections. @return [Array] array of self ids allowed to connect to your app.

# File lib/services/messaging.rb, line 45
def allowed_connections
  acl.list
end
device_id() click to toggle source

Gets the device id for the authenticated app.

@return [String] device_id of the running app.

# File lib/services/messaging.rb, line 69
def device_id
  @client.device_id
end
is_permitted?(id) click to toggle source

Checks if you're permitting messages from a specific self identifier @return [Boolean] yes|no

# File lib/services/messaging.rb, line 51
def is_permitted?(id)
  conns = allowed_connections
  return true if conns.include? "*"
  return true if conns.include? id
  return false
end
notify(recipient, message) click to toggle source
# File lib/services/messaging.rb, line 97
def notify(recipient, message)
  send recipient, {
      typ: 'identities.notify',
      description: message
    }
end
observer(cid) click to toggle source

Get the observer by uuid

@param [String] cid conversation id

# File lib/services/messaging.rb, line 76
def observer(cid)
  @client.uuid_observer[cid]
end
permit_connection(selfid) click to toggle source

Permits incoming messages from the a identity.

@param [String] selfid to be allowed. @return [Boolean] success / failure

# File lib/services/messaging.rb, line 39
def permit_connection(selfid)
  acl.allow selfid
end
revoke_connection(selfid) click to toggle source

Revokes incoming messages from the given identity.

@param [String] selfid to be denied @return [Boolean] success / failure

# File lib/services/messaging.rb, line 62
def revoke_connection(selfid)
  acl.deny selfid
end
send(recipient, request) click to toggle source

Send custom mmessage

@param recipient [string] recipient for the message @param type [string] message type @param request [hash] message to be sent

# File lib/services/messaging.rb, line 86
def send(recipient, request)
  request[:jti] = SecureRandom.uuid
  request[:iss] = @client.jwt.id
  request[:sub] = recipient
  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(recipient, request)
end
subscribe(type, &block) click to toggle source

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

@param [String] type message type (ex: SelfSDK::Messages::AuthenticationResp.MSG_TYPE @yield [SelfSDK::Messages::Message] receives incoming message.

# File lib/services/messaging.rb, line 31
def subscribe(type, &block)
  @client.subscribe(type, &block)
end

Private Instance Methods

acl() click to toggle source
# File lib/services/messaging.rb, line 106
def acl
  @acl ||= ACL.new(@client)
end