Modifies census_api to parse genre and dob
This commit is contained in:
@@ -38,7 +38,10 @@ class CensusApi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def date_of_birth
|
def date_of_birth
|
||||||
data[:datos_habitante][:item][:fecha_nacimiento_string]
|
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 unless day.present? && month.present? && year.present?
|
||||||
|
Date.new(year.to_i, month.to_i, day.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
def postal_code
|
def postal_code
|
||||||
@@ -50,7 +53,12 @@ class CensusApi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def genre # "Varón" or "Mujer"
|
def genre # "Varón" or "Mujer"
|
||||||
data[:datos_habitante][:item][:descripcion_sexo]
|
case data[:datos_habitante][:item][:descripcion_sexo]
|
||||||
|
when "Varón"
|
||||||
|
"male"
|
||||||
|
when "Mujer"
|
||||||
|
"female"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ describe CensusApi do
|
|||||||
|
|
||||||
describe '#call' do
|
describe '#call' do
|
||||||
let(:invalid_body) { {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {}}}} }
|
let(:invalid_body) { {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {}}}} }
|
||||||
let(:valid_body){ {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {item: {fecha_nacimiento_string: "1/1/1980"}}}}} }
|
let(:valid_body){ {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {item: {fecha_nacimiento_string: "1-1-1980"}}}}} }
|
||||||
|
|
||||||
it "returns the response for the first valid variant" do
|
it "returns the response for the first valid variant" do
|
||||||
allow(api).to receive(:get_response_body).with(1, "00123456").and_return(invalid_body)
|
allow(api).to receive(:get_response_body).with(1, "00123456").and_return(invalid_body)
|
||||||
@@ -39,7 +39,7 @@ describe CensusApi do
|
|||||||
response = api.call(1, "123456")
|
response = api.call(1, "123456")
|
||||||
|
|
||||||
expect(response).to be_valid
|
expect(response).to be_valid
|
||||||
expect(response.date_of_birth).to eq('1/1/1980')
|
expect(response.date_of_birth).to eq(Date.new(1980,1,1))
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the last failed response" do
|
it "returns the last failed response" do
|
||||||
|
|||||||
Reference in New Issue
Block a user