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.

[View source]

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

#allowed_connectionsArray

Lists app allowed connections.

Returns:

  • (Array)

    array of self ids allowed to connect to your app.

[View source]

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

def allowed_connections
  acl.list
end

#device_idString

Gets the device id for the authenticated app.

Returns:

  • (String)

    device_id of the running app.

[View source]

69
70
71
# File 'lib/services/messaging.rb', line 69

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

[View source]

51
52
53
54
55
56
# 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) ⇒ Object

[View source]

97
98
99
100
101
102
# File 'lib/services/messaging.rb', line 97

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

[View source]

76
77
78
# File 'lib/services/messaging.rb', line 76

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

[View source]

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

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

[View source]

62
63
64
# File 'lib/services/messaging.rb', line 62

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

[View source]

86
87
88
89
90
91
92
93
94
95
# 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) {|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.

[View source]

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

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