Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/flagsmith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
# This is the patch to use slice in earler versions
require 'flagsmith/hash_slice'

require 'flagsmith/version'

require 'flagsmith/sdk/analytics_processor'
require 'flagsmith/sdk/api_client'
require 'flagsmith/sdk/config'
require 'flagsmith/sdk/errors'
require 'flagsmith/sdk/intervals'
require 'flagsmith/sdk/utils'
require 'flagsmith/sdk/pooling_manager'
require 'flagsmith/sdk/models/flags'
require 'flagsmith/sdk/models/segments'
Expand Down
1 change: 1 addition & 0 deletions lib/flagsmith/sdk/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def build_headers(faraday, config)
faraday.headers['Accept'] = 'application/json'
faraday.headers['Content-Type'] = 'application/json'
faraday.headers['X-Environment-Key'] = config.environment_key
faraday.headers['User-Agent'] = Flagsmith::SDK::Utils.user_agent
faraday.headers.merge(config.custom_headers)
end

Expand Down
29 changes: 29 additions & 0 deletions lib/flagsmith/sdk/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Flagsmith
module SDK
# Utility functions
module Utils
FLAGSMITH_USER_AGENT = 'flagsmith-ruby-sdk'
FLAGSMITH_UNKNOWN_VERSION = 'unknown'

module_function

# Returns the user agent string for HTTP requests
# @return [String] user agent in format "flagsmith-ruby-sdk/version"
def user_agent
"#{FLAGSMITH_USER_AGENT}/#{version}"
end

# Returns the SDK version
# @return [String] version string or 'unknown' if not available
def version
return Flagsmith::VERSION if defined?(Flagsmith::VERSION)

FLAGSMITH_UNKNOWN_VERSION
rescue StandardError
FLAGSMITH_UNKNOWN_VERSION
end
end
end
end
22 changes: 22 additions & 0 deletions spec/sdk/api_client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Flagsmith::ApiClient do
let(:config) do
Flagsmith::Config.new(
environment_key: 'test-key',
api_url: 'https://edge.api.flagsmith.com/api/v1/'
)
end

describe 'headers' do
it 'sets User-Agent header with SDK version' do
api_client = described_class.new(config)
connection = api_client.instance_variable_get(:@conn)

expected_user_agent = "flagsmith-ruby-sdk/#{Flagsmith::VERSION}"
expect(connection.headers['User-Agent']).to eq(expected_user_agent)
end
end
end
39 changes: 39 additions & 0 deletions spec/sdk/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Flagsmith::SDK::Utils do
describe '.user_agent' do
it 'returns user agent with version' do
expected_user_agent = "flagsmith-ruby-sdk/#{Flagsmith::VERSION}"
expect(described_class.user_agent).to eq(expected_user_agent)
end

it 'includes the correct format' do
user_agent = described_class.user_agent
expect(user_agent).to match(/^flagsmith-ruby-sdk\/\d+\.\d+\.\d+$/)
end
end

describe '.version' do
it 'returns the Flagsmith version' do
expect(described_class.version).to eq(Flagsmith::VERSION)
end

it 'returns a non-empty string' do
expect(described_class.version).to be_a(String)
expect(described_class.version).not_to be_empty
end

context 'when VERSION constant is not available' do
it 'returns unknown as fallback' do
version_backup = Flagsmith::VERSION
Flagsmith.send(:remove_const, :VERSION)

expect(described_class.version).to eq('unknown')

Flagsmith.const_set(:VERSION, version_backup)
end
end
end
end