Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update faraday to 2.12, remove deprecated faraday_middleware gem #393

Merged
merged 9 commits into from
Feb 11, 2025
44 changes: 14 additions & 30 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PATH
activemodel (>= 6.0.0)
activesupport (>= 6.0.0)
faraday (>= 1.8, < 3.0)
faraday_middleware (~> 1.2)
faraday-retry (~> 2.2)
oauth2 (>= 2.0.2, < 3)
openssl (>= 2.2.0)

Expand Down Expand Up @@ -47,32 +47,15 @@ GEM
diff-lcs (1.5.1)
docile (1.4.0)
drb (2.2.1)
faraday (1.10.4)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
faraday-httpclient (~> 1.0)
faraday-multipart (~> 1.0)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.0)
faraday-patron (~> 1.0)
faraday-rack (~> 1.0)
faraday-retry (~> 1.0)
ruby2_keywords (>= 0.0.4)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (1.0.2)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
faraday_middleware (1.2.0)
faraday (~> 1.0)
ffi (1.15.5)
faraday (2.12.2)
faraday-net_http (>= 2.0, < 3.5)
json
logger
faraday-net_http (3.4.0)
net-http (>= 0.5.0)
faraday-retry (2.2.1)
faraday (~> 2.0)
ffi (1.17.1)
formatador (1.1.0)
guard (2.18.0)
formatador (>= 0.2.4)
Expand Down Expand Up @@ -115,9 +98,10 @@ GEM
mini_mime (1.1.5)
minitest (5.25.4)
multi_xml (0.6.0)
multipart-post (2.4.1)
mutex_m (0.3.0)
nenv (0.3.0)
net-http (0.6.0)
uri
net-imap (0.4.19)
date
net-protocol
Expand Down Expand Up @@ -195,8 +179,7 @@ GEM
rubocop-rspec (3.4.0)
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
securerandom (0.3.2)
securerandom (0.4.1)
shellany (0.0.1)
simplecov (0.22.0)
docile (~> 1.1)
Expand All @@ -215,6 +198,7 @@ GEM
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
uri (1.0.2)
vcr (6.3.1)
base64
version_gem (1.1.3)
Expand Down
2 changes: 1 addition & 1 deletion ioki-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'activemodel', '>= 6.0.0'
spec.add_dependency 'activesupport', '>= 6.0.0'
spec.add_dependency 'faraday', '>= 1.8', '< 3.0'
spec.add_dependency 'faraday_middleware', '~> 1.2'
spec.add_dependency 'faraday-retry', '~> 2.2'
spec.add_dependency 'oauth2', '>= 2.0.2', '< 3'
spec.add_dependency 'openssl', '>= 2.2.0'
end
2 changes: 1 addition & 1 deletion lib/ioki/apis/endpoints/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def send_request(client, args, options)
)

