class OData4::Service::Response
The result of executing a OData4::Service::Request
.
Attributes
The query that generated the response (optional)
The underlying (raw) response
The service that generated this response
Public Class Methods
Create a new response given a service and a raw response. @param service [OData4::Service] The executing service. @param query [OData4::Query] The related query (optional). @param
# File lib/odata4/service/response.rb, line 23 def initialize(service, query = nil, &block) @service = service @query = query @timed_out = false execute(&block) end
Public Instance Methods
Returns the response body.
# File lib/odata4/service/response.rb, line 88 def body response.body end
Returns the content type of the resonse.
# File lib/odata4/service/response.rb, line 41 def content_type response.headers['Content-Type'] || '' end
Iterates over all entities in the response, using automatic paging if necessary. Provided for Enumerable functionality. @param block [block] a block to evaluate @return [OData4::Entity] each entity in turn for the query result
# File lib/odata4/service/response.rb, line 77 def each(&block) unless empty? process_results(&block) unless next_page.nil? # ensure request gets executed with the same options query.execute(URI.decode next_page_url).each(&block) end end end
Whether the response contained any entities. @return [Boolean]
# File lib/odata4/service/response.rb, line 63 def empty? @empty ||= find_entities.empty? end
# File lib/odata4/service/response.rb, line 45 def is_atom? content_type =~ /#{Regexp.escape OData4::Service::MIME_TYPES[:atom]}/ end
# File lib/odata4/service/response.rb, line 49 def is_json? content_type =~ /#{Regexp.escape OData4::Service::MIME_TYPES[:json]}/ end
# File lib/odata4/service/response.rb, line 53 def is_plain? content_type =~ /#{Regexp.escape OData4::Service::MIME_TYPES[:plain]}/ end
# File lib/odata4/service/response.rb, line 57 def is_xml? content_type =~ /#{Regexp.escape OData4::Service::MIME_TYPES[:xml]}/ end
Returns the HTTP status code.
# File lib/odata4/service/response.rb, line 31 def status response.status end
Whether the request was successful.
# File lib/odata4/service/response.rb, line 36 def success? 200 <= status && status < 300 end
Whether the response failed due to a timeout
# File lib/odata4/service/response.rb, line 68 def timed_out? @timed_out end
Validates the response. Throws an exception with an appropriate message if a 4xx or 5xx status code occured.
@return [self]
# File lib/odata4/service/response.rb, line 97 def validate_response! if error = OData4::Errors::ERROR_MAP[status] raise error, response, error_message end end
Private Instance Methods
# File lib/odata4/service/response.rb, line 123 def check_content_type # Dynamically extend instance with methods for # processing the current result type if is_atom? extend OData4::Service::Response::Atom elsif is_json? extend OData4::Service::Response::JSON elsif is_xml? extend OData4::Service::Response::XML elsif is_plain? extend OData4::Service::Response::Plain elsif response.body.empty? # Some services (*cough* Microsoft *cough*) return # an empty response with no `Content-Type` header set. # We catch that here and bypass content type detection. @empty = true else raise RequestError, response, "Invalid response type '#{content_type}'" end end
# File lib/odata4/service/response.rb, line 144 def entity_options if query query.entity_set.entity_options else { service_name: service.name, } end end
# File lib/odata4/service/response.rb, line 105 def execute(&block) @response = block.call logger.debug <<-EOS [OData4: #{service.name}] Received response: Headers: #{response.headers} Body: #{response.body} EOS check_content_type validate_response! rescue Faraday::TimeoutError logger.info "Request timed out." @timed_out = true end
# File lib/odata4/service/response.rb, line 119 def logger service.logger end
# File lib/odata4/service/response.rb, line 154 def process_results(&block) find_entities.each do |entity_data| entity = parse_entity(entity_data, entity_options) block_given? ? block.call(entity) : yield(entity) end end