Skip to content

Commit

Permalink
Put header in Faraday init and remove constants
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaswilliamgomes committed Aug 10, 2023
1 parent d9ebaa5 commit 7e9f6e5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
13 changes: 6 additions & 7 deletions lib/incognia_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def initialize(client_id:, client_secret:, host:)
@client_secret = client_secret
@host = host

@connection = Faraday.new(host) do |faraday|
headers = { 'User-Agent' => "incognia-ruby/#{Incognia::VERSION} " \
"({#{RbConfig::CONFIG['host']}}) " \
"{#{RbConfig::CONFIG['arch']}} " \
"Ruby/#{RbConfig::CONFIG['ruby_version']}" }

@connection = Faraday.new(host, headers: headers) do |faraday|
faraday.request :json
faraday.response :json, content_type: /\bjson$/
faraday.response :raise_error
Expand All @@ -26,12 +31,6 @@ def initialize(client_id:, client_secret:, host:)
def request(method, endpoint = nil, data = nil, headers = {})
json_data = JSON.generate(data) if data

user_agent_header = "incognia-ruby/#{Incognia::VERSION} " +
"({#{Util::OS_HOST}}) {#{Util::OS_ARCH}} " +
"Ruby/#{Util::LANGUAGE_VERSION}"

headers.merge!('User-Agent' => user_agent_header)

connection.send(method, endpoint, json_data, headers) do |r|
r.headers[Faraday::Request::Authorization::KEY] ||= Faraday::Request
.lookup_middleware(:authorization)
Expand Down
4 changes: 0 additions & 4 deletions lib/incognia_api/util.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module Incognia
module Util
OS_HOST = RbConfig::CONFIG['host']
OS_ARCH = RbConfig::CONFIG['arch']
LANGUAGE_VERSION = RbConfig::CONFIG['ruby_version']

def self.symbolize_names(object)
case object
when Hash
Expand Down
8 changes: 4 additions & 4 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ module Incognia
end

it "injects User-Agent header" do
user_agent_header = { 'User-Agent' => "incognia-ruby/#{Incognia::VERSION} " +
"({#{Util::OS_HOST}}) " +
"{#{Util::OS_ARCH}} " +
"Ruby/#{Util::LANGUAGE_VERSION}" }
user_agent_header = { 'User-Agent' => "incognia-ruby/#{Incognia::VERSION} " \
"({#{RbConfig::CONFIG['host']}}) " \
"{#{RbConfig::CONFIG['arch']}} " \
"Ruby/#{RbConfig::CONFIG['ruby_version']}" }

stub_token_request
stub = stub_request(:post, test_endpoint)
Expand Down

0 comments on commit 7e9f6e5

Please sign in to comment.