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
# File lib/puppet_forge/connection.rb, line 59 def self.accept_language @accept_language end
# File lib/puppet_forge/connection.rb, line 54 def self.accept_language=(lang) lang = nil if lang.respond_to?(:empty?) && lang.empty? @accept_language = lang end
@param opts [Hash] Hash of connection options for Faraday
# File lib/puppet_forge/connection.rb, line 80 def default_connection(opts = {}) begin # Use Typhoeus if available. Gem::Specification.find_by_name('typhoeus', '~> 1.0.1') 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 100 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 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 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 50 def self.proxy @proxy end
# File lib/puppet_forge/connection.rb, line 45 def self.proxy=(url) url = nil if url.respond_to?(:empty?) && url.empty? @proxy = url end
@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 67 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
@param opts [Hash] Hash of connection options for Faraday
# File lib/puppet_forge/connection.rb, line 80 def default_connection(opts = {}) begin # Use Typhoeus if available. Gem::Specification.find_by_name('typhoeus', '~> 1.0.1') 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 100 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 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 PuppetForge::Middleware::SymbolifyJson builder.response(:json, :content_type => /\bjson$/) builder.response(:raise_error) builder.use(:connection_failure) builder.adapter(*adapter_args) end end