Sha256: 9a3a75c099ccac4c15ec511f947f0eb103333fdcd8b34069e11f8698a171fcda

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require "sea_shanty/errors"
require "sea_shanty/configuration"
require "sea_shanty/request_store"
require "sea_shanty/version"

module SeaShanty
  TRUE_VALUES = %w[1 yes y true]

  module_function

  def configuration
    @configuration ||= Configuration.new
  end

  def configure(&block)
    yield configuration

    configuration.bypass = configuration_overwrite(ENV["SEA_SHANTY_BYPASS"], configuration.bypass)
    configuration.readonly = configuration_overwrite(ENV["SEA_SHANTY_READONLY"], configuration.readonly)
  end

  def intercept(identifier)
    return if intercepting.include?(identifier)
    interceptors.fetch(identifier).intercept!(request_store)
    intercepting << identifier
  rescue KeyError
    raise(
      UnknownInterceptor,
      "Cannot find an interceptor for #{identifier}. Available interceptors are: [#{interceptors.keys.join(", ")}]"
    )
  end

  def intercepting
    @intercepting ||= []
  end

  def interceptors
    @interceptors ||= {}
  end

  def register_interceptor(identifier, interceptor)
    interceptors[identifier] = interceptor
  end

  def remove(identifier)
    return unless intercepting.include?(identifier)
    interceptors.fetch(identifier).remove
    intercepting.delete(identifier)
  rescue KeyError
    raise(
      UnknownInterceptor,
      "Cannot find an interceptor for #{identifier}. Available interceptors are: [#{interceptors.keys.join(", ")}]"
    )
  end

  def request_store
    RequestStore.new(configuration)
  end

  def reset!
    @configuration = Configuration.new
    configure {}
  end

  def configuration_overwrite(env_var, value)
    if env_var.nil? || env_var.empty?
      value
    else
      TRUE_VALUES.include?(env_var.downcase)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sea_shanty-0.2.0 lib/sea_shanty.rb