Class: FaradayMiddleware::RelsMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
spaceship/lib/spaceship/helper/rels_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RelsMiddleware

Returns a new instance of RelsMiddleware.



11
12
13
14
# File 'spaceship/lib/spaceship/helper/rels_middleware.rb', line 11

def initialize(app, options = {})
  @app = app
  @options = options
end

Instance Method Details

#call(environment) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'spaceship/lib/spaceship/helper/rels_middleware.rb', line 16

def call(environment)
  @app.call(environment).on_complete do |env|
    links = (env.response_headers["Link"] || "").split(', ').map do |link|
      href, name = link.match(/<(.*?)>; rel="(\w+)"/).captures

      [name.to_sym, href]
    end

    env.rels = Hash[*links.flatten]
  end
end