Class: SelfSDK::Messages::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messaging) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
# File 'lib/messages/base.rb', line 16

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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#exp_timeoutObject

Returns the value of attribute exp_timeout.



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

def exp_timeout
  @exp_timeout
end

#expiresObject

Returns the value of attribute expires.



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

def expires
  @expires
end

#fieldsObject

Returns the value of attribute fields.



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

def fields
  @fields
end

#fromObject

Returns the value of attribute from.



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

def from
  @from
end

#from_deviceObject

Returns the value of attribute from_device.



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

def from_device
  @from_device
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#intermediaryObject

Returns the value of attribute intermediary.



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

def intermediary
  @intermediary
end

#payloadObject

Returns the value of attribute payload.



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

def payload
  @payload
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#subObject

Returns the value of attribute sub.



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

def sub
  @sub
end

#toObject

Returns the value of attribute to.



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

def to
  @to
end

#to_deviceObject

Returns the value of attribute to_device.



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

def to_device
  @to_device
end

#typObject

Returns the value of attribute typ.



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

def typ
  @typ
end

Instance Method Details

#accepted?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/messages/base.rb', line 64

def accepted?
  status == "accepted"
end

#encrypt_message(message, recipients) ⇒ Object



52
53
54
# File 'lib/messages/base.rb', line 52

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

#errored?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/messages/base.rb', line 68

def errored?
  status == "errored"
end

#rejected?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/messages/base.rb', line 60

def rejected?
  status == "rejected"
end

#requestObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/messages/base.rb', line 24

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

#send_message(device_id = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/messages/base.rb', line 40

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)


56
57
58
# File 'lib/messages/base.rb', line 56

def unauthorized?
  status == "unauthorized"
end

#validate!(original) ⇒ Object

Raises:

  • (::StandardError)


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/messages/base.rb', line 72

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.round > (SelfSDK::Time.now + 1).round
end