Class: Excon::HyperMedia::Middleware

Inherits:
Middleware::Base
  • Object
show all
Defined in:
lib/excon/hypermedia/middleware.rb

Overview

Middleware

This middleware sets the ‘hypermedia` datum to `true`, if the returned `Content-Type` header contains `hal+json`.

If the ‘hypermedia` attribute is already set for the connection, it will be left alone by this middleware.

Instance Method Summary collapse

Instance Method Details

#request_call(datum) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/excon/hypermedia/middleware.rb', line 27

def request_call(datum)
  # if `hcp` is enabled, insert the `HypertextCachePattern` middleware in
  # the middleware stack right after this one.
  if datum[:hcp]
    orig_stack = @stack
    @stack = Excon::HyperMedia::Middlewares::HypertextCachePattern.new(orig_stack)
  end

  super
end

#response_call(datum) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/excon/hypermedia/middleware.rb', line 38

def response_call(datum)
  return super unless (headers = datum.dig(:response, :headers))
  return super unless (match = headers.find { |k, v| k.downcase == 'content-type' })
  content_type = match[1].to_s

  datum[:response][:hypermedia] = if datum[:hypermedia].nil?
    content_type.include?('hal+json')
  else
    datum[:hypermedia]
  end

  super
end