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

View File

@@ -1,32 +1,18 @@
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
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]
class Response
def initialize(body)
@body = body
end
def date_of_birth
@@ -38,17 +24,46 @@ class CensusApi
end
def valid?
return false unless data[:datos_habitante][:item].present?
data[:datos_habitante][:item].present?
end
citizen.date_of_birth == date_of_birth &&
citizen.postal_code == postal_code
private
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
def end_point_available?
Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
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"}}}}}
end
end