Skip to content

Commit

Permalink
Add support for Faraday 2.x
Browse files Browse the repository at this point in the history
They changed the middleware namespace, so we need to support both for
the time being.
  • Loading branch information
Nick Campbell committed Mar 29, 2022
1 parent 709247e commit e4d153e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 7 additions & 1 deletion lib/zaikio/client/helpers/json_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion lib/zaikio/client/helpers/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion zaikio-client-helpers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit e4d153e

Please sign in to comment.