From 4f724ef3bfcc299082113d19755fb09657df0f3e Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 12 Apr 2016 18:18:25 +0200 Subject: [PATCH] Modifies census_api to parse genre and dob --- lib/census_api.rb | 12 ++++++++++-- spec/lib/census_api_spec.rb | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/census_api.rb b/lib/census_api.rb index c53e703fb..2b8c7d7a2 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -38,7 +38,10 @@ class CensusApi end 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 def postal_code @@ -50,7 +53,12 @@ class CensusApi end 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 private diff --git a/spec/lib/census_api_spec.rb b/spec/lib/census_api_spec.rb index a49133d23..fb14f39c5 100644 --- a/spec/lib/census_api_spec.rb +++ b/spec/lib/census_api_spec.rb @@ -29,7 +29,7 @@ describe CensusApi do describe '#call' do 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 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") 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 it "returns the last failed response" do