Skip to content

Commit

Permalink
Add support for Faraday 2.x.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Feb 1, 2024
1 parent e446423 commit a3eb885
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 36 deletions.
8 changes: 5 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-02-16 15:23:43 UTC using RuboCop version 1.45.1.
# on 2024-02-01 15:09:00 UTC using RuboCop version 1.48.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -35,16 +35,17 @@ Lint/RedundantSafeNavigation:
Exclude:
- 'lib/hyperclient/link.rb'

# Offense count: 2
# Offense count: 3
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Exclude:
- 'features/steps/api_navigation.rb'
- 'features/steps/default_config.rb'
- 'features/support/env.rb'

# Offense count: 4
# Offense count: 5
# Configuration parameters: AllowedConstants.
Style/Documentation:
Exclude:
Expand All @@ -53,4 +54,5 @@ Style/Documentation:
- 'features/steps/api_navigation.rb'
- 'features/steps/default_config.rb'
- 'features/support/api.rb'
- 'features/support/env.rb'
- 'features/support/fixtures.rb'
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source 'https://rubygems.org'

gem 'faraday', ENV['FARADAY_VERSION'] if ENV['FARADAY_VERSION'] && !ENV['FARADAY_VERSION'].empty?
gem 'faraday_hal_middleware', github: 'fetch/faraday_hal_middleware'

gemspec

