Merge pull request #574 from AyuntamientoMadrid/management-census-api-mod

Refactor in residence verification + census api
This commit is contained in:
Juanjo Bazán
2015-09-30 12:06:51 +02:00
2 changed files with 60 additions and 47 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.call(document_type, document_number)
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,54 +1,63 @@
class CensusApi
attr_accessor :client, :citizen, :response
def initialize(citizen)
@citizen = citizen
def call(document_type, document_number)
Response.new(get_response_body(document_type, document_number))
end
def client
@client = Savon.client(wsdl: Rails.application.secrets.census_api_end_point)
class Response
def initialize(body)
@body = body
end
def valid?
data[:datos_habitante][:item].present?
end
def date_of_birth
data[:datos_habitante][:item][:fecha_nacimiento_string]
end
def postal_code
data[:datos_vivienda][:item][:codigo_postal]
end
private
def data
@body[:get_habita_datos_response][:get_habita_datos_return]
end
end
def response
return stubbed_response unless end_point_available?
client.call(:get_habita_datos, message: request).body
end
private
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 get_response_body(document_type, document_number)
if end_point_available?
client.call(:get_habita_datos, message: request(document_type, document_number)).body
else
stubbed_response_body
end
end
def data
response[:get_habita_datos_response][:get_habita_datos_return]
end
def client
@client = Savon.client(wsdl: Rails.application.secrets.census_api_end_point)
end
def date_of_birth
data[:datos_habitante][:item][:fecha_nacimiento_string]
end
def request(document_type, document_number)
{ 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 postal_code
data[:datos_vivienda][:item][:codigo_postal]
end
def end_point_available?
Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
end
def valid?
return false unless data[:datos_habitante][:item].present?
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