[
parsed_response['data'].map do |attr|
Support.blank?(parsed_response) ? [] : parsed_response['data'].map do |attr|
model_class
.new(attr, nil, show_deprecation_warnings: false)
.tap(&:clear_changes_information)
Expand Down
2 changes: 1 addition & 1 deletion lib/ioki/http_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'faraday'
require 'faraday_middleware'
require 'faraday/retry'

module Ioki
class HttpAdapter
Expand Down
146 changes: 130 additions & 16 deletions spec/ioki/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

require 'spec_helper'

class NullApi
ENDPOINTS = [].freeze
end

RSpec.describe Ioki::Client do
let(:config) { Ioki::Configuration.new http_adapter: http_adapter }
let(:client) { described_class.new(config, NullApi) }
Expand All @@ -18,6 +14,20 @@ class NullApi
end
let(:stubs) { Faraday::Adapter::Test::Stubs.new }

before do
stub_const('DummyModel', Class.new(Ioki::Model::Base) do
attribute :id, type: :string, on: :read
end)
stub_const('NullApi', Class.new)
stub_const('NullApi::ENDPOINTS', [
Ioki::Endpoints.crud_endpoints(
:vehicle,
base_path: ['driver'],
model_class: DummyModel
)
].freeze)
end

describe 'constants' do
it { expect(described_class::VALID_API_NAMESPACES).to eq([:driver, :operator, :passenger, :platform]) }
end
Expand Down Expand Up @@ -197,18 +207,8 @@ class NullApi
[200, {}, '']
end

it 'returns nil' do
expect(client_response[0]).to be_nil
end
end

context 'when response body is somewhat malformed' do
let(:response) do
[200, {}, '{ look"mum" & $ : borken_json ']
end

it 'raise a proper error' do
expect { client_response }.to raise_error(Faraday::ParsingError)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error can not be raised in this spec anymore. We are stubbing the response which Faraday returns after the middleware is applied. So no JSON parsing takes place anymore.

it 'returns empty string' do
expect(client_response[0]).to be_empty
end
end
end
Expand Down Expand Up @@ -253,4 +253,118 @@ class NullApi
end
end
end

context 'with endpoints' do
let(:client) { described_class.new(Ioki::Configuration.new, NullApi) }

context 'with invalid JSON response' do
let!(:vehicles_request) do
stub_request(:get, 'https://app.io.ki/api/driver/vehicles')
.to_return(
status: 200,
body: '{ look"mum" & $ : borken_json ',
headers: { content_type: 'application/json' }
)
end

it 'raises a parsing error' do
expect do
client.vehicles
end.to raise_error(Faraday::ParsingError)
end
end

context 'with empty response' do
let!(:get_vehicles_request) do
stub_request(:get, 'https://app.io.ki/api/driver/vehicles')
.to_return_json(
status: 200,
body: {}
)
end
let!(:get_vehicle_request) do
stub_request(:get, 'https://app.io.ki/api/driver/vehicles/1')
.to_return_json(
status: 200,
body: {}
)
end
let!(:create_vehicle_request) do
stub_request(:post, 'https://app.io.ki/api/driver/vehicles')
.to_return_json(
status: 200,
body: {}
)
end
let!(:update_vehicle_request) do
stub_request(:patch, 'https://app.io.ki/api/driver/vehicles/1')
.to_return_json(
status: 200,
body: {}
)
end
let!(:delete_vehicle_request) do
stub_request(:delete, 'https://app.io.ki/api/driver/vehicles/1')
.to_return_json(
status: 200,
body: {}
)
end

it 'returns empty models for index requests' do
expect(client.vehicles).to eq []
expect(get_vehicles_request).to have_been_requested.once
end

it 'returns empty models for get requests' do
expect(client.vehicle(DummyModel.new(id: 1))).to be_an_instance_of(DummyModel)
expect(get_vehicle_request).to have_been_requested.once
end

it 'returns empty models for create requests' do
expect(client.create_vehicle(DummyModel.new(id: 1))).to be_an_instance_of(DummyModel)
expect(create_vehicle_request).to have_been_requested.once
end

it 'returns empty models for update requests' do
expect(client.update_vehicle(DummyModel.new(id: 1))).to be_an_instance_of(DummyModel)
expect(update_vehicle_request).to have_been_requested.once
end

it 'returns empty models for delete requests' do
expect(client.delete_vehicle(DummyModel.new(id: 1))).to be_an_instance_of(DummyModel)
expect(delete_vehicle_request).to have_been_requested.once
end
end

context 'with an empty string response' do
let!(:update_vehicle_request) do
stub_request(:patch, 'https://app.io.ki/api/driver/vehicles/1')
.to_return_json(
status: 200,
body: ''
)
end

it 'returns nil for update requests' do
expect(client.update_vehicle(DummyModel.new(id: 1))).to be_nil
expect(update_vehicle_request).to have_been_requested.once
end
end

context 'with a nil response' do
let!(:update_vehicle_request) do
stub_request(:patch, 'https://app.io.ki/api/driver/vehicles/1')
.to_return_json(
status: 200,
body: nil
)
end

it 'returns nil for update requests' do
expect(client.update_vehicle(DummyModel.new(id: 1))).to be_nil
expect(update_vehicle_request).to have_been_requested.once
end
end
end
end
Loading