lib/travis/client/session.rb in travis-1.5.3 vs lib/travis/client/session.rb in travis-1.5.4

- old
+ new

@@ -1,6 +1,7 @@ require 'travis/client' +require 'travis/version' require 'faraday' require 'faraday_middleware' require 'travis/tools/system' @@ -14,44 +15,52 @@ module Travis module Client class Session SSL_OPTIONS = { :ca_file => File.expand_path("../../cacert.pem", __FILE__) } include Methods - attr_reader :connection, :headers, :access_token, :instruments, :faraday_adapter + attr_reader :connection, :headers, :access_token, :instruments, :faraday_adapter, :agent_info def initialize(options = Travis::Client::ORG_URI) @headers = {} @cache = {} @instruments = [] + @agent_info = [] @config = nil @faraday_adapter = defined?(Typhoeus) ? :typhoeus : :net_http options = { :uri => options } unless options.respond_to? :each_pair options.each_pair { |key, value| public_send("#{key}=", value) } raise ArgumentError, "neither :uri nor :connection specified" unless connection - headers['Accept'] ||= 'application/json; version=2' + headers['Accept'] = 'application/json; version=2' + set_user_agent end def uri connection.url_prefix.to_s if connection end + def agent_info=(info) + @agent_info = [info].flatten.freeze + set_user_agent + end + def uri=(uri) clear_cache! self.connection = Faraday.new(:url => uri, :ssl => SSL_OPTIONS) do |faraday| faraday.request :url_encoded - faraday.response :json - faraday.response :follow_redirects - faraday.response :raise_error + faraday.response :json + faraday.response :follow_redirects + faraday.response :raise_error faraday.adapter(*faraday_adapter) end end def faraday_adapter=(adapter) @faraday_adapter = adapter self.uri &&= uri + set_user_agent end def access_token=(token) clear_cache! @access_token = token @@ -178,9 +187,22 @@ def private_channels? access_token and user.channels != ['common'] end private + + def set_user_agent + adapter = Array === faraday_adapter ? faraday_adapter.first : faraday_adapter + adapter = adapter.to_s.capitalize.gsub(/_http_(.)/) { "::HTTP::#{$1.upcase}" }.gsub(/_http/, '::HTTP') + headers['User-Agent'] = "Travis/#{Travis::VERSION} (#{Travis::Tools::System.description(agent_info)}) Faraday/#{Faraday::VERSION} #{adapter}/#{adapter_version(adapter)}" + end + + def adapter_version(adapter) + version = Object.const_get(adapter).const_get("VERSION") + [*version].join('.') + rescue Exception + "unknown" + end def instrumented(name, *args) name = [name, *args.map(&:inspect)].join(" ") if args.any? result = nil chain = instruments + [proc { |n,l| result = yield }]