-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(farady): split up middlewares
- Loading branch information
Showing
6 changed files
with
180 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "faraday" | ||
|
||
module Scalingo | ||
class ExtractRootValue < Faraday::Middleware | ||
def on_complete(env) | ||
# We only want to unpack the response for successful responses | ||
return unless env.response.success? | ||
|
||
# Only hash-like objects are relevant to "unpack" | ||
return unless env.body.is_a?(Hash) | ||
|
||
# Dig the root key if it's the only remaining key in the body | ||
env.body = env.body.values.first if env.body.size == 1 | ||
end | ||
end | ||
end | ||
|
||
Faraday::Response.register_middleware(extract_root_value: Scalingo::ExtractRootValue) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
require "spec_helper" | ||
require "scalingo/faraday/extract_meta" | ||
|
||
RSpec.shared_examples "no meta extraction" do | ||
it "does not extract meta from response" do | ||
expect(subject.meta).to eq(nil) | ||
expect(subject.meta?).to eq(false) | ||
expect(subject.pagination).to eq(nil) | ||
expect(subject.paginated?).to eq(false) | ||
expect(subject.cursor_pagination).to eq(nil) | ||
expect(subject.cursor_paginated?).to eq(false) | ||
end | ||
end | ||
|
||
RSpec.describe Scalingo::ExtractMeta do | ||
let(:headers) { {"Content-Type" => content_type}.compact } | ||
let(:content_type) { "application/json" } | ||
let(:body) { nil } | ||
|
||
let(:client) do | ||
Faraday.new do |b| | ||
b.response :extract_meta | ||
b.response :json, content_type: /\bjson$/, parser_options: {symbolize_names: true} | ||
b.adapter :test do |stub| | ||
stub.get("/") { [nil, headers, body] } | ||
end | ||
end | ||
end | ||
|
||
subject { client.get("/") } | ||
|
||
context "with a non-json response" do | ||
let(:content_type) { "text/html" } | ||
let(:body) { "string".to_json } | ||
|
||
include_examples "no meta extraction" | ||
end | ||
|
||
context "without a body" do | ||
include_examples "no meta extraction" | ||
end | ||
|
||
context "with a non indexable body" do | ||
let(:body) { "string".to_json } | ||
|
||
include_examples "no meta extraction" | ||
end | ||
|
||
context "with a non hash indexable body" do | ||
let(:body) { [1, 2, 3].to_json } | ||
|
||
include_examples "no meta extraction" | ||
end | ||
|
||
context "with a hash" do | ||
context "without a meta key" do | ||
let(:body) { {a: 1, b: 2}.to_json } | ||
|
||
include_examples "no meta extraction" | ||
end | ||
|
||
context "with a generic meta key" do | ||
let(:body) { {meta: {a: 1}}.to_json } | ||
|
||
it "extracts the meta" do | ||
expect(subject.meta).to eq({a: 1}) | ||
expect(subject.meta?).to eq(true) | ||
expect(subject.pagination).to eq(nil) | ||
expect(subject.paginated?).to eq(false) | ||
expect(subject.cursor_pagination).to eq(nil) | ||
expect(subject.cursor_paginated?).to eq(false) | ||
end | ||
end | ||
|
||
context "with a pagination meta key" do | ||
let(:body) { {meta: {pagination: {page: 1}}}.to_json } | ||
|
||
it "extracts the meta" do | ||
expect(subject.meta).to eq({pagination: {page: 1}}) | ||
expect(subject.meta?).to eq(true) | ||
expect(subject.pagination).to eq({page: 1}) | ||
expect(subject.paginated?).to eq(true) | ||
expect(subject.cursor_pagination).to eq(nil) | ||
expect(subject.cursor_paginated?).to eq(false) | ||
end | ||
end | ||
|
||
context "with a cursor pagination" do | ||
let(:body) { {next_cursor: 1, has_more: true}.to_json } | ||
|
||
it "extracts the meta" do | ||
expect(subject.meta).to eq({cursor_pagination: {next_cursor: 1, has_more: true}}) | ||
expect(subject.meta?).to eq(true) | ||
expect(subject.pagination).to eq(nil) | ||
expect(subject.paginated?).to eq(false) | ||
expect(subject.cursor_pagination).to eq({next_cursor: 1, has_more: true}) | ||
expect(subject.cursor_paginated?).to eq(true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require "spec_helper" | ||
require "scalingo/faraday/extract_root_value" | ||
|
||
RSpec.describe Scalingo::ExtractRootValue do | ||
let(:status) { 200 } | ||
let(:body) { {a: 1} } | ||
|
||
let(:client) do | ||
Faraday.new do |b| | ||
b.response :extract_root_value | ||
b.adapter :test do |stub| | ||
stub.get("/") { [status, {}, body] } | ||
end | ||
end | ||
end | ||
|
||
subject { client.get("/") } | ||
|
||
context "with a non-successful response" do | ||
let(:status) { 300 } | ||
|
||
it "does not change the response body" do | ||
expect(subject.body).to eq({a: 1}) | ||
end | ||
end | ||
|
||
context "with a non-hash response" do | ||
let(:body) { [{a: 1}] } | ||
|
||
it "does not change the response body" do | ||
expect(subject.body).to eq([{a: 1}]) | ||
end | ||
end | ||
|
||
context "with a multi keys hash" do | ||
let(:body) { {a: 1, b: 2} } | ||
|
||
it "does not change the response body" do | ||
expect(subject.body).to eq({a: 1, b: 2}) | ||
end | ||
end | ||
|
||
context "with a single key hash" do | ||
it "extracts the response body" do | ||
expect(subject.body).to eq(1) | ||
end | ||
end | ||
end |