From 63a26052e5a8982e0d84f4fbbf7c91b7c0c3f9e9 Mon Sep 17 00:00:00 2001 From: Kevin Soltysiak Date: Mon, 12 Feb 2024 10:16:24 +0100 Subject: [PATCH] cleaner code, changelog entries --- CHANGELOG.md | 3 +++ lib/scalingo/faraday/extract_meta.rb | 36 ++++++++++++++++------------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 406154e..d236569 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## Unreleased +** Breaking change: automatic digging of the value if the reponse body is an object with a single key +** Breaking change: remove `Scalingo::API::Reponse` in favor of `Faraday::Response` + ## 3.5.0 - 2023-12-28 * Change: update Faraday to 2.x, released about two years ago. The public API of this gem doesn't change, therefore this is not a major release. However, if you manipulate directly faraday's objects, you may encounter breaking changes. Refer to [Faraday's website](https://lostisland.github.io/faraday/) for how to migrate. diff --git a/lib/scalingo/faraday/extract_meta.rb b/lib/scalingo/faraday/extract_meta.rb index be0304f..51b73ff 100644 --- a/lib/scalingo/faraday/extract_meta.rb +++ b/lib/scalingo/faraday/extract_meta.rb @@ -3,23 +3,29 @@ module Scalingo class ExtractMeta < Faraday::Middleware def on_complete(env) - # Only hash-like objects are relevant return unless env.body.is_a?(Hash) - # Extract meta from response - if env.body[:meta] - env[:response_meta] = env.body.delete(:meta) - end - - # Extract cursor-based pagination - if env.body.key?(:next_cursor) - env[:response_meta] = { - cursor_pagination: { - next_cursor: env.body.delete(:next_cursor), - has_more: env.body.delete(:has_more) - }.compact - } - end + extract_metadata(env) + extract_cursor_metadata(env) + end + + private + + def extract_metadata(env) + return unless env.body[:meta] + + env[:response_meta] = env.body.delete(:meta) + end + + def extract_cursor_metadata(env) + return unless env.body.key?(:next_cursor) + + env[:response_meta] = { + cursor_pagination: { + next_cursor: env.body.delete(:next_cursor), + has_more: env.body.delete(:has_more) + }.compact + } end end end