diff --git a/lib/faraday/rashify.rb b/lib/faraday/rashify.rb new file mode 100644 index 0000000..131caeb --- /dev/null +++ b/lib/faraday/rashify.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require_relative 'rashify/middleware' +require 'faraday' + +module Faraday + # This will be your middleware main module, though the actual middleware implementation will go + # into Faraday::Mashify::Middleware for the correct namespacing. + module Rashify + # Faraday allows you to register your middleware for easier configuration. + # This step is totally optional, but it basically allows users to use a + # custom symbol (in this case, `:mashify`), to use your middleware in their connections. + # After calling this line, the following are both valid ways to set the middleware in a connection: + # * conn.use Faraday::Mashify::Middleware + # * conn.use :mashify + # Without this line, only the former method is valid. + # Faraday::Middleware.register_middleware(mashify: Faraday::Mashify::Middleware) + + # Alternatively, you can register your middleware under Faraday::Request or Faraday::Response. + # This will allow to load your middleware using the `request` or `response` methods respectively. + # + # Load middleware with conn.request :mashify + # Faraday::Request.register_middleware(mashify: Faraday::Mashify::Middleware) + # + # Load middleware with conn.response :mashify + Faraday::Response.register_middleware(rashify: Faraday::Rashify::Middleware) + end +end diff --git a/lib/faraday/rashify/middleware.rb b/lib/faraday/rashify/middleware.rb new file mode 100644 index 0000000..fda74bc --- /dev/null +++ b/lib/faraday/rashify/middleware.rb @@ -0,0 +1,58 @@ +require 'faraday' +require 'rash' + +module Faraday + # This is a Faraday middleware which turns Hashes into Hashie::Mash::Rash + # objects, using the rash_alt gem. + module Rashify + # This is a middleware which acts on responses, so it implements `on_complete`. + class Middleware < ::Faraday::Middleware + CONTENT_TYPE = 'Content-Type' + + # Public: Converts parsed response bodies to a Hashie::Mash::Rash if they were of + # Hash or Array type. + def initialize(app = nil, options = {}) + super(app) + @options = options + @content_types = Array(options[:content_type]) + end + + def on_complete(env) + # return early if we should not process this request + return unless should_process?(env) + + env[:body] = parse(env[:body]) + end + + private + + # An empty list of content_types means "match everything". + # A non-empty list is more demanding: match this String or RegExp to process + + def should_process?(env) + @content_types.empty? || @content_types.any? do |pattern| + type = response_type(env) + + pattern.is_a?(Regexp) ? type =~ pattern : type == pattern + end + end + + def response_type(env) + type = env.dig(:response_headers, CONTENT_TYPE).to_s + type = type.split(';', 2).first if type.include?(';') + type + end + + def parse(body) + case body + when Hash + ::Hashie::Mash::Rash.new(body) + when Array + body.map { |item| parse(item) } + else + body + end + end + end + end +end diff --git a/lib/pactas_itero/client.rb b/lib/pactas_itero/client.rb index 22fdd56..0206e95 100644 --- a/lib/pactas_itero/client.rb +++ b/lib/pactas_itero/client.rb @@ -50,7 +50,7 @@ def bearer_token? private def connection - @connection ||= Faraday.new(api_endpoint, connection_options) + @connection ||= Faraday.new({ url: api_endpoint, **connection_options }) end def request(method, path, params = {}) diff --git a/lib/pactas_itero/default.rb b/lib/pactas_itero/default.rb index 40ccb46..228271e 100644 --- a/lib/pactas_itero/default.rb +++ b/lib/pactas_itero/default.rb @@ -1,6 +1,7 @@ require "pactas_itero/response/raise_error" require "pactas_itero/version" -require "faraday_middleware" +require "faraday/rashify" +require "faraday" module PactasItero # Default configuration options for {Client} diff --git a/pactas_itero.gemspec b/pactas_itero.gemspec index 0004db9..84f142e 100644 --- a/pactas_itero.gemspec +++ b/pactas_itero.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.6" - spec.add_dependency("faraday_middleware", ">= 1.0") + spec.add_dependency("faraday", ">= 2.0") spec.add_dependency("rash_alt") spec.add_development_dependency "bundler"