Class: SelfSDK::Services::Requester
- Inherits:
-
Object
- Object
- SelfSDK::Services::Requester
- Defined in:
- lib/services/requester.rb
Overview
Input class to handle fact requests on self network.
Instance Attribute Summary collapse
-
#messaging ⇒ Object
readonly
Returns the value of attribute messaging.
Instance Method Summary collapse
-
#generate_deep_link(facts, callback, opts = {}) ⇒ String
Generates a deep link to authenticate with self app.
-
#generate_qr(facts, opts = {}) ⇒ String
Generates a QR code so users can send facts to your app.
-
#initialize(messaging, client) ⇒ SelfSDK::Services::Facts
constructor
Creates a new facts service.
-
#request(selfid, facts, opts = {}) {|request| ... } ⇒ Object
Sends a fact request to the specified selfid.
-
#request_via_intermediary(selfid, facts, opts = {}, &block) ⇒ Object
Sends a request through an intermediary.
-
#subscribe(auth, &block) ⇒ Object
Adds an observer for a fact response Whenever you receive a fact response registered observers will receive a notification.
Constructor Details
#initialize(messaging, client) ⇒ SelfSDK::Services::Facts
Creates a new facts service. Facts service mainly manages fact requests against self users wanting to share their verified facts with your app.
23 24 25 26 27 |
# File 'lib/services/requester.rb', line 23 def initialize(messaging, client) @messaging = messaging.client @messaging_service = messaging @client = client end |
Instance Attribute Details
#messaging ⇒ Object (readonly)
Returns the value of attribute messaging.
13 14 15 |
# File 'lib/services/requester.rb', line 13 def messaging @messaging end |
Instance Method Details
#generate_deep_link(facts, callback, opts = {}) ⇒ String
Generates a deep link to authenticate with self app.
129 130 131 132 133 134 135 |
# File 'lib/services/requester.rb', line 129 def generate_deep_link(facts, callback, opts = {}) opts[:request] = false selfid = opts.fetch(:selfid, "-") body = @client.jwt.encode(request(selfid, facts, opts)) @client.jwt.build_dynamic_link(body, @client.env, callback) end |
#generate_qr(facts, opts = {}) ⇒ String
Generates a QR code so users can send facts to your app.
114 115 116 117 118 119 |
# File 'lib/services/requester.rb', line 114 def generate_qr(facts, opts = {}) opts[:request] = false selfid = opts.fetch(:selfid, "-") req = request(selfid, facts, opts) ::RQRCode::QRCode.new(req, level: 'l') end |
#request(selfid, facts, opts = {}, &block) ⇒ Object #request(selfid, facts, opts = {}) ⇒ Object
Sends a fact request to the specified selfid. An fact request allows your app to access trusted facts of your user with its permission.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/services/requester.rb', line 48 def request(selfid, facts, opts = {}, &block) SelfSDK.logger.info "authenticating #{selfid}" rq = opts.fetch(:request, true) req = SelfSDK::Messages::FactRequest.new(@messaging) req.populate(selfid, prepare_facts(facts), opts) body = @client.jwt.prepare(req.body) return body unless rq # when a block is given the request will always be asynchronous. if block_given? @messaging.set_observer(req, timeout: req.exp_timeout, &block) return req. end if opts[:async] == true return req. end # Otherwise the request is synchronous req.request end |
#request_via_intermediary(selfid, facts, opts = {}, &block) ⇒ Object
Sends a request through an intermediary. An intermediary is an entity trusted by the user and acting as a proxy between you and the recipient of your fact request. Intermediaries usually do not provide the original user facts, but they create its own assertions based on your request and the user’s facts.
@param selfid [string] the receiver of the authentication request.
@param [Hash] opts the options to authenticate.
@option opts [String] intermediary an intermediary identity to be used.
@return [Object] SelfSDK:::Messages::FactRequest
82 83 84 85 |
# File 'lib/services/requester.rb', line 82 def request_via_intermediary(selfid, facts, opts = {}, &block) opts[:intermediary] = opts.fetch(:intermediary, DEFAULT_INTERMEDIARY) request(selfid, facts, opts, &block) end |
#subscribe(auth, &block) ⇒ Object
Adds an observer for a fact response Whenever you receive a fact response registered observers will receive a notification.
@yield [request] Invokes the block with a fact response message.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/services/requester.rb', line 91 def subscribe(auth, &block) if auth == true @auth_subscription = block else @fact_subscription = block end @messaging.subscribe :fact_response do |res| if res.auth_response? @auth_subscription&.call(res) else @fact_subscription&.call(res) end end end |