Sha256: 4e5dc1c9f66d1122b8c97faf83a964d4013562ad7435d0534f64369a29c13e32

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

module Aitch
  class Namespace
    def configure(&block)
      yield config
    end

    def config
      @config ||= Configuration.new
    end
    alias_method :configuration, :config

    def execute(request_method = nil, url = nil, data = {}, headers = {}, options = {}, &block)
      if block_given?
        dsl = DSL.new
        dsl.instance_eval(&block)
        args = dsl.to_h
      else
        args = {
          url: url,
          data: data,
          headers: headers,
          options: options
        }
      end

      args.merge!(
        request_method: request_method,
        options: config.to_h.merge(Utils.symbolize_keys(args[:options]))
      )

      Request.new(args).perform
    end

    def execute!(*args, &block)
      response = execute(*args, &block)
      raise response.error if response.error?
      response
    end

    %w[
      get
      post
      put
      patch
      delete
      options
      trace
      head
    ].each do |method_name|
      define_method(method_name) do |url = nil, data = {}, headers = {}, options = {}, &block|
        execute(method_name, url, data, headers, options, &block)
      end

      define_method("#{method_name}!") do |url = nil, data = {}, headers = {}, options = {}, &block|
        execute!(method_name, url, data, headers, options, &block)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aitch-0.5.0 lib/aitch/namespace.rb
aitch-0.4.1 lib/aitch/namespace.rb
aitch-0.4.0 lib/aitch/namespace.rb
aitch-0.3.0 lib/aitch/namespace.rb
aitch-0.2.1 lib/aitch/namespace.rb
aitch-0.2.0 lib/aitch/namespace.rb