Files
nairobi/app/lib/census_api.rb
Javi Martín cb477149c4 Move lib folder inside the app folder
The purpose of the lib folder is to have code that doesn't necessary
belong in the application but can be shared with other applications.

However, we don't have other applications and, if we did, the way to
share code between them would be using a gem or even a git submodule.

So having both the `app/` and the `lib/` folders is confusing IMHO, and
it causes unnecessary problems with autoloading.

So we're moving the `lib/` folder to `app/lib/`. Originally, some of
these files were in the `app/services/` folder and then they were moved
to the `lib/` folder. We're using `app/lib/` instead of `app/services/`
so the upgrade is less confusing.

There's an exception, though. The `OmniAuth::Strategies::Wordpress`
class needs to be available in the Devise initializer. Since this is an
initializer and trying to autoload a class here will be problematic when
switching to Zeitwerk, we'll keep the `require` clause on top of the
Devise initializer in order to load the file and so it will be loaded
even if it isn't in the autoload paths anymore.
2024-04-11 19:08:01 +02:00

127 lines
3.3 KiB
Ruby

class CensusApi
include DocumentParser
def call(document_type, document_number)
response = nil
get_document_number_variants(document_type, document_number).each do |variant|
response = Response.new(get_response_body(document_type, variant))
return response if response.valid?
end
response
end
class Response
def initialize(body)
@body = body
end
def valid?
data[:datos_habitante][:item].present?
end
def date_of_birth
str = data[:datos_habitante][:item][:fecha_nacimiento_string]
day, month, year = str.match(/(\d\d?)\D(\d\d?)\D(\d\d\d?\d?)/)[1..3]
return nil if day.blank? || month.blank? || year.blank?
Time.zone.local(year.to_i, month.to_i, day.to_i).to_date
end
def postal_code
data[:datos_vivienda][:item][:codigo_postal]
end
def district_code
data[:datos_vivienda][:item][:codigo_distrito]
end
def gender
case data[:datos_habitante][:item][:descripcion_sexo]
when "Varón"
"male"
when "Mujer"
"female"
end
end
def name
"#{data[:datos_habitante][:item][:nombre]} #{data[:datos_habitante][:item][:apellido1]}"
end
private
def data
@body[:get_habita_datos_response][:get_habita_datos_return]
end
end
private
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(document_type, document_number)
end
end
def client
@client = Savon.client(wsdl: Rails.application.secrets.census_api_end_point)
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 end_point_defined?
Rails.application.secrets.census_api_end_point.present?
end
def end_point_available?
(Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?) && end_point_defined?
end
def stubbed_response(document_type, document_number)
if (document_number == "12345678Z" || document_number == "12345678Y") && document_type == "1"
stubbed_valid_response
else
stubbed_invalid_response
end
end
def stubbed_valid_response
{
get_habita_datos_response: {
get_habita_datos_return: {
datos_habitante: {
item: {
fecha_nacimiento_string: "31-12-1980",
identificador_documento: "12345678Z",
descripcion_sexo: "Varón",
nombre: "José",
apellido1: "García"
}
},
datos_vivienda: {
item: {
codigo_postal: "28013",
codigo_distrito: "01"
}
}
}
}
}
end
def stubbed_invalid_response
{ get_habita_datos_response: { get_habita_datos_return: { datos_habitante: {}, datos_vivienda: {}}}}
end
end