diff --git a/.circleci/config.yml b/.circleci/config.yml index 174a737..f0c10ef 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,12 +7,6 @@ orbs: defaults: &defaults notify_failure: false -ruby_2_7_defaults: &ruby_2_7_defaults - <<: *defaults - e: - name: ruby/ruby - ruby-version: '2.7' - ruby_3_0_defaults: &ruby_3_0_defaults <<: *defaults e: @@ -31,21 +25,14 @@ ruby_3_2_defaults: &ruby_3_2_defaults name: ruby/ruby ruby-version: '3.2' +ruby_3_4_defaults: &ruby_3_4_defaults + <<: *defaults + e: + name: ruby/ruby + ruby-version: '3.4' + workflows: version: 2 - ruby_2_7: - jobs: - - ruby/bundle-audit: - <<: *ruby_2_7_defaults - name: ruby-2_7-bundle_audit - - ruby/rubocop: - <<: *ruby_2_7_defaults - name: ruby-2_7-rubocop - - ruby/rspec-unit: - <<: *ruby_2_7_defaults - name: ruby-2_7-rspec_unit - db: false - code-climate: false ruby_3_0: jobs: - ruby/bundle-audit: @@ -84,3 +71,16 @@ workflows: name: ruby-3_2-rspec_unit db: false code-climate: false + ruby_3_4: + jobs: + - ruby/bundle-audit: + <<: *ruby_3_4_defaults + name: ruby-3_4-bundle_audit + - ruby/rubocop: + <<: *ruby_3_4_defaults + name: ruby-3_4-rubocop + - ruby/rspec-unit: + <<: *ruby_3_4_defaults + name: ruby-3_4-rspec_unit + db: false + code-climate: false diff --git a/CHANGELOG.md b/CHANGELOG.md index 72a9984..be9712c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ Your contribution here. * [#000](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/000): Brief description here. - [@username](https://github.com/username). +## 2.0.0 + +* [#186](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/186): Update to Faraday 2.14 and add support for Ruby 3.4 - [@markcmurphy](https://github.com/markcmurphy). + ## 1.1.0 * [#174](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/174): Drop support for ruby 2.6; Add support for Ruby 3.2 - [@j05h](https://github.com/j05h). diff --git a/DEPENDENCIES.md b/DEPENDENCIES.md index c0cdf75..74a0107 100644 --- a/DEPENDENCIES.md +++ b/DEPENDENCIES.md @@ -3,6 +3,6 @@ Many thanks to the contributors and authors of the following libraries! - [Faraday](https://github.com/lostisland/faraday) Simple, but flexible HTTP client library, with support for multiple backends. - [MIT](https://github.com/lostisland/faraday/blob/master/LICENSE.md) -- [Faraday Middleware](https://github.com/lostisland/faraday_middleware) Various Faraday middlewares for Faraday-based API wrappers. - [MIT](https://github.com/lostisland/faraday_middleware/blob/master/LICENSE.md) +- [Faraday Gzip](https://github.com/bodrovis/faraday-gzip) Gzip request and response compression middleware for Faraday. - [MIT](https://github.com/bodrovis/faraday-gzip/blob/main/LICENSE.txt) - [Hashie](https://github.com/intridea/hashie) Hashie is a collection of classes and mixins that make hashes more powerful. - [MIT](https://github.com/intridea/hashie/blob/master/LICENSE) - [JWT](https://github.com/jwt/ruby-jwt) Used to encode and sign JWT tokens for the Customer Login API. - [MIT](https://github.com/jwt/ruby-jwt/blob/master/LICENSE) diff --git a/bigcommerce.gemspec b/bigcommerce.gemspec index 6227472..0580103 100644 --- a/bigcommerce.gemspec +++ b/bigcommerce.gemspec @@ -19,8 +19,8 @@ Gem::Specification.new do |s| s.require_paths = ['lib'] s.files = Dir['README.md', 'lib/**/*', 'bigcommerce.gemspec'] - s.add_dependency 'faraday', '~> 1.1.0' - s.add_dependency 'faraday_middleware', '~> 1.0' + s.add_dependency 'faraday', '>= 2.14', '< 3.0' + s.add_dependency 'faraday-gzip', '~> 3.0' s.add_dependency 'hashie', '>= 3.4', '~> 4' s.add_dependency 'jwt', '>= 1.5.4', '~> 2' end diff --git a/lib/bigcommerce.rb b/lib/bigcommerce.rb index 71e10f9..b809504 100644 --- a/lib/bigcommerce.rb +++ b/lib/bigcommerce.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require 'hashie' -require 'faraday_middleware' +require 'faraday' +require 'faraday/gzip' require_relative 'bigcommerce/version' require_relative 'bigcommerce/config' require_relative 'bigcommerce/connection' diff --git a/lib/bigcommerce/connection.rb b/lib/bigcommerce/connection.rb index 7e8b08e..266a69f 100644 --- a/lib/bigcommerce/connection.rb +++ b/lib/bigcommerce/connection.rb @@ -16,12 +16,12 @@ def self.build(config) conn.request :json conn.headers = HEADERS if config.auth == LEGACY_AUTH_MODE - conn.use Faraday::Request::BasicAuthentication, config.username, config.api_key + conn.request :authorization, :basic, config.username, config.api_key else conn.use Bigcommerce::Middleware::Auth, config end + conn.request :gzip conn.use Bigcommerce::Middleware::HttpException - conn.use FaradayMiddleware::Gzip conn.adapter Faraday.default_adapter end end diff --git a/lib/bigcommerce/middleware/auth.rb b/lib/bigcommerce/middleware/auth.rb index 89329f8..93b24a9 100644 --- a/lib/bigcommerce/middleware/auth.rb +++ b/lib/bigcommerce/middleware/auth.rb @@ -6,17 +6,19 @@ class Auth < Faraday::Middleware X_AUTH_CLIENT_HEADER = 'X-Auth-Client' X_AUTH_TOKEN_HEADER = 'X-Auth-Token' - def initialize(app, options = {}) - @app = app - @options = options - super(app) - end - def call(env) - env[:request_headers][X_AUTH_CLIENT_HEADER] = @options[:client_id] - env[:request_headers][X_AUTH_TOKEN_HEADER] = @options[:access_token] + env[:request_headers][X_AUTH_CLIENT_HEADER] = option_value(:client_id) + env[:request_headers][X_AUTH_TOKEN_HEADER] = option_value(:access_token) @app.call env end + + private + + def option_value(key) + return @options[key] if @options.key?(key) + + @options[key.to_s] + end end end end diff --git a/lib/bigcommerce/middleware/http_exception.rb b/lib/bigcommerce/middleware/http_exception.rb index c0dcc45..12c45dd 100644 --- a/lib/bigcommerce/middleware/http_exception.rb +++ b/lib/bigcommerce/middleware/http_exception.rb @@ -4,9 +4,15 @@ module Bigcommerce module Middleware - class HttpException < Faraday::Response::Middleware + class HttpException < Faraday::Middleware include Bigcommerce::HttpErrors + def call(env) + @app.call(env).on_complete do |response_env| + on_complete(response_env) + end + end + def on_complete(env) throw_http_exception! env[:status].to_i, env env diff --git a/lib/bigcommerce/request.rb b/lib/bigcommerce/request.rb index 2bfd577..eed6b3a 100644 --- a/lib/bigcommerce/request.rb +++ b/lib/bigcommerce/request.rb @@ -46,7 +46,7 @@ def raw_request(method, path, params = {}) private def build_response_object(response) - json = parse response.body + json = parse(response.body) if json.is_a? Array json.map { |obj| new obj } else diff --git a/lib/bigcommerce/version.rb b/lib/bigcommerce/version.rb index 76a31fc..2cf90b7 100644 --- a/lib/bigcommerce/version.rb +++ b/lib/bigcommerce/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Bigcommerce - VERSION = '1.1.0' + VERSION = '2.0.0' end diff --git a/spec/bigcommerce/bigcommerce_spec.rb b/spec/bigcommerce/bigcommerce_spec.rb index 67c9a09..69752fd 100644 --- a/spec/bigcommerce/bigcommerce_spec.rb +++ b/spec/bigcommerce/bigcommerce_spec.rb @@ -27,7 +27,11 @@ end it 'should have the correct auth middleware' do - expect(middleware).to include(Faraday::Request::BasicAuthentication) + expect(middleware).to include(Faraday::Request::Authorization) + end + + it 'should include gzip middleware' do + expect(middleware.map(&:klass)).to include(Faraday::Gzip::Middleware) end end @@ -42,6 +46,10 @@ it 'should have the correct auth middleware' do expect(middleware).to include(Bigcommerce::Middleware::Auth) end + + it 'should include gzip middleware' do + expect(middleware.map(&:klass)).to include(Faraday::Gzip::Middleware) + end end end @@ -61,7 +69,7 @@ expect(Bigcommerce.api.instance_variable_get('@builder') .instance_variable_get('@handlers')) - .to include(Faraday::Request::BasicAuthentication) + .to include(Faraday::Request::Authorization) Bigcommerce.configure do |config| config.access_token = 'jksdgkjbhksjdb' diff --git a/spec/bigcommerce/unit/middleware/auth_spec.rb b/spec/bigcommerce/unit/middleware/auth_spec.rb index a623047..570691c 100644 --- a/spec/bigcommerce/unit/middleware/auth_spec.rb +++ b/spec/bigcommerce/unit/middleware/auth_spec.rb @@ -27,5 +27,25 @@ expect(app).to receive(:call).with(expected_hash) subject end + + context 'when options use string keys' do + let(:options) do + { + 'client_id' => client_id, + 'access_token' => client_token + } + end + + it 'sets the correct headers' do + expected_hash = { + request_headers: { + 'X-Auth-Client' => client_id, + 'X-Auth-Token' => client_token + } + } + expect(app).to receive(:call).with(expected_hash) + subject + end + end end end