Class: SelfSDK::Messages::FactResponse

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

Constant Summary collapse

MSG_TYPE =
"identities.facts.query.resp"

Instance Attribute Summary collapse

Attributes inherited from Base

#description, #exp_timeout, #expires, #fields, #from, #from_device, #id, #input, #intermediary, #payload, #status, #sub, #to, #to_device, #typ

Instance Method Summary collapse

Methods inherited from Base

#accepted?, #encrypt_message, #errored?, #rejected?, #request, #send_message, #unauthorized?

Constructor Details

#initialize(messaging) ⇒ FactResponse

Returns a new instance of FactResponse.



16
17
18
19
# File 'lib/messages/fact_response.rb', line 16

def initialize(messaging)
  @typ = MSG_TYPE
  super
end

Instance Attribute Details

#audienceObject

Returns the value of attribute audience.



14
15
16
# File 'lib/messages/fact_response.rb', line 14

def audience
  @audience
end

#authObject

Returns the value of attribute auth.



14
15
16
# File 'lib/messages/fact_response.rb', line 14

def auth
  @auth
end

#factsObject

Returns the value of attribute facts.



14
15
16
# File 'lib/messages/fact_response.rb', line 14

def facts
  @facts
end

Instance Method Details

#attestation_values_for(name) ⇒ Object



66
67
68
69
# File 'lib/messages/fact_response.rb', line 66

def attestation_values_for(name)
  aa = attestations_for(name)
  aa.map{|a| a.value}
end

#attestations_for(name) ⇒ Object



60
61
62
63
64
# File 'lib/messages/fact_response.rb', line 60

def attestations_for(name)
  f = fact(name)
  return [] if f.nil?
  f.attestations
end

#auth_response?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/messages/fact_response.rb', line 97

def auth_response?
  @auth == true
end

#bodyObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/messages/fact_response.rb', line 78

def body
  encoded_facts = []
  @facts.each do |fact|
    encoded_facts.push(fact.to_hash)
  end

  { typ: MSG_TYPE,
    iss: @jwt.id,
    sub: @sub || @to,
    aud: @audience,
    iat: SelfSDK::Time.now.strftime('%FT%TZ'),
    exp: (SelfSDK::Time.now + 3600).strftime('%FT%TZ'),
    cid: @id,
    jti: SecureRandom.uuid,
    status: @status,
    facts: encoded_facts,
    auth: @auth }
end

#fact(name) ⇒ Object



54
55
56
57
58
# File 'lib/messages/fact_response.rb', line 54

def fact(name)
  name = @messaging.source.normalize_fact_name(name)
  name = "image_hash" if name == 'photo'
  @facts.select{|f| f.name == name}.first
end

#object(hash) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/messages/fact_response.rb', line 101

def object(hash)
  payload[:objects].each do |o|
    if o[:image_hash] == hash
      return SelfSDK::Chat::FileObject.new(
        @messaging.client.jwt.auth_token,
        @messaging.client.self_url).build_from_object(o)
    end
  end
end

#parse(input, envelope = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/messages/fact_response.rb', line 21

def parse(input, envelope=nil)
  @input = input
  @typ = MSG_TYPE
  @payload = get_payload input
  @id = payload[:cid]
  @from = payload[:iss]
  @to = payload[:sub]
  @expires = ::Time.parse(payload[:exp])
  @issued = ::Time.parse(payload[:iat])
  @audience = payload[:aud]
  @status = payload[:status]
  @auth = payload[:auth]
  @facts = []
  payload[:facts] = [] if payload[:facts].nil?
  payload[:facts].each do |f|
    begin
      fact = SelfSDK::Messages::Fact.new(@messaging)
      if f[:fact] == 'photo'
        f[:fact] = :image_hash
      end
      fact.parse(f)
      @facts.push(fact)
    rescue StandardError => e
      SelfSDK.logger.info e.message
    end
  end
  if envelope
    issuer = envelope.sender.split(":")
    @from_device = issuer.last
  end

end

#validate!(original) ⇒ Object



71
72
73
74
75
76
# File 'lib/messages/fact_response.rb', line 71

def validate!(original)
  super
  @facts.each do |f|
    f.validate! original
  end
end