From e4d153efa356754da32619ad2d4f24a51baf681b Mon Sep 17 00:00:00 2001 From: Nick Campbell Date: Tue, 29 Mar 2022 14:17:14 +0100 Subject: [PATCH] Add support for Faraday 2.x They changed the middleware namespace, so we need to support both for the time being. --- CHANGELOG.md | 4 ++++ lib/zaikio/client/helpers/json_parser.rb | 8 +++++++- lib/zaikio/client/helpers/pagination.rb | 8 +++++++- zaikio-client-helpers.gemspec | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09dc65a..6d8567a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## [Unreleased] +## [0.2.4] - 2022-03-29 + +- Add support for Faraday 2.x + ## [0.2.3] - 2021-11-12 - Handle "uncountable" responses (i.e. those without a Total-Count or Total-Pages header) diff --git a/lib/zaikio/client/helpers/json_parser.rb b/lib/zaikio/client/helpers/json_parser.rb index 488ceda..1a063e5 100644 --- a/lib/zaikio/client/helpers/json_parser.rb +++ b/lib/zaikio/client/helpers/json_parser.rb @@ -2,7 +2,13 @@ require "multi_json" module Zaikio::Client::Helpers - class JSONParser < Faraday::Response::Middleware + superclass = if Gem.loaded_specs["faraday"].version >= Gem::Version.new("2.0") + Faraday::Middleware + else + Faraday::Response::Middleware + end + + JSONParser = Class.new(superclass) do def on_complete(env) case env.status when 404 then raise Spyke::ResourceNotFound.new(nil, url: env.url) diff --git a/lib/zaikio/client/helpers/pagination.rb b/lib/zaikio/client/helpers/pagination.rb index 245be0f..88a2ef5 100644 --- a/lib/zaikio/client/helpers/pagination.rb +++ b/lib/zaikio/client/helpers/pagination.rb @@ -11,6 +11,12 @@ module Pagination METADATA_KEY = :pagination + superclass = if Gem.loaded_specs["faraday"].version >= Gem::Version.new("2.0") + Faraday::Middleware + else + Faraday::Response::Middleware + end + # Faraday Middleware for extracting any pagination headers into the a top-level # :metadata hash, or the env hash for non-JSON responses. # @@ -22,7 +28,7 @@ module Pagination # response = conn.get("/") # response.env[METADATA_KEY] # #=> {total_count: 4, total_pages: 1, current_page: 1} - class FaradayMiddleware < Faraday::Response::Middleware + FaradayMiddleware = Class.new(superclass) do def on_complete(env) @env = env diff --git a/zaikio-client-helpers.gemspec b/zaikio-client-helpers.gemspec index f265887..9a12426 100644 --- a/zaikio-client-helpers.gemspec +++ b/zaikio-client-helpers.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| "CHANGELOG.md"] spec.require_paths = ["lib"] - spec.add_dependency "faraday" + spec.add_dependency "faraday", ">= 1", "< 3" spec.add_dependency "multi_json" spec.add_dependency "spyke"