Skip to content

Commit

Permalink
cleaner code, changelog entries
Browse files Browse the repository at this point in the history
  • Loading branch information
ksol committed Feb 12, 2024
1 parent ce094af commit 63a2605
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
36 changes: 21 additions & 15 deletions lib/scalingo/faraday/extract_meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 63a2605

Please sign in to comment.