diff --git a/lib/incognia_api/client.rb b/lib/incognia_api/client.rb index 4c752e6..263d8b1 100644 --- a/lib/incognia_api/client.rb +++ b/lib/incognia_api/client.rb @@ -1,4 +1,5 @@ require "time" +require "pry" module Incognia class Client @@ -26,6 +27,12 @@ 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) diff --git a/lib/incognia_api/util.rb b/lib/incognia_api/util.rb index e0a1744..f4b83e8 100644 --- a/lib/incognia_api/util.rb +++ b/lib/incognia_api/util.rb @@ -1,5 +1,9 @@ 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 diff --git a/spec/client_spec.rb b/spec/client_spec.rb index ad986bd..30d76be 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -51,6 +51,32 @@ module Incognia expect(stub).to have_been_made.once 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}" } + + stub_token_request + stub = stub_request(:post, test_endpoint) + .with( + body: sample_json, + headers: user_agent_header + ).to_return( + status: 200, + body: sample_json, + headers: { 'Content-Type' => 'application/json' } + ) + + subject.request( + :post, + "v2/endpoint", + { foo: :bar } + ) + + expect(stub).to have_been_made.once + end + context "when passing an Authorization header" do it "overrides default header" do stub = stub_request(:post, test_endpoint).