Sha256: e811cf46f138282a8f052d0c556f27d30e1a234282345de35d31b8602aaf8355

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require 'backport_dig' if RUBY_VERSION < '2.3'

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)
        orig_stack = @stack
        @stack = Excon::HyperMedia::Middlewares::HypertextCachePattern.new(orig_stack)
        super
      end

      def response_call(datum)
        return super unless (content_type = datum.dig(:response, :headers, 'Content-Type').to_s)

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

        super
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
excon-hypermedia-0.5.2 lib/excon/hypermedia/middleware.rb
excon-hypermedia-0.5.1 lib/excon/hypermedia/middleware.rb
excon-hypermedia-0.5.0 lib/excon/hypermedia/middleware.rb