module Github::Connection

def caching? # :nodoc:

:nodoc:
def caching? # :nodoc:
  !@connection.nil?
end

def clear_cache # :nodoc:

:nodoc:
def clear_cache # :nodoc:
  @connection = nil
end

def connection(options = {}) # :nodoc:

:nodoc:
def connection(options = {}) # :nodoc:
  conn_options = default_options(options)
  clear_cache unless options.empty?
  @connection ||= begin
    Faraday.new(conn_options) do |builder|
      puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG']
      builder.use Github::Request::Jsonize
      builder.use Faraday::Request::Multipart
      builder.use Faraday::Request::UrlEncoded
      builder.use Faraday::Response::Logger if ENV['DEBUG']
      builder.use Github::Request::OAuth2, oauth_token if oauth_token?
      builder.use Github::Request::BasicAuth, authentication if basic_authed?
      builder.use Github::Response::Helpers
      unless options[:raw]
        builder.use Github::Response::Mashify
        builder.use Github::Response::Jsonize
      end
      builder.use Github::Response::RaiseError
      builder.adapter adapter
    end
  end
end

def default_options(options={}) # :nodoc:

:nodoc:
def default_options(options={}) # :nodoc:
  {
    :headers => {
      ACCEPT           => "application/vnd.github.v3.raw+json," \
                          "application/vnd.github.beta.raw+json;q=0.5," \
                          "application/json;q=0.1",
      ACCEPT_CHARSET   => "utf-8",
      USER_AGENT       => user_agent,
      CONTENT_TYPE     => 'application/x-www-form-urlencoded'
    },
    :ssl => { :verify => false },
    :url => options.fetch(:endpoint) { Github.endpoint }
  }.merge(options)
end