module Github::Connection

Specifies Http connection options

Constants

ALLOWED_OPTIONS

Public Instance Methods

caching?() click to toggle source
# File lib/github_api/connection.rb, line 40
def caching?
  !@connection.nil?
end
clear_cache() click to toggle source
# File lib/github_api/connection.rb, line 36
def clear_cache
  @connection = nil
end
connection(api, options = {}) click to toggle source

Creates http connection

Returns a Fraday::Connection object

# File lib/github_api/connection.rb, line 58
def connection(api, options = {})
  connection_options = default_options(options)
  clear_cache unless options.empty?
  connection_options.merge!(builder: stack(options.merge!(api: api)))
  connection_options.deep_merge!(options[:connection_options]) if options[:connection_options]
  if ENV['DEBUG']
    p "Connection options : \n"
    pp connection_options
  end
  @connection ||= Faraday.new(connection_options)
end
default_options(options = {}) click to toggle source
# File lib/github_api/connection.rb, line 16
def default_options(options = {})
  accept = options[:headers] && options[:headers][:accept]
  {
    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     => options[:user_agent]
    },
    ssl: options[:ssl],
    url: options[:endpoint]
  }.tap do |h|
    if type = options[:headers] && options[:headers][CONTENT_TYPE]
      h[:headers][CONTENT_TYPE] = type
    end
    h
  end
end
stack(options = {}) click to toggle source

Exposes middleware builder to facilitate custom stacks and easy addition of new extensions such as cache adapter.

@api public

# File lib/github_api/connection.rb, line 48
def stack(options = {})
  @stack ||= begin
    builder_class = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
    builder_class.new(&Github.default_middleware(options))
  end
end