module PuppetForge::Connection

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.

@example

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

@api private

Constants

USER_AGENT

Attributes

conn[W]

Public Class Methods

authorization() click to toggle source
# File lib/puppet_forge/connection.rb, line 41
def self.authorization
  @authorization
end
authorization=(token) click to toggle source
# File lib/puppet_forge/connection.rb, line 29
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 = {}) click to toggle source

@param opts [Hash] Hash of connection options for Faraday

# File lib/puppet_forge/connection.rb, line 69
def default_connection(opts = {})

  begin
    # Use Typhoeus if available.
    Gem::Specification.find_by_name('typhoeus', '~> 0.6')
    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 = {}) click to toggle source

Generate a new Faraday connection for the given URL.

@param url [String] the base URL for this connection @param opts [Hash] Hash of connection options for Faraday @return [Faraday::Connection]

# File lib/puppet_forge/connection.rb, line 89
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
  end

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

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

    builder.adapter(*adapter_args)
  end
end
proxy() click to toggle source
# File lib/puppet_forge/connection.rb, line 49
def self.proxy
  @proxy
end
proxy=(url) click to toggle source
# File lib/puppet_forge/connection.rb, line 45
def self.proxy=(url)
  @proxy = url
end

Public Instance Methods

conn(reset_connection = nil, opts = {}) click to toggle source

@param reset_connection [Boolean] flag to create a new connection every time this is called @param opts [Hash] Hash of connection options for Faraday @return [Faraday::Connection] An existing Faraday connection if one was

already set, otherwise a new Faraday connection.
# File lib/puppet_forge/connection.rb, line 57
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))

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

Private Instance Methods

default_connection(opts = {}) click to toggle source

@param opts [Hash] Hash of connection options for Faraday

# File lib/puppet_forge/connection.rb, line 69
def default_connection(opts = {})

  begin
    # Use Typhoeus if available.
    Gem::Specification.find_by_name('typhoeus', '~> 0.6')
    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 = {}) click to toggle source

Generate a new Faraday connection for the given URL.

@param url [String] the base URL for this connection @param opts [Hash] Hash of connection options for Faraday @return [Faraday::Connection]

# File lib/puppet_forge/connection.rb, line 89
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
  end

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

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

    builder.adapter(*adapter_args)
  end
end