Sha256: 03627a50d7d9a4186e5f6f36caec0fb448fd66c86c1fc941d14bd0bc84529015

Contents?: true

Size: 964 Bytes

Versions: 10

Compression:

Stored size: 964 Bytes

Contents

require "etc"
require "socket"

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)
        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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
faraday-conductivity-2.0.0 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-1.0.1 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-1.0.0 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-0.3.1 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-0.3.0 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-0.2.1 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-0.2.0 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-0.1.1 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-0.1.0 lib/faraday/conductivity/user_agent.rb
faraday-conductivity-0.0.4 lib/faraday/conductivity/user_agent.rb