module We::Call::Connection
Constants
- DEFAULT_ADAPTER
- DEFAULT_ADAPTER_CLASS
We
use typhoeus instead of default NetHTTP so we can control how many retries are made github.com/lostisland/faraday/issues/612- DEFAULT_RETRY_OPTIONS
- OPEN_TIMEOUT
If your network isn't stable enough to get a sign of life in 1s then you should look into that Or override this default on creating the connection.
- QueryableBuilder
Attributes
Public Instance Methods
@param [Object] host @param [Integer] timeout @param [Integer] open_timeout
@param [String] app @param [String] env @yieldparam [Faraday::Connection] Faraday connection object is yielded to a block
# File lib/we/call/connection.rb, line 49 def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: guess_env, retry_options: {}, &block) @host = host @retry_options = retry_options @app = app or raise_missing_app! @env = env or raise_missing_env! @timeout = timeout or raise_missing_timeout! @open_timeout = open_timeout or raise_missing_open_timeout! create(&block) end
Private Instance Methods
@return [Boolean] Does the adapter handle gzip automatically or not github.com/lostisland/faraday_middleware/blob/master/lib/faraday_middleware/gzip.rb#L9
# File lib/we/call/connection.rb, line 118 def adapter_handles_gzip?(adapter) [:em_http, :net_http, :net_http_persistent].include?(adapter) end
# File lib/we/call/connection.rb, line 96 def config We::Call.configuration end
@return [Faraday::Connection] Preconfigured Faraday Connection
object, for hitting get, post, etc.
# File lib/we/call/connection.rb, line 64 def create builder = QueryableBuilder.new(&Proc.new { |_| }) headers = { 'User-Agent' => app, config.app_name_header => app, config.app_env_header => env, } request = { timeout: timeout, open_timeout: open_timeout } Faraday.new(host, builder: builder, headers: headers, request: request) do |faraday| if config.detect_deprecations faraday.response :sunset, setup_sunset_middleware(faraday) end if should_retry? faraday.request :retry, fetch_retry_options end yield faraday if block_given? unless adapter_handles_gzip?(faraday.builder.get_adapter) faraday.use :gzip end faraday.adapter DEFAULT_ADAPTER unless faraday.builder.adapter? end end
# File lib/we/call/connection.rb, line 135 def fetch_retry_options client_options = retry_options.any? ? retry_options : config.retry_options DEFAULT_RETRY_OPTIONS.merge(client_options) do |key, default_val, new_val| if key == :exceptions default_val + Array(new_val) else new_val end end end
@return [String] Check for config.app_name, or detect name from Rails application
# File lib/we/call/connection.rb, line 154 def guess_app return config.app_name if config.app_name ENV['APP_NAME'] || rails_app_name end
@return [String] Environment (usually 'development', 'staging', 'production', etc.)
# File lib/we/call/connection.rb, line 148 def guess_env return config.app_env if config.app_env ENV['RACK_ENV'] || rails_app_env end
# File lib/we/call/connection.rb, line 159 def rails_app_env ::Rails.env if (defined? ::Rails) end
# File lib/we/call/connection.rb, line 163 def rails_app_name if (defined? ::Rails) && !::Rails.application.nil? ::Rails.application.class.name.deconstantize.underscore.dasherize end end
# File lib/we/call/connection.rb, line 100 def raise_missing_app! raise MissingApp, 'app must be set, e.g: pokedex' end
# File lib/we/call/connection.rb, line 104 def raise_missing_env! raise MissingEnv, 'env must be set, e.g: staging' end
# File lib/we/call/connection.rb, line 112 def raise_missing_open_timeout! raise MissingOpenTimeout, 'open_timeout must be set, and defaults to 1 second. This is the time until a connection is established with another server, and after 1 sec it\'s probably not there.' end
# File lib/we/call/connection.rb, line 108 def raise_missing_timeout! raise MissingTimeout, 'timeout must be set, maybe 5 (seconds) would be a good value. This is the open & read timeout, a.k.a max response time.' end
# File lib/we/call/connection.rb, line 122 def setup_sunset_middleware(faraday) options = { rollbar: :auto, active_support: :auto } # Pass something that might be a logger or anything with a warn method if config.detect_deprecations.respond_to?(:warn) options = options.merge({ logger: config.detect_deprecations }) end options end
# File lib/we/call/connection.rb, line 131 def should_retry? retry_options.any? || config.retry end