Class: FrOData::Service::Response
- Inherits:
-
Object
- Object
- FrOData::Service::Response
- Includes:
- Enumerable
- Defined in:
- lib/frodata/service/response.rb,
lib/frodata/service/response/xml.rb,
lib/frodata/service/response/atom.rb,
lib/frodata/service/response/json.rb,
lib/frodata/service/response/plain.rb
Overview
The result of executing a FrOData::Service::Request.
Defined Under Namespace
Modules: Atom, JSON, Plain, XML
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
The query that generated the response (optional).
-
#response ⇒ Object
readonly
The underlying (raw) response.
-
#service ⇒ Object
readonly
The service that generated this response.
Instance Method Summary collapse
-
#body ⇒ Object
Returns the response body.
-
#content_type ⇒ Object
Returns the content type of the resonse.
-
#each(&block) ⇒ FrOData::Entity
Iterates over all entities in the response, using automatic paging if necessary.
-
#empty? ⇒ Boolean
Whether the response contained any entities.
-
#initialize(service, query = nil, &block) ⇒ Response
constructor
Create a new response given a service and a raw response.
- #is_atom? ⇒ Boolean
- #is_json? ⇒ Boolean
- #is_plain? ⇒ Boolean
- #is_xml? ⇒ Boolean
-
#status ⇒ Object
Returns the HTTP status code.
-
#success? ⇒ Boolean
Whether the request was successful.
-
#timed_out? ⇒ Boolean
Whether the response failed due to a timeout.
-
#validate_response! ⇒ self
Validates the response.
Constructor Details
#initialize(service, query = nil, &block) ⇒ Response
Create a new response given a service and a raw response.
23 24 25 26 27 28 |
# File 'lib/frodata/service/response.rb', line 23 def initialize(service, query = nil, &block) @service = service @query = query @timed_out = false execute(&block) end |
Instance Attribute Details
#query ⇒ Object (readonly)
The query that generated the response (optional)
17 18 19 |
# File 'lib/frodata/service/response.rb', line 17 def query @query end |
#response ⇒ Object (readonly)
The underlying (raw) response
15 16 17 |
# File 'lib/frodata/service/response.rb', line 15 def response @response end |
#service ⇒ Object (readonly)
The service that generated this response
13 14 15 |
# File 'lib/frodata/service/response.rb', line 13 def service @service end |
Instance Method Details
#body ⇒ Object
Returns the response body.
88 89 90 |
# File 'lib/frodata/service/response.rb', line 88 def body response.body end |
#content_type ⇒ Object
Returns the content type of the resonse.
41 42 43 |
# File 'lib/frodata/service/response.rb', line 41 def content_type response.headers['Content-Type'] || '' end |
#each(&block) ⇒ FrOData::Entity
Iterates over all entities in the response, using automatic paging if necessary. Provided for Enumerable functionality.
77 78 79 80 81 82 83 84 85 |
# File 'lib/frodata/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 |
#empty? ⇒ Boolean
Whether the response contained any entities.
63 64 65 |
# File 'lib/frodata/service/response.rb', line 63 def empty? @empty ||= find_entities.empty? end |
#is_atom? ⇒ Boolean
45 46 47 |
# File 'lib/frodata/service/response.rb', line 45 def is_atom? content_type =~ /#{Regexp.escape FrOData::Service::MIME_TYPES[:atom]}/ end |
#is_json? ⇒ Boolean
49 50 51 |
# File 'lib/frodata/service/response.rb', line 49 def is_json? content_type =~ /#{Regexp.escape FrOData::Service::MIME_TYPES[:json]}/ end |
#is_plain? ⇒ Boolean
53 54 55 |
# File 'lib/frodata/service/response.rb', line 53 def is_plain? content_type =~ /#{Regexp.escape FrOData::Service::MIME_TYPES[:plain]}/ end |
#is_xml? ⇒ Boolean
57 58 59 |
# File 'lib/frodata/service/response.rb', line 57 def is_xml? content_type =~ /#{Regexp.escape FrOData::Service::MIME_TYPES[:xml]}/ end |
#status ⇒ Object
Returns the HTTP status code.
31 32 33 |
# File 'lib/frodata/service/response.rb', line 31 def status response.status end |
#success? ⇒ Boolean
Whether the request was successful.
36 37 38 |
# File 'lib/frodata/service/response.rb', line 36 def success? 200 <= status && status < 300 end |
#timed_out? ⇒ Boolean
Whether the response failed due to a timeout
68 69 70 |
# File 'lib/frodata/service/response.rb', line 68 def timed_out? @timed_out end |
#validate_response! ⇒ self
Validates the response. Throws an exception with an appropriate message if a 4xx or 5xx status code occured.
97 98 99 100 101 |
# File 'lib/frodata/service/response.rb', line 97 def validate_response! if error = FrOData::Errors::ERROR_MAP[status] raise error.new(response, ) end end |