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.


20
21
22
# File 'lib/services/messaging.rb', line 20

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

TODO : we should try to remove this accessor.


11
12
13
# File 'lib/services/messaging.rb', line 11

def client
  @client
end

Instance Method Details

#allowed_connectionsArray

Lists app allowed connections.

Returns:

  • (Array)

    array of self ids allowed to connect to your app.


43
44
45
# File 'lib/services/messaging.rb', line 43

def allowed_connections
  acl.list
end

#device_idString

Gets the device id for the authenticated app.

Returns:

  • (String)

    device_id of the running app.


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

def device_id
  @client.device_id
end

#is_permitted?(id) ⇒ Boolean

Checks if you’re permitting messages from a specific self identifier

Returns:

  • (Boolean)

    yes|no


49
50
51
52
53
54
# File 'lib/services/messaging.rb', line 49

def is_permitted?(id)
  conns = allowed_connections
  return true if conns.include? "*"
  return true if conns.include? id
  return false
end

#notify(recipient, message) ⇒ Object


95
96
97
98
99
100
# File 'lib/services/messaging.rb', line 95

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


74
75
76
# File 'lib/services/messaging.rb', line 74

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

#permit_connection(selfid) ⇒ Boolean

Permits incoming messages from the a identity.

Parameters:

  • selfid (String)

    to be allowed.

Returns:

  • (Boolean)

    success / failure


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

def permit_connection(selfid)
  acl.allow selfid
end

#revoke_connection(selfid) ⇒ Boolean

Revokes incoming messages from the given identity.

Parameters:

  • selfid (String)

    to be denied

Returns:

  • (Boolean)

    success / failure


60
61
62
# File 'lib/services/messaging.rb', line 60

def revoke_connection(selfid)
  acl.deny selfid
end

#send(recipient, request) ⇒ Object

Send custom mmessage

Parameters:

  • recipient (string)

    recipient for the message

  • type (string)

    message type

  • request (hash)

    message to be sent


84
85
86
87
88
89
90
91
92
93
# File 'lib/services/messaging.rb', line 84

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) {|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::AuthenticationResp.MSG_TYPE

Yields:

  • (SelfSDK::Messages::Message)

    receives incoming message.


29
30
31
# File 'lib/services/messaging.rb', line 29

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