diff --git a/.circleci/config.yml b/.circleci/config.yml index 44cd2b1..575586f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -18,11 +18,14 @@ jobs: type: string rails_version: type: string + faraday_version: + type: string docker: - image: cimg/ruby:<< parameters.ruby_version >> executor: ruby/default environment: RAILS_TEST_VERSION: << parameters.rails_version >> + FARADAY_TEST_VERSION: << parameters.faraday_version >> EUID: 0 # not sure why this is needed, but sudo npm fails steps: - checkout @@ -56,23 +59,27 @@ workflows: parameters: rails_version: ["~> 4.0"] ruby_version: ["2.6.9"] + faraday_version: ["< 2.0"] - build: matrix: alias: Rails 5 parameters: rails_version: ["~> 5.0"] ruby_version: ["2.6.9", "2.7.5"] + faraday_version: ["< 2.0", ">= 2.0"] - build: matrix: alias: Rails 6 parameters: rails_version: ["~> 6.0"] ruby_version: ["2.6.9", "2.7.5", "3.0.3"] + faraday_version: ["< 2.0", ">= 2.0"] - build: matrix: alias: Rails 7 parameters: rails_version: ["~> 7.0"] ruby_version: ["2.7.5", "3.0.3", "3.1.1"] + faraday_version: ["< 2.0", ">= 2.0"] - coverage: requires: ["Rails 4", "Rails 5", "Rails 6", "Rails 7"] diff --git a/Gemfile b/Gemfile index 6dd23a6..3d13184 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,7 @@ source 'https://rubygems.org' # Specify your gem's dependencies in spyke.gemspec gemspec + +if ENV['FARADAY_TEST_VERSION'] == '< 2.0' + gem 'faraday_middleware' +end diff --git a/README.md b/README.md index 3b2cd70..3d2dbb1 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,10 @@ So, for example for an API that returns JSON like this: ```ruby # config/initializers/spyke.rb -class JSONParser < Faraday::Response::Middleware - def parse(body) - json = MultiJson.load(body, symbolize_keys: true) - { +class JSONParser < Faraday::Middleware + def on_complete(env) + json = MultiJson.load(env.body, symbolize_keys: true) + env.body = { data: json[:result], metadata: json[:extra], errors: json[:errors] @@ -60,7 +60,8 @@ class JSONParser < Faraday::Response::Middleware end Spyke::Base.connection = Faraday.new(url: 'http://api.com') do |c| - c.request :json + c.request :multipart + c.request :json # if using Faraday 1.x, please add `faraday_middleware` to your dependencies first c.use JSONParser c.adapter Faraday.default_adapter end @@ -202,7 +203,7 @@ but this can be disabled or customized: ```ruby class Article < Spyke::Base # Default - include_root_in_json true # { article: { title: ...} } + include_root_in_json true # { article: { title: ...} } # Custom include_root_in_json :post # { post: { title: ...} } diff --git a/lib/spyke.rb b/lib/spyke.rb index 92962f1..351dd83 100644 --- a/lib/spyke.rb +++ b/lib/spyke.rb @@ -1,5 +1,21 @@ require 'active_support' require 'active_support/core_ext' + +require 'faraday' +require 'faraday/multipart' + +if Gem.loaded_specs["faraday"].version < Gem::Version.new("2.0") + begin + require 'faraday_middleware' + rescue LoadError => e + puts <<~MSG + Please add `faraday_middleware` to your Gemfile when using Faraday 1.x. Alternatively, + upgrade to Faraday `~> 2` to avoid this dependency. + MSG + raise e + end +end + require 'spyke/base' require 'spyke/instrumentation' if defined?(Rails) require 'spyke/version' diff --git a/lib/spyke/attributes.rb b/lib/spyke/attributes.rb index 629e311..e6e55db 100644 --- a/lib/spyke/attributes.rb +++ b/lib/spyke/attributes.rb @@ -12,7 +12,7 @@ def parse_value(value) case when value.is_a?(Spyke::Base) then value.attributes.to_params when value.is_a?(Array) then value.map { |v| parse_value(v) } - when value.respond_to?(:content_type) then Faraday::UploadIO.new(value.path, value.content_type) + when value.respond_to?(:content_type) then Faraday::Multipart::FilePart.new(value.path, value.content_type) else value end end diff --git a/lib/spyke/http.rb b/lib/spyke/http.rb index 0175150..c3ff901 100644 --- a/lib/spyke/http.rb +++ b/lib/spyke/http.rb @@ -1,5 +1,3 @@ -require 'faraday' -require 'faraday_middleware' require 'spyke/config' require 'spyke/path' require 'spyke/result' diff --git a/lib/spyke/version.rb b/lib/spyke/version.rb index 43edc87..419bede 100644 --- a/lib/spyke/version.rb +++ b/lib/spyke/version.rb @@ -1,3 +1,3 @@ module Spyke - VERSION = '6.1.3' + VERSION = '7.0.0' end diff --git a/spyke.gemspec b/spyke.gemspec index 93ea1c6..5edfaa3 100644 --- a/spyke.gemspec +++ b/spyke.gemspec @@ -20,8 +20,8 @@ Gem::Specification.new do |spec| spec.add_dependency 'activesupport', ENV.fetch('RAILS_TEST_VERSION', '>= 4.0.0') spec.add_dependency 'activemodel', ENV.fetch('RAILS_TEST_VERSION', '>= 4.0.0') - spec.add_dependency 'faraday', '>= 0.9.0', '< 2.0' - spec.add_dependency 'faraday_middleware', '>= 0.9.1', '< 2.0' + spec.add_dependency 'faraday', ENV.fetch('FARADAY_TEST_VERSION', '>= 1.0.0'), '< 3.0' + spec.add_dependency 'faraday-multipart', '~> 1.0' spec.add_dependency 'addressable', '>= 2.5.2' spec.add_development_dependency 'actionpack', ENV.fetch('RAILS_TEST_VERSION', '>= 4.0.0') diff --git a/test/attributes_test.rb b/test/attributes_test.rb index 2e12e30..81e6c91 100644 --- a/test/attributes_test.rb +++ b/test/attributes_test.rb @@ -134,7 +134,7 @@ def test_inheriting_explicit_attributes end def test_converting_files_to_faraday_io - Faraday::UploadIO.stubs(:new).with('/photo.jpg', 'image/jpeg').returns('UploadIO') + Faraday::Multipart::FilePart.stubs(:new).with('/photo.jpg', 'image/jpeg').returns('UploadIO') file = mock file.stubs(:path).returns('/photo.jpg') file.stubs(:content_type).returns('image/jpeg') diff --git a/test/support/fixtures.rb b/test/support/fixtures.rb index 2d3c357..55a5a2d 100644 --- a/test/support/fixtures.rb +++ b/test/support/fixtures.rb @@ -1,20 +1,21 @@ require 'multi_json' # Dummy api -class JSONParser < Faraday::Response::Middleware - def parse(body) - json = MultiJson.load(body, symbolize_keys: true) - { +class JSONParser < Faraday::Middleware + def on_complete(env) + json = MultiJson.load(env.body, symbolize_keys: true) + env.body = { data: json[:result], metadata: json[:metadata], errors: json[:errors] } rescue MultiJson::ParseError => exception - { errors: { base: [ error: exception.message ] } } + env.body = { errors: { base: [ error: exception.message ] } } end end Spyke::Base.connection = Faraday.new(url: 'http://sushi.com') do |faraday| + faraday.request :multipart faraday.request :json faraday.use JSONParser faraday.adapter Faraday.default_adapter