Sha256: 32b15a32f2c5645d133ce68500f404cb158602fa8b6e30b39f6f1e34b82b341f

Contents?: true

Size: 897 Bytes

Versions: 1

Compression:

Stored size: 897 Bytes

Contents

# frozen_string_literal: true

Excon.defaults[:middlewares].delete(Excon::Addressable::Middleware)
Excon.defaults[:middlewares].unshift(Excon::Addressable::Middleware)

module Excon
  module HyperMedia
    # 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.
    #
    class Middleware < Excon::Middleware::Base
      def request_call(datum)
        return super unless (content_type = datum.dig(:response, :headers, 'Content-Type').to_s)

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

        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
excon-hypermedia-0.4.0 lib/excon/hypermedia/middleware.rb