Class: Europeana::API::Response
- Inherits:
-
Object
- Object
- Europeana::API::Response
- Defined in:
- lib/europeana/api/response.rb
Instance Attribute Summary collapse
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#faraday_response ⇒ Object
readonly
Returns the value of attribute faraday_response.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #detect_error_message ⇒ Object
-
#initialize(request, faraday_response) ⇒ Response
constructor
A new instance of Response.
- #validate! ⇒ Object
- #validate_endpoint_errors! ⇒ Object
- #validate_generic_errors! ⇒ Object
Constructor Details
#initialize(request, faraday_response) ⇒ Response
12 13 14 15 16 |
# File 'lib/europeana/api/response.rb', line 12 def initialize(request, faraday_response) @request = request @faraday_response = faraday_response @error_message = end |
Instance Attribute Details
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
5 6 7 |
# File 'lib/europeana/api/response.rb', line 5 def @error_message end |
#faraday_response ⇒ Object (readonly)
Returns the value of attribute faraday_response.
5 6 7 |
# File 'lib/europeana/api/response.rb', line 5 def faraday_response @faraday_response end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
5 6 7 |
# File 'lib/europeana/api/response.rb', line 5 def request @request end |
Instance Method Details
#detect_error_message ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/europeana/api/response.rb', line 37 def return nil unless (400..599).cover?(status) if body.is_a?(Hash) && body.key?(:error) body[:error] else Rack::Utils::HTTP_STATUS_CODES[status] end end |
#validate! ⇒ Object
18 19 20 21 22 |
# File 'lib/europeana/api/response.rb', line 18 def validate! return if body.is_a?(Hash) && body[:success] validate_endpoint_errors! validate_generic_errors! end |
#validate_endpoint_errors! ⇒ Object
24 25 26 27 28 |
# File 'lib/europeana/api/response.rb', line 24 def validate_endpoint_errors! (endpoint[:errors] || {}).each_pair do |error_pattern, exception_class| fail exception_class.new(faraday_response), if =~ Regexp.new(error_pattern) end end |
#validate_generic_errors! ⇒ Object
30 31 32 33 34 35 |
# File 'lib/europeana/api/response.rb', line 30 def validate_generic_errors! fail Errors::ResourceNotFoundError.new(faraday_response), if status == 404 fail Errors::MissingAPIKeyError.new(faraday_response), if status == 403 && =~ /No API key/ fail Errors::ClientError.new(faraday_response), if (400..499).cover?(status) fail Errors::ServerError.new(faraday_response), if (500..599).cover?(status) end |