Sha256: 131ddd91acfe9da0dacead37d4c54586b8660e4b6a55b433a138b518d3644a05

Contents?: true

Size: 797 Bytes

Versions: 2

Compression:

Stored size: 797 Bytes

Contents

# frozen_string_literal: true

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][:hypermedia] = if datum[:hypermedia].nil?
          content_type.include?('hal+json')
        else
          datum[:hypermedia]
        end

        super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
excon-hypermedia-0.3.0 lib/excon/hypermedia/middleware.rb
excon-hypermedia-0.2.0 lib/excon/hypermedia/middleware.rb