Sha256: 77b094543b71144ee02fb89c93fb8950bdd799efd4b622fa6a60ff9c4891b781

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'addressable/template'
require 'addressable/uri'
require 'excon'
require 'excon/addressable/parser'
require 'excon/addressable/version'

Excon.defaults[:uri_parser] = Excon::Addressable::Parser

# Excon
#
# We inject the `expand` key to the allowed lists of keys to be used when
# creating a request, or connection object. Excon does not enforce this yet, but
# it does print a warning, so this makes things future-proof.
module Excon
  VALID_REQUEST_KEYS << :expand
  VALID_CONNECTION_KEYS << :expand

  module Addressable
    # Middleware
    #
    # Parses a Templated URI string and merges it with the provided variables.
    #
    class Middleware < Excon::Middleware::Base
      def request_call(datum)
        # we need to convert a query hash (or string) to the proper format for
        # Addressable to work with. We also need to remove the `?` character
        # that Excon prepends to the final query string.
        datum[:query] = Excon::Utils.query_string(datum)[1..-1]

        url = ::Addressable::URI.new(datum)

        if (template = ::Addressable::Template.new(url)) && template.variables.any?
          uri = template.expand(datum[:expand].to_h)
          datum.merge!(uri.to_hash)
        end

        super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
excon-addressable-0.4.1 lib/excon/addressable.rb
excon-addressable-0.4.0 lib/excon/addressable.rb