Class: Rack::Webfinger

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/webfinger.rb,
lib/rack/webfinger/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(app, data_provider) ⇒ Webfinger

Returns a new instance of Webfinger.



9
10
11
12
# File 'lib/rack/webfinger.rb', line 9

def initialize(app, data_provider)
  @app = app
  @data_provider = data_provider
end

Instance Method Details

#call(env) ⇒ Object



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/rack/webfinger.rb', line 16

def call(env)
  request = Rack::Request.new(env)

  if request.path != '/.well-known/webfinger'
    return @app.call(env) if @app
    return not_found
  end
  
  resource = request.params['resource']

  if !resource
    return [400, { 'content-type' => 'text/plain' }, ['Missing resource parameter']]
  end

  # We need this because Rack's handling of the query string by default overwrites
  # subsequent 'rel' parameters.

  query_params = Rack::Utils.parse_query(env['QUERY_STRING'])
  rel_params = Array(query_params['rel'])

  data = @data_provider.call(resource, rel_params)
      
  return not_found if !data

  response_data = {
    subject: resource,
    aliases: data[:aliases],
    links: data[:links]
  }
  filtered_data = filter_by_rel(response_data, rel_params)

  [200,
    { 'content-type' => 'application/jrd+json' },
    [JSON.generate(filtered_data)]
  ]
end

#not_foundObject



14
# File 'lib/rack/webfinger.rb', line 14

def not_found = [404, { 'content-type' => 'text/plain' }, ['Resource not found']]