Class: Opencellid::Response

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ok) ⇒ Response

Returns a new instance of Response.

Parameters:

  • ok (bool)

    whether the response is an ok response or a failure one



16
17
18
# File 'lib/opencellid/response.rb', line 16

def initialize(ok)
  @ok = ok
end

Instance Attribute Details

#cell_idObject

Returns the value of attribute cell_id.



12
13
14
# File 'lib/opencellid/response.rb', line 12

def cell_id
  @cell_id
end

#cellsObject

Returns the value of attribute cells.



12
13
14
# File 'lib/opencellid/response.rb', line 12

def cells
  @cells
end

#errorObject

Returns the value of attribute error.



12
13
14
# File 'lib/opencellid/response.rb', line 12

def error
  @error
end

#measure_idObject

Returns the value of attribute measure_id.



12
13
14
# File 'lib/opencellid/response.rb', line 12

def measure_id
  @measure_id
end

#measuresObject

Returns the value of attribute measures.



12
13
14
# File 'lib/opencellid/response.rb', line 12

def measures
  @measures
end

#resultObject

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

Parameters:

  • string (String)

    a string containing the XML response received from the server

Returns:

  • (Response)

    the Response object obtained by parsing the XML file

Raises:

  • (RuntimeError)


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

Returns:

  • (bool)

    ‘true` if the response is a failure response



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

Returns:

  • (bool)

    ‘true` if the response is a successful response



22
23
24
# File 'lib/opencellid/response.rb', line 22

def ok?
  @ok
end