Class: Opencellid::Response
- Inherits:
-
Object
- Object
- Opencellid::Response
- Defined in:
- lib/opencellid/response.rb
Overview
The class modelling all possible answers from the server. Also when the server is not reachable or encounters an error (e.g. a 500 response), the library will return a Response object, so that clients will not need to handle application errors and HTTP errors separately
Instance Attribute Summary collapse
-
#cell_id ⇒ Object
Returns the value of attribute cell_id.
-
#cells ⇒ Object
Returns the value of attribute cells.
-
#error ⇒ Object
Returns the value of attribute error.
-
#measure_id ⇒ Object
Returns the value of attribute measure_id.
-
#measures ⇒ Object
Returns the value of attribute measures.
-
#result ⇒ Object
Returns the value of attribute result.
Class Method Summary collapse
-
.from_xml(string) ⇒ Response
Parses the string containing the response as XML and returns the corresponding Response object.
Instance Method Summary collapse
-
#failed? ⇒ bool
Tells whether the response is a failure response or not.
-
#initialize(ok) ⇒ Response
constructor
A new instance of Response.
-
#ok? ⇒ bool
Tells whether the response is a successful response or not.
Constructor Details
#initialize(ok) ⇒ Response
Returns a new instance of Response.
16 17 18 |
# File 'lib/opencellid/response.rb', line 16 def initialize(ok) @ok = ok end |
Instance Attribute Details
#cell_id ⇒ Object
Returns the value of attribute cell_id.
12 13 14 |
# File 'lib/opencellid/response.rb', line 12 def cell_id @cell_id end |
#cells ⇒ Object
Returns the value of attribute cells.
12 13 14 |
# File 'lib/opencellid/response.rb', line 12 def cells @cells end |
#error ⇒ Object
Returns the value of attribute error.
12 13 14 |
# File 'lib/opencellid/response.rb', line 12 def error @error end |
#measure_id ⇒ Object
Returns the value of attribute measure_id.
12 13 14 |
# File 'lib/opencellid/response.rb', line 12 def measure_id @measure_id end |
#measures ⇒ Object
Returns the value of attribute measures.
12 13 14 |
# File 'lib/opencellid/response.rb', line 12 def measures @measures end |
#result ⇒ Object
Returns the value of attribute result.
12 13 14 |
# File 'lib/opencellid/response.rb', line 12 def result @result end |
Class Method Details
.from_xml(string) ⇒ Response
Parses the string containing the response as XML and returns the corresponding Response object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/opencellid/response.rb', line 35 def self.from_xml(string) doc = REXML::Document.new string raise RuntimeError, "Could not parse server response" unless doc and doc.root case doc.root.name when "rsp" response = Response.new(doc.root.attributes['stat'] == "ok") response.measures = [] if response.ok? response.cells= doc.root.elements.collect('cell') {|e| Cell.from_element e } response.cell_id = ::Opencellid.to_i_or_nil(doc.root.attributes['cellid']) response.measure_id = ::Opencellid.to_i_or_nil(doc.root.attributes['id']) res = doc.root.elements['res'] response.result = res.text if res else response.cells = [] response.error = Error.from_element doc.root.elements['err'] end when "measures" response = Response.new true response.measures = doc.root.elements.collect('measure') {|e| Measure.from_element e } response.cells = [] else raise RuntimeError, "The server response does not contain a valid response" end response end |
Instance Method Details
#failed? ⇒ bool
Tells whether the response is a failure response or not
28 29 30 |
# File 'lib/opencellid/response.rb', line 28 def failed? not @ok end |
#ok? ⇒ bool
Tells whether the response is a successful response or not
22 23 24 |
# File 'lib/opencellid/response.rb', line 22 def ok? @ok end |