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(name) ⇒ Object?

Returns an attestation by name

Parameters:

  • name (String)

    the name of the fact to retrieve the attestation for

Returns:

  • (Object, nil)

    the first attestation of the fact, or nil if no fact is found



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

def attestation(name)
  f = fact(name)
  return nil if f.nil?

  f.attestations.first
end

#attestation_values_for(name) ⇒ Object



79
80
81
82
# File 'lib/messages/fact_response.rb', line 79

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

#attestations_for(name) ⇒ Object



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

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

  f.attestations
end

#auth_response?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/messages/fact_response.rb', line 110

def auth_response?
  @auth == true
end

#bodyObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/messages/fact_response.rb', line 91

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



114
115
116
117
118
119
120
121
122
# File 'lib/messages/fact_response.rb', line 114

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



84
85
86
87
88
89
# File 'lib/messages/fact_response.rb', line 84

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