Class: Alma::Response
- Inherits:
-
Object
- Object
- Alma::Response
- Extended by:
- Forwardable
- Defined in:
- lib/alma/response.rb
Defined Under Namespace
Classes: StandardError
Instance Attribute Summary collapse
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
Instance Method Summary collapse
-
#errors ⇒ Object
Returns an array of errors.
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #loggable ⇒ Object
- #validate(response) ⇒ Object
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 # We could validate and throw an error here but currently a validate(response) end |
Instance Attribute Details
#raw_response ⇒ Object (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
#errors ⇒ Object
Returns an array of errors
41 42 43 |
# File 'lib/alma/response.rb', line 41 def errors @raw_response.parsed_response&.dig("errorList", "error") || [] end |
#loggable ⇒ Object
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 |
#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" = "The requested item already exists." log = loggable.merge(response.parsed_response) raise Alma::BibRequest::ItemAlreadyExists.new(, log) end if response.code != 200 log = loggable.merge(response.parsed_response) raise StandardError.new("Invalid Response.", log) end end |