Module: PuppetForge::Connection Private

Included in:
V3::Base
Defined in:
lib/puppet_forge/connection.rb,
lib/puppet_forge/connection/connection_failure.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Provide a common mixin for adding a HTTP connection to classes.

This module provides a common method for creating HTTP connections as well as reusing a single connection object between multiple classes. Including classes can invoke #conn to get a reasonably configured HTTP connection. Connection objects can be passed with the #conn= method.

Examples:

class HTTPThing
  include PuppetForge::Connection
end
thing = HTTPThing.new
thing.conn = thing.make_connection('https://non-standard-forge.site')

Defined Under Namespace

Classes: ConnectionFailure

Constant Summary collapse

AUTHORIZATION_TOKEN_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/^[a-f0-9]{64}$/
USER_AGENT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"#{PuppetForge.user_agent} PuppetForge.gem/#{PuppetForge::VERSION} Faraday/#{Faraday::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_PLATFORM})".strip

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#conn(reset_connection = nil, opts = {}) ⇒ Faraday::Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns An existing Faraday connection if one was already set, otherwise a new Faraday connection.

Parameters:

  • reset_connection (Boolean) (defaults to: nil)

    flag to create a new connection every time this is called

  • opts (Hash) (defaults to: {})

    Hash of connection options for Faraday

Returns:

  • (Faraday::Connection)

    An existing Faraday connection if one was already set, otherwise a new Faraday connection.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet_forge/connection.rb', line 68

def conn(reset_connection = nil, opts = {})
  new_auth = @conn && @conn.headers['Authorization'] != PuppetForge::Connection.authorization
  new_proxy = @conn && ((@conn.proxy.nil? && PuppetForge::Connection.proxy) || (@conn.proxy && @conn.proxy.uri.to_s != PuppetForge::Connection.proxy))
  new_lang = @conn && @conn.headers['Accept-Language'] != PuppetForge::Connection.accept_language

  if reset_connection || new_auth || new_proxy || new_lang
    default_connection(opts)
  else
    @conn ||= default_connection(opts)
  end
end

Class Method Details

.accept_languageObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



60
61
62
# File 'lib/puppet_forge/connection.rb', line 60

def self.accept_language
  @accept_language
end

.accept_language=(lang) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
58
# File 'lib/puppet_forge/connection.rb', line 55

def self.accept_language=(lang)
  lang = nil if lang.respond_to?(:empty?) && lang.empty?
  @accept_language = lang
end

.authorizationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
# File 'lib/puppet_forge/connection.rb', line 42

def self.authorization
  @authorization
end

.authorization=(token) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def self.authorization=(token)
  @authorization = token

  # RK-229 Specific Workaround
  # Capture instance specific proxy setting if defined.
  if defined?(PuppetForge::V3::Base)
    if old_conn = PuppetForge::V3::Base.instance_variable_get(:@conn)
      self.proxy = old_conn.proxy.uri.to_s if old_conn.proxy
    end
  end
end

.default_connection(opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • opts (Hash) (defaults to: {})

    Hash of connection options for Faraday



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet_forge/connection.rb', line 81

def default_connection(opts = {})

  begin
    # Use Typhoeus if available.
    Gem::Specification.find_by_name('typhoeus', '~> 1.4')
    require 'typhoeus/adapters/faraday'
    adapter = :typhoeus
  rescue Gem::LoadError
    adapter = Faraday.default_adapter
  end

  make_connection(PuppetForge.host, [adapter], opts)
end

.make_connection(url, adapter_args = nil, opts = {}) ⇒ Faraday::Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate a new Faraday connection for the given URL.

Parameters:

  • url (String)

    the base URL for this connection

  • opts (Hash) (defaults to: {})

    Hash of connection options for Faraday

Returns:

  • (Faraday::Connection)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/puppet_forge/connection.rb', line 101

def make_connection(url, adapter_args = nil, opts = {})
  adapter_args ||= [Faraday.default_adapter]
  options = { :headers => { :user_agent => USER_AGENT } }.merge(opts)

  if token = PuppetForge::Connection.authorization
    options[:headers][:authorization] = token =~ AUTHORIZATION_TOKEN_REGEX ? "Bearer #{token}" : token
  end

  if lang = PuppetForge::Connection.accept_language
    options[:headers]['Accept-Language'] = lang
  end

  if proxy = PuppetForge::Connection.proxy
    options[:proxy] = proxy
  end

  Faraday.new(url, options) do |builder|
    builder.use Faraday::FollowRedirects::Middleware
    builder.response(:json, :content_type => /\bjson$/, :parser_options => { :symbolize_names => true })
    builder.response(:raise_error)
    builder.use(:connection_failure)

    builder.adapter(*adapter_args)
  end
end

.proxyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
# File 'lib/puppet_forge/connection.rb', line 51

def self.proxy
  @proxy
end

.proxy=(url) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
49
# File 'lib/puppet_forge/connection.rb', line 46

def self.proxy=(url)
  url = nil if url.respond_to?(:empty?) && url.empty?
  @proxy = url
end