diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c12f650..74400645 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: strategy: fail-fast: false matrix: - ruby: ["2.5", "2.6", "2.7", "3.0", "3.1", ruby-head] + ruby: ["2.6", "2.7", "3.0", "3.1", ruby-head] steps: - uses: actions/checkout@v2 diff --git a/.travis.yml b/.travis.yml index fe23121a..4b34aece 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,11 @@ language: ruby rvm: - - 2.0.0 - - 2.1.0 - - 2.2.0 - - 2.3.0 - - 2.4.0 - 2.5.0 - 2.6.0 - 2.7.0 + - 3.0.0 + - 3.1.0 + # Bundler 1.13 added support for `ruby RUBY_VERSION` in the Gemfile before_install: gem install bundler -v '1.17.3' diff --git a/HISTORY.md b/HISTORY.md index 7118600f..fc8411a5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +## Next +* Use Faraday 2.0 gem +* Replace custom Faraday GZip implementation with `faraday-gzip` gem +* Relax gemspec to allow OAuth2 v2.x gem +* No longer support Ruby 2.5 + ## 1.0.21 (2022-04-26) * Add ability to download PDF of a Payment. (#577). Thanks @anaulin diff --git a/lib/quickbooks-ruby.rb b/lib/quickbooks-ruby.rb index 19956d9b..3a6079f4 100644 --- a/lib/quickbooks-ruby.rb +++ b/lib/quickbooks-ruby.rb @@ -8,12 +8,13 @@ require 'forwardable' require 'oauth2' require 'net/http/post/multipart' +require 'faraday/multipart' +require 'faraday/gzip' require 'quickbooks/util/collection' require 'quickbooks/util/logging' require 'quickbooks/util/http_encoding_helper' require 'quickbooks/util/name_entity' require 'quickbooks/util/query_builder' -require 'quickbooks/faraday/middleware/gzip' #== OAuth Responses require 'quickbooks/service/responses/oauth_http_response' @@ -187,9 +188,6 @@ require 'quickbooks/service/change_data_capture' require 'quickbooks/service/refund_receipt_change' -# Register Faraday Middleware -Faraday::Middleware.register_middleware :gzip => lambda { Gzip } - module Quickbooks @@sandbox_mode = false @@logger = nil diff --git a/lib/quickbooks/faraday/middleware/gzip.rb b/lib/quickbooks/faraday/middleware/gzip.rb deleted file mode 100644 index cfe2cd8f..00000000 --- a/lib/quickbooks/faraday/middleware/gzip.rb +++ /dev/null @@ -1,74 +0,0 @@ -# https://github.com/lostisland/faraday_middleware/blob/master/lib/faraday_middleware/gzip.rb - -require 'faraday' - -# Middleware to automatically decompress response bodies. If the -# "Accept-Encoding" header wasn't set in the request, this sets it to -# "gzip,deflate" and appropriately handles the compressed response from the -# server. This resembles what Ruby 1.9+ does internally in Net::HTTP#get. -# -# This middleware is NOT necessary when these adapters are used: -# - net_http on Ruby 1.9+ -# - net_http_persistent on Ruby 2.0+ -# - em_http -class Gzip < Faraday::Middleware - dependency 'zlib' - - ACCEPT_ENCODING = 'Accept-Encoding'.freeze - CONTENT_ENCODING = 'Content-Encoding'.freeze - CONTENT_LENGTH = 'Content-Length'.freeze - SUPPORTED_ENCODINGS = 'gzip,deflate,br'.freeze - RUBY_ENCODING = '1.9'.respond_to?(:force_encoding) - - def call(env) - env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS - @app.call(env).on_complete do |response_env| - break if response_env[:response_headers].nil? - - case response_env[:response_headers][CONTENT_ENCODING] - when 'gzip' - reset_body(response_env, &method(:uncompress_gzip)) - when 'deflate' - reset_body(response_env, &method(:inflate)) - when 'br' - reset_body(response_env, &method(:brotli_inflate)) - end - end - end - - def reset_body(env) - env[:body] = yield(env[:body]) - env[:response_headers].delete(CONTENT_ENCODING) - env[:response_headers][CONTENT_LENGTH] = env[:body].length - end - - def uncompress_gzip(body) - io = StringIO.new(body) - gzip_reader = if RUBY_ENCODING - Zlib::GzipReader.new(io, :encoding => 'ASCII-8BIT') - else - Zlib::GzipReader.new(io) - end - gzip_reader.read - end - - def inflate(body) - # Inflate as a DEFLATE (RFC 1950+RFC 1951) stream - Zlib::Inflate.inflate(body) - rescue Zlib::DataError - # Fall back to inflating as a "raw" deflate stream which - # Microsoft servers return - inflate = Zlib::Inflate.new(-Zlib::MAX_WBITS) - begin - inflate.inflate(body) - ensure - inflate.close - end - end - - def brotli_inflate(body) - self.class.dependency 'brotli' - - Brotli.inflate(body) - end -end diff --git a/lib/quickbooks/service/access_token.rb b/lib/quickbooks/service/access_token.rb index 2d6dafb8..4038a39c 100644 --- a/lib/quickbooks/service/access_token.rb +++ b/lib/quickbooks/service/access_token.rb @@ -22,7 +22,7 @@ def renew def disconnect connection = Faraday.new(headers: { 'Content-Type' => 'application/json' }) do |f| f.adapter(::Quickbooks.http_adapter) - f.basic_auth(oauth.client.id, oauth.client.secret) + f.request(:authorization, :basic, oauth.client.id, oauth.client.secret) end url = "#{DISCONNECT_URL}?minorversion=#{Quickbooks.minorversion}" diff --git a/lib/quickbooks/service/base_service.rb b/lib/quickbooks/service/base_service.rb index 80834778..b55b1728 100644 --- a/lib/quickbooks/service/base_service.rb +++ b/lib/quickbooks/service/base_service.rb @@ -49,12 +49,11 @@ def realm_id=(company_id) # [OAuth2] The default Faraday connection does not have gzip or multipart support. # We need to reset the existing connection and build a new one. def rebuild_connection! - @oauth.client.connection = nil - @oauth.client.connection.build do |builder| - builder.use :gzip - builder.request :multipart - builder.request :url_encoded - builder.adapter ::Quickbooks.http_adapter + @oauth.client.connection = Faraday.new do |f| + f.request :multipart + f.request :gzip + f.request :url_encoded + f.adapter ::Quickbooks.http_adapter end end diff --git a/quickbooks-ruby.gemspec b/quickbooks-ruby.gemspec index 248f723e..b86ca61b 100644 --- a/quickbooks-ruby.gemspec +++ b/quickbooks-ruby.gemspec @@ -14,13 +14,17 @@ Gem::Specification.new do |gem| gem.files = Dir['lib/**/*'] - gem.add_dependency 'oauth2', '~>1.4' + gem.required_ruby_version = '>= 2.6' + + gem.add_dependency 'oauth2', '< 3.0' gem.add_dependency 'roxml', '~> 4.2' gem.add_dependency 'activemodel', '> 4.0' gem.add_dependency 'net-http-persistent' gem.add_dependency 'nokogiri' # promiscuous mode gem.add_dependency 'multipart-post' # promiscuous mode - gem.add_dependency 'faraday', '< 2.0' + gem.add_dependency 'faraday', '< 3.0' + gem.add_dependency 'faraday-multipart', '~> 1.0' + gem.add_dependency 'faraday-gzip', '~> 0.1' gem.add_development_dependency 'rake' gem.add_development_dependency 'simplecov' diff --git a/spec/lib/quickbooks/model/account_spec.rb b/spec/lib/quickbooks/model/account_spec.rb index b0b51331..9ee93d92 100644 --- a/spec/lib/quickbooks/model/account_spec.rb +++ b/spec/lib/quickbooks/model/account_spec.rb @@ -18,7 +18,6 @@ account.name = "Regular" account.valid? - pp account.errors expect(account.errors.map(&:attribute).include?(:name)).to be false end