Class: Decidim::ViaOberta::Api::Response
- Inherits:
-
Object
- Object
- Decidim::ViaOberta::Api::Response
- Defined in:
- app/services/decidim/via_oberta/api/response.rb
Constant Summary collapse
- RESULT_CODES =
{ "1" => "CONSTA", "2" => "NO CONSTA", "3" => "ERROR", "4" => "MUNICIPI NO ADHERIT" }.freeze
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #body ⇒ Object
- #code ⇒ Object
- #error ⇒ Object
-
#found? ⇒ Boolean
if the user is in the census.
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #raw_body ⇒ Object
- #resident_data ⇒ Object
- #result_code ⇒ Object
- #result_code_string ⇒ Object
- #slim_body ⇒ Object
-
#success? ⇒ Boolean
if no problems during the request.
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
13 14 15 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 13 def initialize(response) @response = response end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
17 18 19 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 17 def response @response end |
Instance Method Details
#body ⇒ Object
19 20 21 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 19 def body @body ||= Nokogiri::XML(response.body).remove_namespaces! end |
#code ⇒ Object
31 32 33 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 31 def code @code ||= slim_body.xpath("//CodigoEstado").text.presence || slim_body.xpath("//faultcode").text.presence || response.status end |
#error ⇒ Object
35 36 37 38 39 40 41 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 35 def error @error ||= if success? result_code_string else slim_body.xpath("//LiteralError").text.presence || slim_body.xpath("//faultstring").text.presence || body.xpath("//title").text end end |
#found? ⇒ Boolean
if the user is in the census
49 50 51 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 49 def found? result_code == RESULT_CODES.key("CONSTA") end |
#raw_body ⇒ Object
23 24 25 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 23 def raw_body @raw_body ||= response.body end |
#resident_data ⇒ Object
53 54 55 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 53 def resident_data @resident_data ||= slim_body.xpath("//respuestaResidenteMunicipio") end |
#result_code ⇒ Object
57 58 59 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 57 def result_code @result_code ||= resident_data.xpath("//codigoResultado").text end |
#result_code_string ⇒ Object
61 62 63 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 61 def result_code_string @result_code_string ||= RESULT_CODES[result_code] end |
#slim_body ⇒ Object
27 28 29 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 27 def slim_body @slim_body ||= body.search("Body").children end |
#success? ⇒ Boolean
if no problems during the request
44 45 46 |
# File 'app/services/decidim/via_oberta/api/response.rb', line 44 def success? slim_body.xpath("//LiteralError").text == "OK" end |