Class: SelfSDK::Services::Facts
- Inherits:
-
Object
- Object
- SelfSDK::Services::Facts
- Defined in:
- lib/services/facts.rb
Overview
Input class to handle authentication requests on self network.
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 = {}, &block) ⇒ Object
Sends a fact request to the specified selfid.
-
#request_via_intermediary(selfid, facts, opts = {}, &block) ⇒ Object
Sends a request through an intermediary.
-
#subscribe(&block) ⇒ Object
Adds an observer for a fact response Whenever you receive a fact response registered observers will receive a notification.
Constructor Details
permalink #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.
19 20 21 22 23 |
# File 'lib/services/facts.rb', line 19 def initialize(messaging, client) @messaging = messaging.client @messaging_service = messaging @client = client end |
Instance Method Details
permalink #generate_deep_link(facts, callback, opts = {}) ⇒ String
Generates a deep link to authenticate with self app.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/services/facts.rb', line 110 def generate_deep_link(facts, callback, opts = {}) opts[:request] = false selfid = opts.fetch(:selfid, "-") body = @client.jwt.encode(request(selfid, facts, opts)) if @client.env.empty? return "https://joinself.page.link/?link=#{callback}%3Fqr=#{body}&apn=com.joinself.app" elsif @client.env == 'development' return "https://joinself.page.link/?link=#{callback}%3Fqr=#{body}&apn=com.joinself.app.dev" end "https://joinself.page.link/?link=#{callback}%3Fqr=#{body}&apn=com.joinself.app.#{@client.env}" end |
permalink #generate_qr(facts, opts = {}) ⇒ String
Generates a QR code so users can send facts to your app.
95 96 97 98 99 100 |
# File 'lib/services/facts.rb', line 95 def generate_qr(facts, opts = {}) opts[:request] = false selfid = opts.fetch(:selfid, "-") req = request(selfid, facts, opts) ::RQRCode::QRCode.new(req, level: 'l') end |
permalink #request(selfid, facts, opts = {}) {|request| ... } ⇒ 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.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/services/facts.rb', line 42 def request(selfid, facts, opts = {}, &block) SelfSDK.logger.info "authenticating #{selfid}" rq = opts.fetch(:request, true) if rq raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid) end 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 # Otherwise the request is synchronous req.request end |
permalink #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
75 76 77 78 |
# File 'lib/services/facts.rb', line 75 def request_via_intermediary(selfid, facts, opts = {}, &block) opts[:intermediary] = opts.fetch(:intermediary, DEFAULT_INTERMEDIARY) request(selfid, facts, opts, &block) end |
permalink #subscribe(&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.
84 85 86 |
# File 'lib/services/facts.rb', line 84 def subscribe(&block) @messaging.subscribe(:fact_response, &block) end |