Module: Ai2Web::Negotiator

Defined in:
lib/ai2web/negotiator.rb

Overview

Capability negotiation (spec section 5). Port of @ai2web/core negotiate().

Class Method Summary collapse

Class Method Details

.endpoint_of(name, value) ⇒ Object



8
9
10
11
12
# File 'lib/ai2web/negotiator.rb', line 8

def endpoint_of(name, value)
  return value["endpoint"] if value.is_a?(Hash) && value["endpoint"].is_a?(String)

  "/ai2w/#{name}"
end

.negotiate(manifest, agent = nil) ⇒ Object



14
15
16
17
18
19
20
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
# File 'lib/ai2web/negotiator.rb', line 14

def negotiate(manifest, agent = nil)
  m = Util.deep_stringify(manifest)
  agent = Util.deep_stringify(agent || {})
  caps = m["capabilities"].is_a?(Hash) ? m["capabilities"] : {}
  site_caps = caps.select { |_k, v| Util.enabled?(v) }.keys

  want_caps = agent.key?("capabilities") ? (agent["capabilities"] || []) : site_caps
  capabilities = site_caps.select { |c| want_caps.include?(c) }
  unsupported = want_caps.reject { |c| site_caps.include?(c) }

  # Only transports explicitly enabled are negotiable.
  transports = m["transports"].is_a?(Hash) ? m["transports"] : {}
  site_transports = transports.select { |_k, v| v.is_a?(Hash) && v["enabled"] == true }.keys
  want_transports = agent.key?("transports") ? (agent["transports"] || []) : site_transports
  transport = want_transports.find { |t| site_transports.include?(t) }

  auth_block = m["auth"].is_a?(Hash) ? m["auth"] : {}
  site_auth = auth_block["methods"] || ["none"]
  want_auth = agent.key?("auth") ? (agent["auth"] || []) : site_auth
  auth =
    if site_auth.include?("oauth2") && want_auth.include?("oauth2")
      "oauth2"
    else
      picked = want_auth.find { |a| site_auth.include?(a) }
      picked.nil? && site_auth.include?("none") ? "none" : picked
    end

  endpoints = {}
  capabilities.each { |c| endpoints[c] = endpoint_of(c, caps[c]) }
  if !transport.nil? && transports[transport].is_a?(Hash) && transports[transport]["endpoint"]
    endpoints[transport] = transports[transport]["endpoint"]
  end

  {
    negotiated: { transport: transport, capabilities: capabilities, auth: auth, endpoints: endpoints },
    unsupported: unsupported
  }
end