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:
kikito
2015-09-29 20:18:52 +02:00
parent 39119b9d6b
commit 1c372328d9
2 changed files with 64 additions and 45 deletions

View File

@@ -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

View File

@@ -1,54 +1,69 @@
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 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 end
def request private
{ 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 def get_response_body
response[:get_habita_datos_response][:get_habita_datos_return] if end_point_available
end client.call(:get_habita_datos, message: request).body
else
stubbed_response_body
end
end
def date_of_birth def client
data[:datos_habitante][:item][:fecha_nacimiento_string] @client = Savon.client(wsdl: Rails.application.secrets.census_api_end_point)
end end
def postal_code def request
data[:datos_vivienda][:item][:codigo_postal] { request:
end { 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? def end_point_available?
return false unless data[:datos_habitante][:item].present? Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
end
citizen.date_of_birth == date_of_birth && def stubbed_response_body
citizen.postal_code == postal_code {: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
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
end end