Class: SelfSDK::Messages::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/messages/base.rb

Direct Known Subclasses

AuthenticationMessage, FactRequest, FactResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messaging) ⇒ Base

Returns a new instance of Base.

[View source]

12
13
14
15
16
17
# File 'lib/messages/base.rb', line 12

def initialize(messaging)
  @client = messaging.client
  @jwt = @client.jwt
  @messaging = messaging
  @device_id = "1"
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.


8
9
10
# File 'lib/messages/base.rb', line 8

def description
  @description
end

#exp_timeoutObject

Returns the value of attribute exp_timeout.


8
9
10
# File 'lib/messages/base.rb', line 8

def exp_timeout
  @exp_timeout
end

#expiresObject

Returns the value of attribute expires.


8
9
10
# File 'lib/messages/base.rb', line 8

def expires
  @expires
end

#fieldsObject

Returns the value of attribute fields.


8
9
10
# File 'lib/messages/base.rb', line 8

def fields
  @fields
end

#fromObject

Returns the value of attribute from.


8
9
10
# File 'lib/messages/base.rb', line 8

def from
  @from
end

#from_deviceObject

Returns the value of attribute from_device.


8
9
10
# File 'lib/messages/base.rb', line 8

def from_device
  @from_device
end

#idObject

Returns the value of attribute id.


8
9
10
# File 'lib/messages/base.rb', line 8

def id
  @id
end

#inputObject

Returns the value of attribute input.


8
9
10
# File 'lib/messages/base.rb', line 8

def input
  @input
end

#intermediaryObject

Returns the value of attribute intermediary.


8
9
10
# File 'lib/messages/base.rb', line 8

def intermediary
  @intermediary
end

#payloadObject

Returns the value of attribute payload.


8
9
10
# File 'lib/messages/base.rb', line 8

def payload
  @payload
end

#statusObject

Returns the value of attribute status.


8
9
10
# File 'lib/messages/base.rb', line 8

def status
  @status
end

#subObject

Returns the value of attribute sub.


8
9
10
# File 'lib/messages/base.rb', line 8

def sub
  @sub
end

#toObject

Returns the value of attribute to.


8
9
10
# File 'lib/messages/base.rb', line 8

def to
  @to
end

#to_deviceObject

Returns the value of attribute to_device.


8
9
10
# File 'lib/messages/base.rb', line 8

def to_device
  @to_device
end

#typObject

Returns the value of attribute typ.


8
9
10
# File 'lib/messages/base.rb', line 8

def typ
  @typ
end

Instance Method Details

#accepted?Boolean

Returns:

  • (Boolean)
[View source]

54
55
56
# File 'lib/messages/base.rb', line 54

def accepted?
  status == "accepted"
end

#encrypt_message(message, recipient, recipient_device) ⇒ Object

[View source]

42
43
44
# File 'lib/messages/base.rb', line 42

def encrypt_message(message, recipient, recipient_device)
  @messaging.encryption_client.encrypt(message, recipient, recipient_device)
end

#errored?Boolean

Returns:

  • (Boolean)
[View source]

58
59
60
# File 'lib/messages/base.rb', line 58

def errored?
  status == "errored"
end

#rejected?Boolean

Returns:

  • (Boolean)
[View source]

50
51
52
# File 'lib/messages/base.rb', line 50

def rejected?
  status == "rejected"
end

#requestObject

[View source]

19
20
21
22
23
24
25
26
27
28
# File 'lib/messages/base.rb', line 19

def request
  check_credits!
  msgs = []
  devices.each do |d|
    msgs << proto(d)
    SelfSDK.logger.info "synchronously messaging to #{@to}:#{d}"
  end
  res = @messaging.send_and_wait_for_response(msgs, self)
  res
end

#send_message(device_id = nil) ⇒ Object

[View source]

30
31
32
33
34
35
36
37
38
39
40
# File 'lib/messages/base.rb', line 30

def send_message(device_id = nil)
  check_credits!
  dds = devices
  dds = [device_id] if device_id
  res = []
  dds.each do |d|
    res << @messaging.send_message(proto(d))
    SelfSDK.logger.info "asynchronously requested information to #{@to}:#{d}"
  end
  res.first
end

#unauthorized?Boolean

Returns:

  • (Boolean)
[View source]

46
47
48
# File 'lib/messages/base.rb', line 46

def unauthorized?
  status == "unauthorized"
end

#validate!(original) ⇒ Object

Raises:

  • (::StandardError)
[View source]

62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/messages/base.rb', line 62

def validate!(original)
  unless original.nil?
    raise ::StandardError.new("bad response audience") if @audience != original.from
    if original.intermediary.nil?
      raise ::StandardError.new("bad issuer") if @from != original.to
    else
      raise ::StandardError.new("bad issuer") if @from != original.intermediary
    end
  end
  raise ::StandardError.new("expired message") if @expires < SelfSDK::Time.now
  raise ::StandardError.new("issued too soon") if @issued > SelfSDK::Time.now
end