Refactor in residence verification + census api
Needed for a new census api verification using only the document type (no date of birth and no postal code)
This commit is contained in:
@@ -46,16 +46,12 @@ class Verification::Residence
|
|||||||
|
|
||||||
def residence_in_madrid
|
def residence_in_madrid
|
||||||
return if errors.any?
|
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)
|
errors.add(:residence_in_madrid, false)
|
||||||
store_failed_attempt
|
store_failed_attempt
|
||||||
Lock.increase_tries(user)
|
Lock.increase_tries(user)
|
||||||
end
|
end
|
||||||
self.date_of_birth = string_to_date(date_of_birth)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_failed_attempt
|
def store_failed_attempt
|
||||||
@@ -70,6 +66,14 @@ class Verification::Residence
|
|||||||
|
|
||||||
private
|
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
|
def clean_document_number
|
||||||
self.document_number = self.document_number.gsub(/[^a-z0-9]+/i, "").upcase unless self.document_number.blank?
|
self.document_number = self.document_number.gsub(/[^a-z0-9]+/i, "").upcase unless self.document_number.blank?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,32 +1,18 @@
|
|||||||
class CensusApi
|
class CensusApi
|
||||||
attr_accessor :client, :citizen, :response
|
attr_accessor :document_type, :document_number
|
||||||
|
|
||||||
def initialize(citizen)
|
def initialize(document_type, document_number)
|
||||||
@citizen = citizen
|
@document_type = document_type
|
||||||
|
@document_number = document_number
|
||||||
end
|
end
|
||||||
|
|
||||||
def client
|
def call
|
||||||
@client = Savon.client(wsdl: Rails.application.secrets.census_api_end_point)
|
Response.new(get_response_body)
|
||||||
end
|
end
|
||||||
|
|
||||||
def response
|
class Response
|
||||||
return stubbed_response unless end_point_available?
|
def initialize(body)
|
||||||
client.call(:get_habita_datos, message: request).body
|
@body = body
|
||||||
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
|
|
||||||
|
|
||||||
def data
|
|
||||||
response[:get_habita_datos_response][:get_habita_datos_return]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def date_of_birth
|
def date_of_birth
|
||||||
@@ -38,17 +24,46 @@ class CensusApi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
return false unless data[:datos_habitante][:item].present?
|
data[:datos_habitante][:item].present?
|
||||||
|
end
|
||||||
|
|
||||||
citizen.date_of_birth == date_of_birth &&
|
private
|
||||||
citizen.postal_code == postal_code
|
|
||||||
|
def data
|
||||||
|
@body[:get_habita_datos_response][:get_habita_datos_return]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def get_response_body
|
||||||
|
if end_point_available
|
||||||
|
client.call(:get_habita_datos, message: request).body
|
||||||
|
else
|
||||||
|
stubbed_response_body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def client
|
||||||
|
@client = Savon.client(wsdl: Rails.application.secrets.census_api_end_point)
|
||||||
|
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
|
end
|
||||||
|
|
||||||
def end_point_available?
|
def end_point_available?
|
||||||
Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
|
Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
|
||||||
end
|
end
|
||||||
|
|
||||||
def stubbed_response
|
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"}}}}}
|
{: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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user