Module: Github::Connection

Extended by:
Connection
Includes:
Constants
Included in:
API, Connection
Defined in:
lib/github_api/connection.rb

Overview

Specifies Http connection options

Constant Summary collapse

ALLOWED_OPTIONS =
[
  :headers,
  :url,
  :params,
  :request,
  :ssl
].freeze

Constants included from Constants

Github::Constants::ACCEPT, Github::Constants::ACCEPTED_OAUTH_SCOPES, Github::Constants::ACCEPT_CHARSET, Github::Constants::CACHE_CONTROL, Github::Constants::CONTENT_LENGTH, Github::Constants::CONTENT_TYPE, Github::Constants::DATE, Github::Constants::ETAG, Github::Constants::HEADER_LAST, Github::Constants::HEADER_LINK, Github::Constants::HEADER_NEXT, Github::Constants::LOCATION, Github::Constants::META_FIRST, Github::Constants::META_LAST, Github::Constants::META_NEXT, Github::Constants::META_PREV, Github::Constants::META_REL, Github::Constants::OAUTH_SCOPES, Github::Constants::PARAM_PAGE, Github::Constants::PARAM_PER_PAGE, Github::Constants::PARAM_START_PAGE, Github::Constants::RATELIMIT_LIMIT, Github::Constants::RATELIMIT_REMAINING, Github::Constants::SERVER, Github::Constants::USER_AGENT

Instance Method Summary collapse

Instance Method Details

#caching?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/github_api/connection.rb', line 72

def caching?
  !@connection.nil?
end

#clear_cacheObject



68
69
70
# File 'lib/github_api/connection.rb', line 68

def clear_cache
  @connection = nil
end

#connection(options = {}) ⇒ Object

Creates http connection

Returns a Fraday::Connection object



92
93
94
95
96
97
98
99
100
101
# File 'lib/github_api/connection.rb', line 92

def connection(options = {})
  connection_options = default_options(options)
  clear_cache unless options.empty?
  connection_options.merge!(builder: stack(options))
  if ENV['DEBUG']
    p "Connection options : \n"
    pp connection_options
  end
  @connection ||= Faraday.new(connection_options)
end

#default_middleware(options = {}) ⇒ Object

Default middleware stack that uses default adapter as specified at configuration stage.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/github_api/connection.rb', line 46

def default_middleware(options = {})
  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 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
  end
end

#default_options(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/github_api/connection.rb', line 28

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     => user_agent
    },
    ssl: ssl,
    url: options.fetch(:endpoint) { Github.endpoint }
  }
end

#stack(options = {}, &block) ⇒ Object

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



79
80
81
82
83
84
85
86
87
# File 'lib/github_api/connection.rb', line 79

def stack(options = {}, &block)
  @stack ||= begin
    if block_given?
      Faraday::Builder.new(&block)
    else
      Faraday::Builder.new(&default_middleware(options))
    end
  end
end