lib/faraday/conductivity/user_agent.rb in faraday-conductivity-0.0.3 vs lib/faraday/conductivity/user_agent.rb in faraday-conductivity-0.0.4

- old
+ new

@@ -1,25 +1,49 @@ require "etc" require "socket" -require "thread" module Faraday module Conductivity class UserAgent < Faraday::Middleware def initialize(app, options = {}) super(app) @name = options.fetch(:app) { "Faraday" } @version = options.fetch(:version) { "0.0" } + @environment = options.fetch(:environment) { Environment.new } end def call(env) - login = Etc.getlogin - hostname = Socket.gethostname - pid = Process.pid - user_agent = "#{@name}/#{@version} (#{hostname}; #{login}; #{pid}) #{RUBY_ENGINE}/#{RUBY_VERSION} (#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})" - env[:request_headers]['User-Agent'] ||= user_agent + env[:request_headers]['User-Agent'] = user_agent @app.call(env) + end + + def user_agent + [ app, ruby ].join(' ') + end + + def app + "#{@name}/#{@version} (#{@environment.hostname}; #{@environment.login}; #{@environment.pid})" + end + + def ruby + "#{RUBY_ENGINE}/#{RUBY_VERSION} (#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})" + end + + end + + class Environment + + def login + Etc.getlogin + end + + def hostname + Socket.gethostname + end + + def pid + Process.pid end end end end