Module: PuppetForge::Connection Private

Included in:
V3::Module, V3::ModuleRelease
Defined in:
lib/shared/puppet_forge/connection.rb,
lib/shared/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

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/#{PuppetForge::VERSION} Faraday/#{Faraday::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_PLATFORM})"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connFaraday::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.

Returns:

  • (Faraday::Connection)

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



39
40
41
# File 'lib/shared/puppet_forge/connection.rb', line 39

def conn
  @conn ||= make_connection('https://forgeapi.puppetlabs.com')
end

Class Method Details

.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.



33
34
35
# File 'lib/shared/puppet_forge/connection.rb', line 33

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.



29
30
31
# File 'lib/shared/puppet_forge/connection.rb', line 29

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

Instance Method Details

#make_connection(url, adapter_args = nil) ⇒ 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

Returns:

  • (Faraday::Connection)


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

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

  if token = PuppetForge::Connection.authorization
    options[:headers][:authorization] = token
  end

  Faraday.new(url, options) do |builder|
    builder.response(:json, :content_type => /\bjson$/)
    builder.response(:raise_error)
    builder.use(:connection_failure)

    builder.adapter(*adapter_args)
  end
end