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
Public Class Methods
@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
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
# File lib/puppet_forge/connection.rb, line 49 def self.proxy @proxy end
# File lib/puppet_forge/connection.rb, line 45 def self.proxy=(url) @proxy = url end
Public Instance Methods
@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
@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
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