class Aws::Plugins::UserAgent::Handler::UserAgent

Public Class Methods

new(context) click to toggle source
# File lib/aws-sdk-core/plugins/user_agent.rb, line 77
def initialize(context)
  @context = context
end

Public Instance Methods

to_s() click to toggle source
# File lib/aws-sdk-core/plugins/user_agent.rb, line 81
def to_s
  ua = "aws-sdk-ruby3/#{CORE_GEM_VERSION}"
  ua += ' ua/2.1'
  if (api_m = api_metadata)
    ua += " #{api_m}"
  end
  ua += " #{os_metadata}"
  ua += " #{language_metadata}"
  if (env_m = env_metadata)
    ua += " #{env_m}"
  end
  if (app_id_m = app_id_metadata)
    ua += " #{app_id_m}"
  end
  if (framework_m = framework_metadata)
    ua += " #{framework_m}"
  end
  if (metric_m = metric_metadata)
    ua += " #{metric_m}"
  end
  if @context.config.user_agent_suffix
    ua += " #{@context.config.user_agent_suffix}"
  end
  ua.strip
end

Private Instance Methods

api_metadata() click to toggle source

Used to be gem_name/gem_version

# File lib/aws-sdk-core/plugins/user_agent.rb, line 110
def api_metadata
  service_id = @context.config.api.metadata['serviceId']
  return unless service_id

  service_id = service_id.gsub(' ', '_').downcase
  gem_version = @context[:gem_version]
  "api/#{service_id}##{gem_version}"
end
app_id_metadata() click to toggle source
# File lib/aws-sdk-core/plugins/user_agent.rb, line 149
def app_id_metadata
  return unless (app_id = @context.config.sdk_ua_app_id)

  # Sanitize and only allow these characters
  app_id = app_id.gsub(/[^!#$%&'*+\-.^_`|~0-9A-Za-z]/, '-')
  "app/#{app_id}"
end
env_metadata() click to toggle source
# File lib/aws-sdk-core/plugins/user_agent.rb, line 143
def env_metadata
  return unless (execution_env = ENV['AWS_EXECUTION_ENV'])

  "exec-env/#{execution_env}"
end
framework_metadata() click to toggle source
# File lib/aws-sdk-core/plugins/user_agent.rb, line 157
def framework_metadata
  if (frameworks_cfg = @context.config.user_agent_frameworks).empty?
    return
  end

  # Frameworks may be aws-record, aws-sdk-rails, etc.
  regex = /gems\/(?<name>#{frameworks_cfg.join('|')})-(?<version>\d+\.\d+\.\d+)/.freeze
  frameworks = {}
  Kernel.caller.each do |line|
    match = line.match(regex)
    next unless match

    frameworks[match[:name]] = match[:version]
  end
  frameworks.map { |n, v| "lib/#{n}##{v}" }.join(' ')
end
language_metadata() click to toggle source

Used to be RUBY_ENGINE/RUBY_VERSION

# File lib/aws-sdk-core/plugins/user_agent.rb, line 139
def language_metadata
  "lang/#{RUBY_ENGINE}##{RUBY_ENGINE_VERSION} md/#{RUBY_VERSION}"
end
metric_metadata() click to toggle source
# File lib/aws-sdk-core/plugins/user_agent.rb, line 174
def metric_metadata
  if Thread.current[:aws_sdk_core_user_agent_metric].nil? ||
     Thread.current[:aws_sdk_core_user_agent_metric].empty?
    return
  end

  metrics = Thread.current[:aws_sdk_core_user_agent_metric].join(',')
  # Metric metadata is limited to 1024 bytes
  return "m/#{metrics}" if metrics.bytesize <= 1024

  # Removes the last unfinished metric
  "m/#{metrics[0...metrics[0..1024].rindex(',')]}"
end
os_metadata() click to toggle source

Used to be RUBY_PLATFORM

# File lib/aws-sdk-core/plugins/user_agent.rb, line 120
def os_metadata
  os =
    case RbConfig::CONFIG['host_os']
    when /mac|darwin/
      'macos'
    when /linux|cygwin/
      'linux'
    when /mingw|mswin/
      'windows'
    else
      'other'
    end
  metadata = "os/#{os}"
  local_version = Gem::Platform.local.version
  metadata += "##{local_version}" if local_version
  metadata += " md/#{RbConfig::CONFIG['host_cpu']}"
end