Class: Alma::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/alma/response.rb

Defined Under Namespace

Classes: StandardError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



15
16
17
18
19
# File 'lib/alma/response.rb', line 15

def initialize(response)
  @raw_response = response
  @response = response.parsed_response
  validate(response)
end

Instance Attribute Details

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



12
13
14
# File 'lib/alma/response.rb', line 12

def raw_response
  @raw_response
end

Instance Method Details

#errorsObject

Returns an array of errors



53
54
55
# File 'lib/alma/response.rb', line 53

def errors
  @response&.dig("errorList", "error") || []
end

#loggableObject



21
22
23
24
# File 'lib/alma/response.rb', line 21

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

#managed_by_libraryObject



44
45
46
# File 'lib/alma/response.rb', line 44

def managed_by_library
  @response.fetch("managed_by_library", "")
end

#managed_by_library_codeObject



48
49
50
# File 'lib/alma/response.rb', line 48

def managed_by_library_code
  @response.fetch("managed_by_library_code", "")
end

#request_idObject



40
41
42
# File 'lib/alma/response.rb', line 40

def request_id
  @response.fetch("request_id", "")
end

#validate(response) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/alma/response.rb', line 26

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