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

Support new Faraday version #135

Merged
merged 4 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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: ...} }
Expand Down
16 changes: 16 additions & 0 deletions lib/spyke.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/spyke/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/spyke/http.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'faraday'
require 'faraday_middleware'
require 'spyke/config'
require 'spyke/path'
require 'spyke/result'
Expand Down
4 changes: 2 additions & 2 deletions spyke.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion test/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
11 changes: 6 additions & 5 deletions test/support/fixtures.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down