Class: FrOData::Service::Response

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(service, query = nil, &block) ⇒ Response

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

Parameters:



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

#queryObject (readonly)

The query that generated the response (optional)



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

def query
  @query
end

#responseObject (readonly)

The underlying (raw) response



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

def response
  @response
end

#serviceObject (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

#bodyObject

Returns the response body.



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

def body
  response.body
end

#content_typeObject

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.

Parameters:

  • block (block)

    a block to evaluate

Returns:



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.

Returns:

  • (Boolean)


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

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

#is_atom?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

#statusObject

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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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.

Returns:

  • (self)


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, error_message)
  end
end