Class: Decidim::ViaOberta::Api::Response

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

Instance Method Summary collapse

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

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

#bodyObject



19
20
21
# File 'app/services/decidim/via_oberta/api/response.rb', line 19

def body
  @body ||= Nokogiri::XML(response.body).remove_namespaces!
end

#codeObject



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

#errorObject



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

Returns:

  • (Boolean)


49
50
51
# File 'app/services/decidim/via_oberta/api/response.rb', line 49

def found?
  result_code == RESULT_CODES.key("CONSTA")
end

#raw_bodyObject



23
24
25
# File 'app/services/decidim/via_oberta/api/response.rb', line 23

def raw_body
  @raw_body ||= response.body
end

#resident_dataObject



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_codeObject



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_stringObject



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_bodyObject



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

Returns:

  • (Boolean)


44
45
46
# File 'app/services/decidim/via_oberta/api/response.rb', line 44

def success?
  slim_body.xpath("//LiteralError").text == "OK"
end