Class: OData4::Service::Response

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/odata4/service/response.rb,
lib/odata4/service/response/xml.rb,
lib/odata4/service/response/atom.rb,
lib/odata4/service/response/json.rb,
lib/odata4/service/response/plain.rb

Overview

The result of executing a OData4::Service::Request.

Defined Under Namespace

Modules: Atom, JSON, Plain, XML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, response, query = nil) ⇒ Response

Create a new response given a service and a raw response.

Parameters:



22
23
24
25
26
27
28
# File 'lib/odata4/service/response.rb', line 22

def initialize(service, response, query = nil)
  @service  = service
  @response = response
  @query    = query
  check_content_type
  validate!
end

Instance Attribute Details

#queryObject (readonly)

The query that generated the response (optional)



17
18
19
# File 'lib/odata4/service/response.rb', line 17

def query
  @query
end

#responseObject (readonly)

The underlying (raw) response



15
16
17
# File 'lib/odata4/service/response.rb', line 15

def response
  @response
end

#serviceObject (readonly)

The service that generated this response



13
14
15
# File 'lib/odata4/service/response.rb', line 13

def service
  @service
end

Instance Method Details

#bodyObject

Returns the response body.



88
89
90
# File 'lib/odata4/service/response.rb', line 88

def body
  response.body
end

#content_typeObject

Returns the content type of the resonse.



41
42
43
# File 'lib/odata4/service/response.rb', line 41

def content_type
  response.headers['Content-Type'] || ''
end

#each(&block) ⇒ OData4::Entity

Iterates over all entities in the response, using automatic paging if necessary. Provided for Enumerable functionality.

Parameters:

  • block (block)

    a block to evaluate

Returns:



77
78
79
80
81
82
83
84
85
# 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

#empty?Boolean

Whether the response contained any entities.

Returns:

  • (Boolean)


63
64
65
# File 'lib/odata4/service/response.rb', line 63

def empty?
  @empty ||= find_entities.empty?
end

#is_atom?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/odata4/service/response.rb', line 45

def is_atom?
  content_type =~ /#{Regexp.escape OData4::Service::MIME_TYPES[:atom]}/
end

#is_json?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/odata4/service/response.rb', line 49

def is_json?
  content_type =~ /#{Regexp.escape OData4::Service::MIME_TYPES[:json]}/
end

#is_plain?Boolean

Returns:

  • (Boolean)


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

def is_plain?
  content_type =~ /#{Regexp.escape OData4::Service::MIME_TYPES[:plain]}/
end

#is_xml?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/odata4/service/response.rb', line 57

def is_xml?
  content_type =~ /#{Regexp.escape OData4::Service::MIME_TYPES[:xml]}/
end

#statusObject

Returns the HTTP status code.



31
32
33
# File 'lib/odata4/service/response.rb', line 31

def status
  response.code
end

#success?Boolean

Whether the request was successful.

Returns:

  • (Boolean)


36
37
38
# File 'lib/odata4/service/response.rb', line 36

def success?
  200 <= status && status < 300
end

#timed_out?Boolean

Whether the response failed due to a timeout

Returns:

  • (Boolean)


68
69
70
# File 'lib/odata4/service/response.rb', line 68

def timed_out?
  response.timed_out?
end

#validate!self

Validates the response. Throws an exception with an appropriate message if a 4xx or 5xx status code occured.

Returns:

  • (self)


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/odata4/service/response.rb', line 97

def validate!
  raise "Bad Request. #{error_message(response)}" if response.code == 400
  raise "Access Denied" if response.code == 401
  raise "Forbidden" if response.code == 403
  raise "Not Found" if [0,404].include?(response.code)
  raise "Method Not Allowed" if response.code == 405
  raise "Not Acceptable" if response.code == 406
  raise "Request Entity Too Large" if response.code == 413
  raise "Internal Server Error" if response.code == 500
  raise "Service Unavailable" if response.code == 503
  self
end