From 4681ad5ff338a2c5c2440ff71215c6ea88997176 Mon Sep 17 00:00:00 2001 From: bodrovis Date: Fri, 6 Sep 2024 14:41:29 +0300 Subject: [PATCH] refactors --- docs/Gemfile | 6 +- lib/ruby_lokalise_api/collections/base.rb | 34 +++++------ lib/ruby_lokalise_api/connection.rb | 32 ++++++++--- .../endpoints/base_endpoint.rb | 6 +- .../endpoints/oauth2/oauth2_endpoint.rb | 14 ++++- lib/ruby_lokalise_api/oauth2/auth.rb | 57 ++++++++++--------- lib/ruby_lokalise_api/request.rb | 12 ++-- lib/ruby_lokalise_api/resources/base.rb | 26 ++++++--- lib/ruby_lokalise_api/response.rb | 10 +--- lib/ruby_lokalise_api/utils/attributes.rb | 16 ++++-- lib/ruby_lokalise_api/utils/classes.rb | 15 +++-- lib/ruby_lokalise_api/utils/keys.rb | 39 ++++++++----- lib/ruby_lokalise_api/utils/loaders.rb | 31 ++++++++-- lib/ruby_lokalise_api/utils/strings.rb | 15 ++--- spec/support/expectations.rb | 2 +- spec/support/fixtures.rb | 15 +++-- spec/support/stubs.rb | 49 ++++++++-------- 17 files changed, 224 insertions(+), 155 deletions(-) diff --git a/docs/Gemfile b/docs/Gemfile index aa18ad5..0b03894 100644 --- a/docs/Gemfile +++ b/docs/Gemfile @@ -10,14 +10,14 @@ source 'https://rubygems.org' # # This will help ensure the proper Jekyll version is running. # Happy Jekylling! -gem 'jekyll', '~> 3.9.0' +gem 'jekyll', '~> 3.10.0' # This is the default theme for new Jekyll sites. You may change this to anything you like. gem 'minima', '~> 2.0' # If you want to use GitHub Pages, remove the "gem "jekyll"" above and # uncomment the line below. To upgrade, run `bundle update github-pages`. -gem 'github-pages', '~> 231', group: :jekyll_plugins +gem 'github-pages', '~> 232', group: :jekyll_plugins # If you have any plugins, put them here! group :jekyll_plugins do @@ -32,7 +32,7 @@ install_if -> { RUBY_PLATFORM =~ /mingw|mswin|java/ } do end # Performance-booster for watching directories on Windows -gem 'wdm', '~> 0.1.0', install_if: Gem.win_platform? +gem 'wdm', '~> 0.2.0', install_if: Gem.win_platform? # kramdown v2 ships without the gfm parser by default. If you're using # kramdown v1, comment out this line. diff --git a/lib/ruby_lokalise_api/collections/base.rb b/lib/ruby_lokalise_api/collections/base.rb index 15a9f1a..32b74a8 100644 --- a/lib/ruby_lokalise_api/collections/base.rb +++ b/lib/ruby_lokalise_api/collections/base.rb @@ -32,13 +32,7 @@ def initialize(response) def next_page return nil if last_page? - override_params = { page: current_page + 1 } - - self.class.new( - reinit_endpoint( - override_req_params: override_params - ).do_get - ) + fetch_page(current_page + 1) end # Tries to fetch the next cursor for paginated collection @@ -46,13 +40,7 @@ def next_page def load_next_cursor return nil unless next_cursor? - override_params = { cursor: next_cursor } - - self.class.new( - reinit_endpoint( - override_req_params: override_params - ).do_get - ) + fetch_cursor(next_cursor) end # Tries to fetch the previous page for paginated collection @@ -60,11 +48,7 @@ def load_next_cursor def prev_page return nil if first_page? - self.class.new( - reinit_endpoint( - override_req_params: { page: current_page - 1 } - ).do_get - ) + fetch_page(current_page - 1) end # Checks whether the next page is available @@ -131,7 +115,7 @@ def produce_collection_from(response) data_key_plural = collection_key_for klass: self.class.base_name resources_data = content[data_key_plural] - other_data = content.reject { |key, _| key == data_key_plural } + other_data = content.except(data_key_plural) @collection = build_collection resources_data, other_data end @@ -165,6 +149,16 @@ def resource_endpoint klass.const_defined?(:RESOURCES_ENDPOINT) ? klass.const_get(:RESOURCES_ENDPOINT) : klass.const_get(:ENDPOINT) end + + # Helper method to fetch a page for paginated collections + def fetch_page(page) + self.class.new(reinit_endpoint(override_req_params: { page: page }).do_get) + end + + # Helper method to fetch the next cursor + def fetch_cursor(cursor) + self.class.new(reinit_endpoint(override_req_params: { cursor: cursor }).do_get) + end end end end diff --git a/lib/ruby_lokalise_api/connection.rb b/lib/ruby_lokalise_api/connection.rb index 330f211..98e8373 100644 --- a/lib/ruby_lokalise_api/connection.rb +++ b/lib/ruby_lokalise_api/connection.rb @@ -4,10 +4,14 @@ module RubyLokaliseApi # Module to setup connection using Faraday module Connection # Creates a new Faraday object with specified params + # + # @param endpoint [Object] the API endpoint + # @param params [Hash] additional connection parameters + # @return [Faraday::Connection] the Faraday connection object def connection(endpoint, params = {}) Faraday.new(options(endpoint, params), request_params_for(endpoint.client)) do |faraday| faraday.adapter Faraday.default_adapter - faraday.request(:gzip) + faraday.request :gzip end end @@ -17,19 +21,33 @@ def options(endpoint, params) req_params = __base_options(endpoint) client = endpoint.client - if client.respond_to?(:token) && client.respond_to?(:token_header) - req_params[:headers][client.token_header] = client.token - end + add_token_header(req_params, client) if client_responds_to_token?(client) - # Sending content-type is needed only when the body is actually present - # Trying to send this header in other cases seems to result in error 500 - req_params[:headers]['Content-type'] = 'application/json' if !params[:get_request] && endpoint.req_params + # Set Content-Type if required (skip for GET requests) + add_content_type_header(req_params, params, endpoint) req_params[:headers][:accept_encoding] = 'gzip,deflate,br' req_params end + # Adds the token header to the request parameters if token is present + def add_token_header(req_params, client) + req_params[:headers][client.token_header] = client.token + end + + # Checks if the client can respond to token and token header methods + def client_responds_to_token?(client) + client.respond_to?(:token) && client.respond_to?(:token_header) + end + + # Conditionally adds the Content-Type header + def add_content_type_header(req_params, params, endpoint) + return unless !params[:get_request] && endpoint.req_params + + req_params[:headers]['Content-Type'] = 'application/json' + end + def __base_options(endpoint) { headers: { diff --git a/lib/ruby_lokalise_api/endpoints/base_endpoint.rb b/lib/ruby_lokalise_api/endpoints/base_endpoint.rb index e315f80..2d1eaa3 100644 --- a/lib/ruby_lokalise_api/endpoints/base_endpoint.rb +++ b/lib/ruby_lokalise_api/endpoints/base_endpoint.rb @@ -28,11 +28,11 @@ def reinitialize(query_params: nil, req_params: {}) end def base_url - self.class.const_get(:BASE_URL) + self.class::BASE_URL end def full_uri - base_url + uri + base_url + uri.to_s end # Creates methods like `do_post`, `do_get` that proxy calls to the @@ -52,7 +52,7 @@ def partial_uri(*_args) end def partial_uri_template - self.class.const_get(:PARTIAL_URI_TEMPLATE) + self.class::PARTIAL_URI_TEMPLATE end end end diff --git a/lib/ruby_lokalise_api/endpoints/oauth2/oauth2_endpoint.rb b/lib/ruby_lokalise_api/endpoints/oauth2/oauth2_endpoint.rb index c8d7806..130ae5b 100644 --- a/lib/ruby_lokalise_api/endpoints/oauth2/oauth2_endpoint.rb +++ b/lib/ruby_lokalise_api/endpoints/oauth2/oauth2_endpoint.rb @@ -10,20 +10,30 @@ class OAuth2Endpoint < BaseEndpoint def initialize(client, params = {}) super - @uri = partial_uri(base_query(*@query_params), params.fetch(:get, [])) + @uri = build_uri(params.fetch(:get, [])) end private + # Builds the complete URI for the OAuth2 endpoint + def build_uri(query) + partial_uri(base_query(*@query_params), query) + end + def partial_uri(segments, query) template = super template.expand( segments: segments.to_a.flatten, - query: query.filter { |_k, v| !v.nil? } + query: filter_query_params(query) ).to_s end + # Filters out nil values from query parameters + def filter_query_params(query) + query.compact + end + def base_query(segment = nil) [segment] end diff --git a/lib/ruby_lokalise_api/oauth2/auth.rb b/lib/ruby_lokalise_api/oauth2/auth.rb index b80bd32..3001832 100644 --- a/lib/ruby_lokalise_api/oauth2/auth.rb +++ b/lib/ruby_lokalise_api/oauth2/auth.rb @@ -29,7 +29,7 @@ def oauth2_endpoint def auth(scope:, redirect_uri: nil, state: nil) get_params = { client_id: client_id, - scope: (scope.is_a?(Array) ? scope.join(' ') : scope), + scope: scope_to_string(scope), state: state, redirect_uri: redirect_uri } @@ -37,41 +37,42 @@ def auth(scope:, redirect_uri: nil, state: nil) oauth2_endpoint.new(self, query: 'auth', get: get_params).full_uri end - # Requests OAuth2 access token. Requires OAuth2 code obtained - # using the `.auth` method - # @return [RubyLokaliseApi::Resources::OAuth2Token] - # @param code [String] + # Requests an OAuth2 access token using a code + # @param code [String] The authorization code + # @return [RubyLokaliseApi::Resources::OAuth2Token] The OAuth2 token resource def token(code) - endpoint = oauth2_endpoint.new( - self, - query: :token, - req: common_params.merge( - grant_type: 'authorization_code', - code: code - ) - ) - - RubyLokaliseApi::Resources::OAuth2Token.new endpoint.do_post + request_token(grant_type: 'authorization_code', code: code) end - # Refreshes expired OAuth2 access token. - # @return [RubyLokaliseApi::Resources::OAuth2RefreshedToken] - # @param refresh_token [String] + # Refreshes an expired OAuth2 token + # @param refresh_token [String] The refresh token + # @return [RubyLokaliseApi::Resources::OAuth2RefreshedToken] The refreshed token resource def refresh(refresh_token) - endpoint = oauth2_endpoint.new( - self, - query: :token, - req: common_params.merge( - grant_type: 'refresh_token', - refresh_token: refresh_token - ) - ) - - RubyLokaliseApi::Resources::OAuth2RefreshedToken.new endpoint.do_post + request_token(grant_type: 'refresh_token', refresh_token: refresh_token) end private + # Generalized method for requesting a token + # @param params [Hash] Request parameters including grant type + # @return [RubyLokaliseApi::Resources::OAuth2Token, RubyLokaliseApi::Resources::OAuth2RefreshedToken] + def request_token(params) + endpoint = oauth2_endpoint.new(self, query: :token, req: common_params.merge(params)) + resource_class = if params[:grant_type] == 'authorization_code' + RubyLokaliseApi::Resources::OAuth2Token + else + RubyLokaliseApi::Resources::OAuth2RefreshedToken + end + resource_class.new(endpoint.do_post) + end + + # Converts scope to a space-separated string if it's an array + # @param scope [Array, String] The scope or scopes + # @return [String] The scope as a string + def scope_to_string(scope) + scope.is_a?(Array) ? scope.join(' ') : scope + end + def common_params { client_id: @client_id, diff --git a/lib/ruby_lokalise_api/request.rb b/lib/ruby_lokalise_api/request.rb index 739fa94..8dd7a1b 100644 --- a/lib/ruby_lokalise_api/request.rb +++ b/lib/ruby_lokalise_api/request.rb @@ -63,17 +63,19 @@ def raise_on_error!(response, body) end def respond_with(response, endpoint) - begin - body = custom_load response.body - rescue JSON::ParserError - respond_with_error(response.status, response.body) - end + body = parse_response_body(response) raise_on_error! response, body RubyLokaliseApi::Response.new(body, endpoint, response.headers) end + def parse_response_body(response) + custom_load(response.body) + rescue JSON::ParserError + respond_with_error(response.status, response.body) + end + def respond_with_error(code, body) raise(RubyLokaliseApi::Error, body['error'] || body) unless RubyLokaliseApi::Error::ERRORS.key? code diff --git a/lib/ruby_lokalise_api/resources/base.rb b/lib/ruby_lokalise_api/resources/base.rb index dcf6c0c..75eb73f 100644 --- a/lib/ruby_lokalise_api/resources/base.rb +++ b/lib/ruby_lokalise_api/resources/base.rb @@ -57,20 +57,28 @@ def reinit_endpoint(req_params = {}) @self_endpoint.reinitialize(query_params: read_main_params, req_params: req_params) end + # Populates attributes from the response content def populate_attrs_from(content) return unless content - data_key = data_key_for klass: self.class.base_name + data_key = data_key_for(klass: self.class.base_name) + supported_attrs.each { |attrib| set_instance_variable(attrib, content, data_key) } + end - supported_attrs.each do |attrib| - value = if content.key?(data_key) && content[data_key].is_a?(Hash) && content[data_key].key?(attrib) - content[data_key][attrib] - else - content[attrib] - end + # Sets the instance variable for a given attribute + def set_instance_variable(attrib, content, data_key) + value = if content_key_exists?(content, data_key, attrib) + content[data_key][attrib] + else + content[attrib] + end - instance_variable_set :"@#{attrib}", value - end + instance_variable_set(:"@#{attrib}", value) + end + + # Checks if the content contains the specified key + def content_key_exists?(content, data_key, attrib) + content.key?(data_key) && content[data_key].is_a?(Hash) && content[data_key].key?(attrib) end end end diff --git a/lib/ruby_lokalise_api/response.rb b/lib/ruby_lokalise_api/response.rb index 41a8ae5..980a84f 100644 --- a/lib/ruby_lokalise_api/response.rb +++ b/lib/ruby_lokalise_api/response.rb @@ -31,19 +31,11 @@ def patch_endpoint_with(new_endpoint) @endpoint = new_endpoint end - # Returns an array of pagination headers - def pagination_headers - self.class.const_get(:PAGINATION_HEADERS) - end - private # Keep only pagination headers def extract_headers_from(raw_headers) - raw_headers. - to_h. - keep_if { |header, _value| pagination_headers.include?(header) }. - transform_keys(&:to_sym) + raw_headers.to_h.slice(*PAGINATION_HEADERS).transform_keys(&:to_sym) end end end diff --git a/lib/ruby_lokalise_api/utils/attributes.rb b/lib/ruby_lokalise_api/utils/attributes.rb index 83ecd20..a8480e1 100644 --- a/lib/ruby_lokalise_api/utils/attributes.rb +++ b/lib/ruby_lokalise_api/utils/attributes.rb @@ -14,11 +14,11 @@ module Attributes # # @return [Array] def attributes_for(klass, filename) - @attributes ||= YAML.load_file(File.expand_path("../data/#{filename}", __dir__)).freeze + @attributes ||= load_attributes(filename) name = unify klass.base_name.snakecase - @attributes.key?(name) ? @attributes[name] : @attributes["#{name}s"] + @attributes[name] || @attributes["#{name}s"] end # Unify some resources' names (eg, `ProjectComment` and `KeyComment` have the same @@ -26,11 +26,15 @@ def attributes_for(klass, filename) # # @return [String] def unify(name) - UNIFIED_RESOURCES.each do |u_a| - return u_a if name.match?(/#{u_a}/) - end + UNIFIED_RESOURCES.find { |u_a| name.match?(/#{u_a}/) } || name + end - name + # Loads attributes from a YAML file + # + # @param filename [String] The filename to load attributes from + # @return [Hash] The loaded attributes hash + def load_attributes(filename) + YAML.load_file(File.expand_path("../data/#{filename}", __dir__)).freeze end end end diff --git a/lib/ruby_lokalise_api/utils/classes.rb b/lib/ruby_lokalise_api/utils/classes.rb index a28efa9..bf98a60 100644 --- a/lib/ruby_lokalise_api/utils/classes.rb +++ b/lib/ruby_lokalise_api/utils/classes.rb @@ -5,18 +5,25 @@ module RubyLokaliseApi module Utils module Classes refine Object do - # Turn `Module::Nested::ClassName` to just `ClassName` + # Extracts the base name of a class, removing any module nesting. + # + # @return [String] The base class name def base_name name.split('::').last end - # Converts object to array unless it is already an array + # Converts the object to an array, unless it is already an array. + # + # @return [Array] The object wrapped in an array if not already an array def to_array is_a?(Array) ? self : [self] end - # Converts object to array and then places this array - # inside hash under the provided key + # Converts the object to an array, then places this array inside a hash + # under the provided key. + # + # @param key [Symbol, String] The key under which to place the array + # @return [Hash] The hash with the array under the provided key def to_array_obj(key) return self if is_a?(Hash) && (key?(key) || key?(key.to_s)) diff --git a/lib/ruby_lokalise_api/utils/keys.rb b/lib/ruby_lokalise_api/utils/keys.rb index f7f242e..6eabbcb 100644 --- a/lib/ruby_lokalise_api/utils/keys.rb +++ b/lib/ruby_lokalise_api/utils/keys.rb @@ -5,25 +5,36 @@ module Utils module Keys using RubyLokaliseApi::Utils::Strings - # Reads DATA_KEY for resources. DATA_KEY specifies the name of the key - # in the API response that contains the actual data + # Retrieves the DATA_KEY for resources. + # DATA_KEY specifies the key in the API response containing the actual data. + # + # @param klass [Class] The class of the resource + # @return [String] The snake-cased DATA_KEY def data_key_for(klass:) - key = if Module.const_defined? "RubyLokaliseApi::Resources::#{klass}::DATA_KEY" - Module.const_get "RubyLokaliseApi::Resources::#{klass}::DATA_KEY" - else - klass - end - - key.snakecase + retrieve_data_key("RubyLokaliseApi::Resources::#{klass}::DATA_KEY", klass) end - # Reads DATA_KEY for collections. DATA_KEY specifies the name of the key - # in the API response that contains the actual data + # Retrieves the DATA_KEY for collections. + # DATA_KEY specifies the key in the API response containing the actual data. + # + # @param klass [Class] The class of the collection + # @return [String] The snake-cased DATA_KEY def collection_key_for(klass:) - key = if Module.const_defined?("RubyLokaliseApi::Collections::#{klass}::DATA_KEY") - Module.const_get("RubyLokaliseApi::Collections::#{klass}::DATA_KEY") + retrieve_data_key("RubyLokaliseApi::Collections::#{klass}::DATA_KEY", klass) + end + + private + + # Helper method to retrieve the DATA_KEY for a given class. + # + # @param constant_path [String] The constant path to check + # @param fallback [Class] The fallback class if the constant isn't defined + # @return [String] The snake-cased key + def retrieve_data_key(constant_path, fallback) + key = if Module.const_defined?(constant_path) + Module.const_get(constant_path) else - klass + fallback end key.snakecase diff --git a/lib/ruby_lokalise_api/utils/loaders.rb b/lib/ruby_lokalise_api/utils/loaders.rb index 59705f0..bbe375d 100644 --- a/lib/ruby_lokalise_api/utils/loaders.rb +++ b/lib/ruby_lokalise_api/utils/loaders.rb @@ -6,21 +6,40 @@ module Loaders private # Instantiates an endpoint + # + # @param name [String] The name of the endpoint + # @param client [Object] The client to associate with the endpoint (default: self) + # @param params [Hash] Additional parameters for the endpoint + # @return [Object] The instantiated endpoint def endpoint(name:, client: self, params: {}) - klass = RubyLokaliseApi.const_get "Endpoints::#{name}Endpoint" - klass.new client, params + instantiate("Endpoints::#{name}Endpoint", client, params) end # Instantiates a resource + # + # @param name [String] The name of the resource + # @param data [Hash] The data to initialize the resource with + # @return [Object] The instantiated resource def resource(name, data) - klass = RubyLokaliseApi.const_get "Resources::#{name}" - klass.new data + instantiate("Resources::#{name}", data) end # Instantiates a collection + # + # @param name [String] The name of the collection + # @param data [Hash] The data to initialize the collection with + # @return [Object] The instantiated collection def collection(name, data) - klass = RubyLokaliseApi.const_get "Collections::#{name}" - klass.new data + instantiate("Collections::#{name}", data) + end + + # Helper method to instantiate a class by constant name + # + # @param constant_path [String] The constant path for the class + # @param args [Array] Arguments to initialize the class with + # @return [Object] The instantiated class + def instantiate(constant_path, *args) + RubyLokaliseApi.const_get(constant_path).new(*args) end end end diff --git a/lib/ruby_lokalise_api/utils/strings.rb b/lib/ruby_lokalise_api/utils/strings.rb index e59b463..9e744bd 100644 --- a/lib/ruby_lokalise_api/utils/strings.rb +++ b/lib/ruby_lokalise_api/utils/strings.rb @@ -4,15 +4,16 @@ module RubyLokaliseApi module Utils module Strings refine String do - # Initial code taken from Facets gem by Rubyworks + # Converts a string to snake_case format. + # Original code inspired by the Facets gem by Rubyworks: # https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb def snakecase - gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). - gsub(/([a-z\d])([A-Z])/, '\1_\2'). - tr('-', '_'). - gsub(/\s/, '_'). - gsub(/__+/, '_'). - downcase + gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). # Handle acronyms like 'HTMLParser' + gsub(/([a-z\d])([A-Z])/, '\1_\2'). # Handle camelCase to snake_case + tr('-', '_'). # Replace dashes with underscores + gsub(/\s/, '_'). # Replace spaces with underscores + gsub(/__+/, '_'). # Collapse multiple underscores + downcase # Convert everything to lowercase end end end diff --git a/spec/support/expectations.rb b/spec/support/expectations.rb index f36c31c..3d6b017 100644 --- a/spec/support/expectations.rb +++ b/spec/support/expectations.rb @@ -38,7 +38,7 @@ def proper_endpoint(collection_obj) res.send(p).to_s end - expect(ep.uri.split('/')).to include(*params) + expect(ep.uri.split('/')).to include(*params) unless params.empty? end def collection_from(obj) diff --git a/spec/support/fixtures.rb b/spec/support/fixtures.rb index c8e2fc3..6b34241 100644 --- a/spec/support/fixtures.rb +++ b/spec/support/fixtures.rb @@ -5,17 +5,20 @@ module Fixtures class << self def eager_load - cached_stubs = Pathname.glob("#{fixture_path}/**/*.json").each_with_object({}) do |full_path, result| - result[ - full_path.relative_path_from(fixture_path).sub_ext('').to_s - ] = File.read(full_path).freeze - end + cached_stubs = load_fixtures - const_set :STUBS, cached_stubs + const_set :STUBS, cached_stubs.freeze end private + def load_fixtures + Pathname.glob("#{fixture_path}/**/*.json").each_with_object({}) do |full_path, result| + key = full_path.relative_path_from(fixture_path).sub_ext('').to_s + result[key] = File.read(full_path) + end + end + def fixture_path File.expand_path('../fixtures', __dir__) end diff --git a/spec/support/stubs.rb b/spec/support/stubs.rb index e576340..cf17734 100644 --- a/spec/support/stubs.rb +++ b/spec/support/stubs.rb @@ -29,45 +29,44 @@ def fixture(filename) private def response_params(resp) - params = { - status: resp.fetch(:code, 200) - } - - if resp[:body] - params[:body] = resp[:body].is_a?(Hash) ? Oj.dump(resp[:body], mode: :strict) : resp[:body] - end + { + status: resp.fetch(:code, 200), + body: formatted_body(resp[:body]), + headers: resp[:headers] + }.compact + end - params[:headers] = resp[:headers] if resp[:headers] + def formatted_body(body) + return unless body - params + body.is_a?(Hash) ? Oj.dump(body, mode: :strict) : body end def request_params(req) - params = { headers: { + { + headers: default_headers(req), + body: formatted_body(req[:body]), + query: req[:query] + }.compact + end + + def default_headers(req) + headers = { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip,deflate,br', 'User-Agent' => "ruby-lokalise-api gem/#{RubyLokaliseApi::VERSION}" - } } - - params = add_auth_header(params, req) - - # The default :object mode encode hashes in a way that's not properly - # recognized by Webmock - params[:body] = Oj.dump(req[:body], mode: :strict) if req[:body] - - params[:query] = req[:query] if req[:query] + } - params + add_auth_header(headers, req) end - def add_auth_header(params, req) - return params if req[:skip_token] + def add_auth_header(headers, req) + return headers if req[:skip_token] token_header = req.fetch(:token_header, 'X-Api-Token') token = req.fetch(:token) { ENV.fetch('LOKALISE_API_TOKEN', nil) } - params[:headers][token_header] = token - - params + headers[token_header] = token + headers end end