lib/github_api/connection.rb in github_api-0.11.3 vs lib/github_api/connection.rb in github_api-0.12.0

- old
+ new

@@ -4,16 +4,12 @@ require 'github_api/response' require 'github_api/response/mashify' require 'github_api/response/jsonize' require 'github_api/response/raise_error' require 'github_api/response/header' -require 'github_api/request/oauth2' -require 'github_api/request/basic_auth' -require 'github_api/request/jsonize' module Github - # Specifies Http connection options module Connection extend self include Github::Constants @@ -31,35 +27,36 @@ headers: { ACCEPT => accept || 'application/vnd.github.v3+json,' \ 'application/vnd.github.beta+json;q=0.5,' \ 'application/json;q=0.1', ACCEPT_CHARSET => 'utf-8', - USER_AGENT => user_agent + USER_AGENT => options[:user_agent] }, - ssl: ssl, - url: options.fetch(:endpoint) { Github.endpoint } + ssl: options[:ssl], + url: options[:endpoint] } end # Default middleware stack that uses default adapter as specified at # configuration stage. # def default_middleware(options = {}) + api = options[:api] proc do |builder| builder.use Github::Request::Jsonize builder.use Faraday::Request::Multipart builder.use Faraday::Request::UrlEncoded - builder.use Github::Request::OAuth2, oauth_token if oauth_token? - builder.use Github::Request::BasicAuth, authentication if basic_authed? + builder.use Github::Request::OAuth2, api.oauth_token if api.oauth_token? + builder.use Github::Request::BasicAuth, api.authentication if api.basic_authed? builder.use Faraday::Response::Logger if ENV['DEBUG'] unless options[:raw] builder.use Github::Response::Mashify builder.use Github::Response::Jsonize end builder.use Github::Response::RaiseError - builder.adapter adapter + builder.adapter options[:adapter] end end @connection = nil @@ -76,29 +73,30 @@ # Exposes middleware builder to facilitate custom stacks and easy # addition of new extensions such as cache adapter. # def stack(options = {}, &block) @stack ||= begin + builder_class = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder + if block_given? - Faraday::Builder.new(&block) + builder_class.new(&block) else - Faraday::Builder.new(&default_middleware(options)) + builder_class.new(&default_middleware(options)) end end end # Creates http connection # # Returns a Fraday::Connection object - def connection(options = {}) + def connection(api, options = {}) connection_options = default_options(options) clear_cache unless options.empty? - connection_options.merge!(builder: stack(options)) + connection_options.merge!(builder: stack(options.merge!(api: api))) if ENV['DEBUG'] p "Connection options : \n" pp connection_options end @connection ||= Faraday.new(connection_options) end - end # Connection end # Github