Sha256: 785dfa561e807bfa980dad8733af1d24160e7db751f1c03df571ec0eb5fb0362

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require "forwardable"

module Alma
  class Response
    class StandardError < Alma::StandardError
    end

    extend ::Forwardable

    attr_reader :raw_response
    def_delegators :raw_response, :body, :success?, :response, :request

    def initialize(response)
      @raw_response = response
      # We could validate and throw an error here but currently a
      validate(response)
    end

    def loggable
      { uri: @raw_response&.request&.uri.to_s
      }.select { |k, v| !(v.nil? || v.empty?) }
    end

    def validate(response)
      if errors.first&.dig("errorCode") == "401136"
        message = "The requested item already exists."
        log = loggable.merge(response.parsed_response)

        raise Alma::BibRequest::ItemAlreadyExists.new(message, log)
      end

      if response.code != 200
        log = loggable.merge(response.parsed_response)
        raise StandardError.new("Invalid Response.", log)
      end
    end

    # Returns an array of errors
    def errors
      @raw_response.parsed_response&.dig("errorList", "error") || []
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
alma-0.5.1 lib/alma/response.rb
alma-0.5.0 lib/alma/response.rb
alma-0.4.2 lib/alma/response.rb
alma-0.4.1 lib/alma/response.rb
alma-0.4.0 lib/alma/response.rb
alma-0.3.3 lib/alma/response.rb
alma-0.3.2 lib/alma/response.rb
alma-0.3.1 lib/alma/response.rb