Sha256: d004734b500089d10743ba3c449953571570e6ea6d0ec10a77f6c1860e99c718

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module ArchivesSpace
  class Request
    include HTTParty
    attr_reader :config, :headers, :method, :path, :options

    def default_headers(method = :get)
      headers = {
        delete: {},
        get: {},
        post: {
          "Content-Type" => "application/json",
          "Content-Length" => "nnnn"
        },
        put: {
          "Content-Type" => "application/json",
          "Content-Length" => "nnnn"
        }
      }
      headers[method]
    end

    def initialize(config, method = "GET", path = "", options = {})
      @config = config
      @method = method.downcase.to_sym
      @path = path.gsub(%r{^/+}, "")
      @options = options
      @options[:headers] =
        options[:headers] ? default_headers(@method).merge(options[:headers]) : default_headers(@method)
      @options[:headers]["User-Agent"] = "#{Client::NAME}/#{Client::VERSION}"
      @options[:verify] = config.verify_ssl
      @options[:timeout] = config.timeout
      @options[:query] = {} unless options.key? :query

      self.class.debug_output($stdout) if @config.debug

      base_uri = config.base_repo&.length&.positive? ? File.join(config.base_uri, config.base_repo) : config.base_uri
      self.class.base_uri base_uri
    end

    def execute
      self.class.send method, "/#{path}", options
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
archivesspace-client-0.4.1 lib/archivesspace/client/request.rb
archivesspace-client-0.4.0 lib/archivesspace/client/request.rb
archivesspace-client-0.3.0 lib/archivesspace/client/request.rb