Class: David::ResourceDiscovery

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/david/resource_discovery.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ResourceDiscovery

Returns a new instance of ResourceDiscovery.


5
6
7
# File 'lib/david/resource_discovery.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#_call(env) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/david/resource_discovery.rb', line 13

def _call(env)
  return @app.call(env) if env['PATH_INFO']      != '/.well-known/core'
  return [405, {}, []]  if env['REQUEST_METHOD'] != 'GET'

  @env = env

  filtered = routes_hash.select { |link| filter(link) }
  body     = filtered.keys.map(&:to_s).join(',')

  # TODO On multicast, do not respond if result set empty.

  [
    200,
    {
      'Content-Type'   => 'application/link-format',
      'Content-Length' => body.bytesize.to_s
    },
    [body]
  ]
end

#call(env) ⇒ Object


9
10
11
# File 'lib/david/resource_discovery.rb', line 9

def call(env)
  dup._call(env)
end

#register(controller, options) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/david/resource_discovery.rb', line 34

def register(controller, options)
  name    = controller.controller_name
  default = options.delete(:default)

  routes_hash.each do |link, route|
    next unless route[:controller] == name

    link.merge!(default) unless default.nil?

    attrs = options[route[:action].to_sym]
    link.merge!(attrs) unless attrs.nil?
  end
end