Class: SelfSDK::Messages::FactRequest

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

Constant Summary collapse

MSG_TYPE =
"identities.facts.query.req"
DEFAULT_EXP_TIMEOUT =
900

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?, #validate!

Constructor Details

#initialize(messaging) ⇒ FactRequest

Returns a new instance of FactRequest.



17
18
19
20
# File 'lib/messages/fact_request.rb', line 17

def initialize(messaging)
  @typ = MSG_TYPE
  super
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



15
16
17
# File 'lib/messages/fact_request.rb', line 15

def auth
  @auth
end

#factsObject

Returns the value of attribute facts.



15
16
17
# File 'lib/messages/fact_request.rb', line 15

def facts
  @facts
end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/messages/fact_request.rb', line 15

def options
  @options
end

Instance Method Details

#bodyObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/messages/fact_request.rb', line 86

def body
  b = {
    typ: MSG_TYPE,
    iss: @jwt.id,
    aud: @intermediary ? @intermediary : @to,
    sub: @to,
    iat: SelfSDK::Time.now.strftime('%FT%TZ'),
    exp: (SelfSDK::Time.now + @exp_timeout).strftime('%FT%TZ'),
    cid: @id,
    jti: SecureRandom.uuid,
    facts: @facts,
  }
  b[:options] = @options unless (@options.nil? || @options == false)
  b[:description] = @description unless (@description.nil? || @description.empty?)
  b[:allowed_until] = (SelfSDK::Time.now + @allowed_for).strftime('%FT%TZ') unless @allowed_for.nil?
  b[:auth] = @auth unless @auth.nil?
  b
end

#build_responseObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/messages/fact_request.rb', line 69

def build_response
  m = SelfSDK::Messages::FactResponse.new(@messaging)
  m.id = @id
  m.from = @to
  m.to = @from
  m.sub = @to
  m.audience = @from
  m.facts = @facts
  m
end

#parse(input, envelope = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/messages/fact_request.rb', line 50

def parse(input, envelope=nil)
  @input = input
  @typ = MSG_TYPE
  @payload = get_payload input
  @id = @payload[:cid]
  @from = @payload[:iss]
  @to = @payload[:sub]
  @audience = payload[:aud]
  @expires = @payload[:exp]
  @description = @payload.include?(:description) ? @payload[:description] : nil
  @facts = @payload[:facts]
  @options = @payload[:options]

  if envelope
    issuer = envelope.sender.split(":")
    @from_device = issuer.last
  end
end

#parse_facts(facts) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/messages/fact_request.rb', line 22

def parse_facts(facts)
  @facts = []
  facts.each do |fact|
    f = SelfSDK::Messages::Fact.new(@messaging)
    f.parse(fact)
    @facts << f.to_hash
  end
  @facts
end

#populate(selfid, facts, opts) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/messages/fact_request.rb', line 32

def populate(selfid, facts, opts)
  @id = SecureRandom.uuid
  @from = @client.jwt.id
  @to = selfid
  @facts = parse_facts(facts)

  @id = opts[:cid] if opts.include?(:cid)
  @options = opts.fetch(:options, false)
  @description = opts.include?(:description) ? opts[:description] : nil
  @exp_timeout = opts.fetch(:exp_timeout, DEFAULT_EXP_TIMEOUT)
  @allowed_for = opts.fetch(:allowed_for, nil)
  @auth = opts.fetch(:auth, false)

  @intermediary = if opts.include?(:intermediary)
                    opts[:intermediary]
                  end
end

#share_facts(facts) ⇒ Object



80
81
82
83
84
# File 'lib/messages/fact_request.rb', line 80

def share_facts(facts)
  m = build_response
  m.facts = parse_facts(facts)
  m.send_message
end