diff --git a/app/models/verification/residence.rb b/app/models/verification/residence.rb index 8650236ad..4bab08367 100644 --- a/app/models/verification/residence.rb +++ b/app/models/verification/residence.rb @@ -46,16 +46,12 @@ class Verification::Residence def residence_in_madrid return if errors.any? - self.date_of_birth = date_to_string(date_of_birth) - residency = CensusApi.new(self) - - unless residency.valid? + unless residency_valid? errors.add(:residence_in_madrid, false) store_failed_attempt Lock.increase_tries(user) end - self.date_of_birth = string_to_date(date_of_birth) end def store_failed_attempt @@ -70,6 +66,14 @@ class Verification::Residence private + def residency_valid? + response = CensusApi.new(document_type, document_number).call + + response.valid? && + response.postal_code == postal_code && + response.date_of_birth == date_to_string(date_of_birth) + end + def clean_document_number self.document_number = self.document_number.gsub(/[^a-z0-9]+/i, "").upcase unless self.document_number.blank? end diff --git a/lib/census_api.rb b/lib/census_api.rb index 30b321e96..953ea3ead 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -1,54 +1,69 @@ class CensusApi - attr_accessor :client, :citizen, :response + attr_accessor :document_type, :document_number - def initialize(citizen) - @citizen = citizen + def initialize(document_type, document_number) + @document_type = document_type + @document_number = document_number end - def client - @client = Savon.client(wsdl: Rails.application.secrets.census_api_end_point) + def call + Response.new(get_response_body) end - def response - return stubbed_response unless end_point_available? - client.call(:get_habita_datos, message: request).body + class Response + def initialize(body) + @body = body + end + + def date_of_birth + data[:datos_habitante][:item][:fecha_nacimiento_string] + end + + def postal_code + data[:datos_vivienda][:item][:codigo_postal] + end + + def valid? + data[:datos_habitante][:item].present? + end + + private + + def data + @body[:get_habita_datos_response][:get_habita_datos_return] + end end - def request - { request: - { codigo_institucion: Rails.application.secrets.census_api_institution_code, - codigo_portal: Rails.application.secrets.census_api_portal_name, - codigo_usuario: Rails.application.secrets.census_api_user_code, - documento: citizen.document_number, - tipo_documento: citizen.document_type, - codigo_idioma: 102, - nivel: 3 }} - end + private - def data - response[:get_habita_datos_response][:get_habita_datos_return] - end + def get_response_body + if end_point_available + client.call(:get_habita_datos, message: request).body + else + stubbed_response_body + end + end - def date_of_birth - data[:datos_habitante][:item][:fecha_nacimiento_string] - end + def client + @client = Savon.client(wsdl: Rails.application.secrets.census_api_end_point) + end - def postal_code - data[:datos_vivienda][:item][:codigo_postal] - end + def request + { request: + { codigo_institucion: Rails.application.secrets.census_api_institution_code, + codigo_portal: Rails.application.secrets.census_api_portal_name, + codigo_usuario: Rails.application.secrets.census_api_user_code, + documento: document_number, + tipo_documento: document_type, + codigo_idioma: 102, + nivel: 3 }} + end - def valid? - return false unless data[:datos_habitante][:item].present? + def end_point_available? + Rails.env.staging? || Rails.env.preproduction? || Rails.env.production? + end - citizen.date_of_birth == date_of_birth && - citizen.postal_code == postal_code - end - - def end_point_available? - Rails.env.staging? || Rails.env.preproduction? || Rails.env.production? - end - - def stubbed_response - {:get_habita_datos_response=>{:get_habita_datos_return=>{:hay_errores=>false, :datos_habitante=>{:item=>{:fecha_nacimiento_string=>"31-12-1980", :identificador_documento=>"12345678Z", }}, :datos_vivienda=>{:item=>{:codigo_postal=>"28013"}}}}} - end + def stubbed_response_body + {:get_habita_datos_response=>{:get_habita_datos_return=>{:hay_errores=>false, :datos_habitante=>{:item=>{:fecha_nacimiento_string=>"31-12-1980", :identificador_documento=>"12345678Z", }}, :datos_vivienda=>{:item=>{:codigo_postal=>"28013"}}}}} + end end