Skip to content

Commit 184e991

Browse files
committed
feat: added-version-in-user-agent-header
1 parent da0c6e0 commit 184e991

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

lib/flagsmith.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
# This is the patch to use slice in earler versions
88
require 'flagsmith/hash_slice'
99

10+
require 'flagsmith/version'
11+
1012
require 'flagsmith/sdk/analytics_processor'
1113
require 'flagsmith/sdk/api_client'
1214
require 'flagsmith/sdk/config'
1315
require 'flagsmith/sdk/errors'
1416
require 'flagsmith/sdk/intervals'
17+
require 'flagsmith/sdk/utils'
1518
require 'flagsmith/sdk/pooling_manager'
1619
require 'flagsmith/sdk/models/flags'
1720
require 'flagsmith/sdk/models/segments'

lib/flagsmith/sdk/api_client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def build_headers(faraday, config)
3131
faraday.headers['Accept'] = 'application/json'
3232
faraday.headers['Content-Type'] = 'application/json'
3333
faraday.headers['X-Environment-Key'] = config.environment_key
34+
faraday.headers['User-Agent'] = Flagsmith::SDK::Utils.user_agent
3435
faraday.headers.merge(config.custom_headers)
3536
end
3637

lib/flagsmith/sdk/utils.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module Flagsmith
4+
module SDK
5+
# Utility functions
6+
module Utils
7+
FLAGSMITH_USER_AGENT = 'flagsmith-ruby-sdk'
8+
FLAGSMITH_UNKNOWN_VERSION = 'unknown'
9+
10+
module_function
11+
12+
# Returns the user agent string for HTTP requests
13+
# @return [String] user agent in format "flagsmith-ruby-sdk/version"
14+
def user_agent
15+
"#{FLAGSMITH_USER_AGENT}/#{version}"
16+
end
17+
18+
# Returns the SDK version
19+
# @return [String] version string or 'unknown' if not available
20+
def version
21+
return Flagsmith::VERSION if defined?(Flagsmith::VERSION)
22+
23+
FLAGSMITH_UNKNOWN_VERSION
24+
rescue StandardError
25+
FLAGSMITH_UNKNOWN_VERSION
26+
end
27+
end
28+
end
29+
end

spec/sdk/api_client_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Flagsmith::ApiClient do
6+
let(:config) do
7+
Flagsmith::Config.new(
8+
environment_key: 'test-key',
9+
api_url: 'https://edge.api.flagsmith.com/api/v1/'
10+
)
11+
end
12+
13+
describe 'headers' do
14+
it 'sets User-Agent header with SDK version' do
15+
api_client = described_class.new(config)
16+
connection = api_client.instance_variable_get(:@conn)
17+
18+
expected_user_agent = "flagsmith-ruby-sdk/#{Flagsmith::VERSION}"
19+
expect(connection.headers['User-Agent']).to eq(expected_user_agent)
20+
end
21+
end
22+
end

spec/sdk/utils_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Flagsmith::SDK::Utils do
6+
describe '.user_agent' do
7+
it 'returns user agent with version' do
8+
expected_user_agent = "flagsmith-ruby-sdk/#{Flagsmith::VERSION}"
9+
expect(described_class.user_agent).to eq(expected_user_agent)
10+
end
11+
12+
it 'includes the correct format' do
13+
user_agent = described_class.user_agent
14+
expect(user_agent).to match(/^flagsmith-ruby-sdk\/\d+\.\d+\.\d+$/)
15+
end
16+
end
17+
18+
describe '.version' do
19+
it 'returns the Flagsmith version' do
20+
expect(described_class.version).to eq(Flagsmith::VERSION)
21+
end
22+
23+
it 'returns a non-empty string' do
24+
expect(described_class.version).to be_a(String)
25+
expect(described_class.version).not_to be_empty
26+
end
27+
28+
context 'when VERSION constant is not available' do
29+
it 'returns unknown as fallback' do
30+
version_backup = Flagsmith::VERSION
31+
Flagsmith.send(:remove_const, :VERSION)
32+
33+
expect(described_class.version).to eq('unknown')
34+
35+
Flagsmith.const_set(:VERSION, version_backup)
36+
end
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)