Sha256: d0948f9ba7a9fb3e6b35d0c7ef4df4defd2376f236cd1e3fb30979d070e465df

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true
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

3 entries across 3 versions & 1 rubygems

Version Path
aitch-1.0.2 lib/aitch/namespace.rb
aitch-1.0.1 lib/aitch/namespace.rb
aitch-1.0.0 lib/aitch/namespace.rb