Expand All @@ -25,6 +26,7 @@ end
group :test do
gem 'danger-changelog', '~> 0.6.0'
gem 'danger-toc', '~> 0.2.0'
gem 'faraday-follow_redirects'
gem 'minitest'
gem 'mocha'
gem 'rack-test'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Hyperclient constructs a connection using typical [Faraday](http://github.com/lo
```ruby
api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
client.connection do |conn|
conn.use Faraday::Request::OAuth
conn.use Faraday::Request::Instrumentation
end
end
```
Expand All @@ -60,7 +60,7 @@ You can pass options to the Faraday connection block in the `connection` block:
```ruby
api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
client.connection(ssl: { verify: false }) do |conn|
conn.use Faraday::Request::OAuth
conn.use Faraday::Request::Instrumentation
end
end
```
Expand Down
6 changes: 3 additions & 3 deletions features/steps/api_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class Spinach::Features::ApiNavigation < Spinach::FeatureSteps
end

step 'I should be able to access it\'s title and body' do
@post._attributes.title.wont_equal nil
@post._attributes.body.wont_equal nil
assert_kind_of String, @post._attributes.title
assert_kind_of String, @post._attributes.body
end

step 'I should also be able to access it\'s embedded comments' do
comment = @post._embedded.comments.first
comment._attributes.title.wont_equal nil
assert_kind_of String, comment._attributes.title
end

step 'I should be able to navigate to next page' do
Expand Down
4 changes: 2 additions & 2 deletions features/steps/default_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Spinach::Features::DefaultConfig < Spinach::FeatureSteps
end

step 'it should have been parsed as JSON' do
@posts._attributes.total_posts.to_i.must_equal 4
@posts._attributes['total_posts'].to_i.must_equal 4
assert_equal 4, @posts._attributes.total_posts.to_i
assert_equal 4, @posts._attributes['total_posts'].to_i
end
end
15 changes: 14 additions & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
require 'minitest/spec'
require 'minitest'
require 'webmock'
WebMock.enable!
require 'hyperclient'

Minitest::Spec.new nil if defined?(Minitest::Spec)
Spinach.config[:failure_exceptions] << Minitest::Assertion

class Spinach::FeatureSteps
include Minitest::Assertions
attr_accessor :assertions

def initialize(*args)
super(*args)
self.assertions = 0
end
end
5 changes: 2 additions & 3 deletions hyperclient.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Gem::Specification.new do |gem|
gem.version = Hyperclient::VERSION

gem.add_dependency 'addressable'
gem.add_dependency 'faraday', '>= 0.9.0'
gem.add_dependency 'faraday_hal_middleware'
gem.add_dependency 'faraday_middleware'
gem.add_dependency 'faraday', '>= 2.0'
gem.add_dependency 'faraday_hal_middleware', '>= 0.2.0'
gem.metadata['rubygems_mfa_required'] = 'true'
end
6 changes: 3 additions & 3 deletions lib/hyperclient/entry_point.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'faraday_middleware'
require 'faraday/follow_redirects'
require 'faraday_hal_middleware'

module Hyperclient
Expand All @@ -20,7 +20,7 @@ def message
#
# client = Hyperclient::EntryPoint.new('http://my.api.org') do |entry_point|
# entry_point.connection do |conn|
# conn.use Faraday::Request::OAuth
# conn.use Faraday::Request::Instrumentation
# end
# entry_point.headers['Access-Token'] = 'token'
# end
Expand Down Expand Up @@ -138,7 +138,7 @@ def faraday_block=(value)
def default_faraday_block
lambda do |connection, &block|
connection.use Faraday::Response::RaiseError
connection.use FaradayMiddleware::FollowRedirects
connection.use Faraday::FollowRedirects::Middleware
connection.request :hal_json
connection.response :hal_json, content_type: /\bjson$/

Expand Down
26 changes: 13 additions & 13 deletions test/hyperclient/entry_point_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ module Hyperclient

it 'can insert additional middleware after a connection has been constructed' do
_(entry_point.connection).must_be_kind_of Faraday::Connection
entry_point.connection.use :instrumentation
entry_point.connection.use Faraday::Request::Instrumentation
handlers = entry_point.connection.builder.handlers
_(handlers).must_include FaradayMiddleware::Instrumentation
_(handlers).must_include Faraday::Request::Instrumentation
end

it 'creates a Faraday connection with the default block' do
handlers = entry_point.connection.builder.handlers

_(handlers).must_include Faraday::Response::RaiseError
_(handlers).must_include FaradayMiddleware::FollowRedirects
_(handlers).must_include FaradayMiddleware::EncodeHalJson
_(handlers).must_include FaradayMiddleware::ParseHalJson
_(handlers).must_include Faraday::FollowRedirects::Middleware
_(handlers).must_include Faraday::HalJson::Request
_(handlers).must_include Faraday::HalJson::Response

_(entry_point.connection.options.params_encoder).must_equal Faraday::FlatParamsEncoder
end
Expand Down Expand Up @@ -137,9 +137,9 @@ module Hyperclient
it 'creates a Faraday connection with the default block' do
handlers = entry_point.connection.builder.handlers
_(handlers).wont_include Faraday::Response::RaiseError
_(handlers).wont_include FaradayMiddleware::FollowRedirects
_(handlers).must_include FaradayMiddleware::EncodeJson
_(handlers).must_include FaradayMiddleware::ParseJson
_(handlers).wont_include Faraday::FollowRedirects
_(handlers).must_include Faraday::Request::Json
_(handlers).must_include Faraday::Response::Json
end
end
end
Expand All @@ -148,7 +148,7 @@ module Hyperclient
let(:entry_point) do
EntryPoint.new 'http://my.api.org' do |entry_point|
entry_point.connection do |conn|
conn.use Faraday::Request::OAuth
conn.use Faraday::Request::Instrumentation
end
entry_point.headers['Access-Token'] = 'token'
end
Expand All @@ -168,11 +168,11 @@ module Hyperclient
it 'creates a Faraday connection with the default block plus any additional handlers' do
handlers = entry_point.connection.builder.handlers

_(handlers).must_include Faraday::Request::OAuth
_(handlers).must_include Faraday::Request::Instrumentation
_(handlers).must_include Faraday::Response::RaiseError
_(handlers).must_include FaradayMiddleware::FollowRedirects
_(handlers).must_include FaradayMiddleware::EncodeHalJson
_(handlers).must_include FaradayMiddleware::ParseHalJson
_(handlers).must_include Faraday::FollowRedirects::Middleware
_(handlers).must_include Faraday::HalJson::Request
_(handlers).must_include Faraday::HalJson::Response

_(entry_point.connection.options.params_encoder).must_equal Faraday::FlatParamsEncoder
end
Expand Down
10 changes: 5 additions & 5 deletions test/hyperclient_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
let(:client) do
Hyperclient.new('http://api.example.org') do |client|
client.connection(default: true) do |conn|
conn.use Faraday::Request::OAuth
conn.use Faraday::Request::Instrumentation
end
client.headers['Access-Token'] = 'token'
end
Expand All @@ -31,11 +31,11 @@

it 'creates a Faraday connection with the default block plus any additional handlers' do
handlers = client.connection.builder.handlers
_(handlers).must_include Faraday::Request::OAuth
_(handlers).must_include Faraday::Request::Instrumentation
_(handlers).must_include Faraday::Response::RaiseError
_(handlers).must_include FaradayMiddleware::FollowRedirects
_(handlers).must_include FaradayMiddleware::EncodeHalJson
_(handlers).must_include FaradayMiddleware::ParseHalJson
_(handlers).must_include Faraday::FollowRedirects::Middleware
_(handlers).must_include Faraday::HalJson::Request
_(handlers).must_include Faraday::HalJson::Response
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

require 'minitest/autorun'
require 'minitest/pride'
require 'minitest/unit'
require 'mocha/minitest'
require 'json'

MiniTest::Test.class_eval do
require 'faraday'
require 'faraday/request/instrumentation'

MiniTest::Assertions.class_eval do
def stub_request(conn, adapter_class = Faraday::Adapter::Test, &stubs_block)
adapter_handler = conn.builder.handlers.find { |h| h.klass < Faraday::Adapter }
if adapter_handler
Expand Down

0 comments on commit a3eb885

Please sign in to comment.