Sha256: d8350d1bb82a56f52b69d3ebb8e798ec45ac3ffdd7dc1e35da5a1f84f3085007

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module NxtHttpClient
  module ClientDsl
    def configure(opts = {}, &block)
      opts.each { |k, v| default_config.send(k, v) }
      default_config.tap { |d| block.call(d) }
      default_config
    end

    def before_fire(&block)
      @before_fire_callback = block
    end

    def before_fire_callback
      @before_fire_callback ||= dup_instance_variable_from_ancestor_chain(:@before_fire_callback)
    end

    def after_fire(&block)
      @after_fire_callback = block
    end

    def after_fire_callback
      @after_fire_callback ||= dup_instance_variable_from_ancestor_chain(:@after_fire_callback)
    end

    def default_config
      @default_config ||= dup_instance_variable_from_ancestor_chain(:@default_config) { DefaultConfig.new }
    end

    def register_response_handler(handler = nil, &block)
      @response_handler = handler
      @response_handler ||= dup_instance_variable_from_ancestor_chain(:@response_handler) { NxtHttpClient::ResponseHandler.new }
      @response_handler.configure(&block) if block_given?
      @response_handler
    end

    def response_handler
      @response_handler ||= dup_instance_variable_from_ancestor_chain(:@response_handler) { NxtHttpClient::ResponseHandler.new }
    end

    def client_ancestors
      ancestors.select { |ancestor| ancestor <= NxtHttpClient::Client }
    end

    def instance_variable_from_ancestor_chain(instance_variable_name)
      client = client_ancestors.find { |c| c.instance_variable_get(instance_variable_name) }

      client.instance_variable_get(instance_variable_name)
    end

    def dup_instance_variable_from_ancestor_chain(instance_variable_name)
      result = instance_variable_from_ancestor_chain(instance_variable_name).dup
      return result unless block_given?

      result || yield
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nxt_http_client-0.3.4 lib/nxt_http_client/client_dsl.rb
nxt_http_client-0.3.3 lib/nxt_http_client/client_dsl.rb
nxt_http_client-0.3.2 lib/nxt_http_client/client_dsl.rb
nxt_http_client-0.3.1 lib/nxt_http_client/client_dsl.rb
nxt_http_client-0.3.0 lib/nxt_http_client/client_dsl.rb