From 3b754fd998741b230a9ac5e686178dcf86aaf219 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 11 Apr 2016 18:28:28 +0200 Subject: [PATCH 1/9] Adds two new fields to user --- db/migrate/20160411161531_add_genre_and_dob_to_users.rb | 6 ++++++ db/schema.rb | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20160411161531_add_genre_and_dob_to_users.rb diff --git a/db/migrate/20160411161531_add_genre_and_dob_to_users.rb b/db/migrate/20160411161531_add_genre_and_dob_to_users.rb new file mode 100644 index 000000000..aa8100fa3 --- /dev/null +++ b/db/migrate/20160411161531_add_genre_and_dob_to_users.rb @@ -0,0 +1,6 @@ +class AddGenreAndDobToUsers < ActiveRecord::Migration + def change + add_column :users, :genre, :string, index: true, limit: 10 + add_column :users, :date_of_birth, :datetime, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index e86f4db3b..cd05a0d2f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160406163649) do +ActiveRecord::Schema.define(version: 20160411161531) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -422,6 +422,8 @@ ActiveRecord::Schema.define(version: 20160406163649) do t.string "oauth_email" t.integer "geozone_id" t.string "redeemable_code" + t.string "genre", limit: 10 + t.datetime "date_of_birth" end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree From 9cc831bbc10873e8fc2e9384a5694f79d2b80082 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 11 Apr 2016 18:41:33 +0200 Subject: [PATCH 2/9] adds genre to the census api response --- lib/census_api.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/census_api.rb b/lib/census_api.rb index 737f7f873..9bcde62be 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -49,6 +49,10 @@ class CensusApi data[:datos_vivienda][:item][:codigo_distrito] end + def genre # "Varón" or "Mujer" + data[:datos_habitante][:item][:descripcion_sexo] + end + private def data @@ -86,7 +90,7 @@ class CensusApi 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", :codigo_distrito=>"01"}}}}} + {get_habita_datos_response: {get_habita_datos_return: {hay_errores: false, datos_habitante: { item: {fecha_nacimiento_string: "31-12-1980", identificador_documento: "12345678Z", description_sexo: "Varón" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}} end def is_dni?(document_type) From 622909703217b15b20f359319fcf6acf1d8ded91 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 12 Apr 2016 13:38:01 +0200 Subject: [PATCH 3/9] Fixes typo --- lib/census_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/census_api.rb b/lib/census_api.rb index 9bcde62be..c53e703fb 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -90,7 +90,7 @@ class CensusApi 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", description_sexo: "Varón" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}} + {get_habita_datos_response: {get_habita_datos_return: {hay_errores: false, datos_habitante: { item: {fecha_nacimiento_string: "31-12-1980", identificador_documento: "12345678Z", descripcion_sexo: "Varón" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}} end def is_dni?(document_type) From 2c51115d6d7a31742a468a8908e612d7efde04c9 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 12 Apr 2016 13:38:39 +0200 Subject: [PATCH 4/9] Residence Verification now fills up the date of birth and genre --- app/models/verification/residence.rb | 11 +++++++++++ spec/models/residence_spec.rb | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/verification/residence.rb b/app/models/verification/residence.rb index 0c1ed5f9a..1f5ce4bfd 100644 --- a/app/models/verification/residence.rb +++ b/app/models/verification/residence.rb @@ -31,6 +31,8 @@ class Verification::Residence user.update(document_number: document_number, document_type: document_type, geozone: self.geozone, + date_of_birth: date_of_birth.to_datetime, + genre: genre, residence_verified_at: Time.now) end @@ -75,6 +77,15 @@ class Verification::Residence @census_api_response.district_code end + def genre + case @census_api_response.genre + when "Varón" + "male" + when "Mujer" + "female" + end + end + private def call_census_api diff --git a/spec/models/residence_spec.rb b/spec/models/residence_spec.rb index fbc33d39c..faa81b5c3 100644 --- a/spec/models/residence_spec.rb +++ b/spec/models/residence_spec.rb @@ -85,7 +85,7 @@ describe Verification::Residence do describe "save" do - it "should store document number, document type, and geozone" do + it "should store document number, document type, geozone, date of birth and genre" do user = create(:user) residence.user = user residence.save @@ -93,6 +93,10 @@ describe Verification::Residence do user.reload expect(user.document_number).to eq('12345678Z') expect(user.document_type).to eq("1") + expect(user.date_of_birth.year).to eq(1980) + expect(user.date_of_birth.month).to eq(12) + expect(user.date_of_birth.day).to eq(31) + expect(user.genre).to eq('male') expect(user.geozone).to eq(geozone) end From 0ae97eada8b1401fd2e6b33e9f5c85cd369bcfc0 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 12 Apr 2016 17:06:50 +0200 Subject: [PATCH 5/9] adds rake task to assign demographic info to users --- lib/tasks/users.rake | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake index 0ce4cdfee..50f96e592 100644 --- a/lib/tasks/users.rake +++ b/lib/tasks/users.rake @@ -40,5 +40,23 @@ namespace :users do end end + desc "Associates demographic information to users" + task assign_demographic: :environment do + User.residence_verified.where(genre: nil).find_each do |u| + begin + response = CensusApi.new.call(u.document_type, u.document_number) + if response.valid? + u.genre = response.genre == 'Varón' ? 'male' : 'female' + u.date_of_birth = response.date_of_birth.to_datetime + u.save + print "." + else + print "X" + end + rescue + puts "Could not assign genre/dob for user: #{u.id}" + end + end + end end From 4f724ef3bfcc299082113d19755fb09657df0f3e Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 12 Apr 2016 18:18:25 +0200 Subject: [PATCH 6/9] 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 From 7987a3410b6a1c3249cfc4af2657b3a7a875bb5e Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 12 Apr 2016 18:19:05 +0200 Subject: [PATCH 7/9] Updates residence to handle CensusAPI's new response --- app/models/verification/residence.rb | 9 ++------- lib/active_model/dates.rb | 6 +----- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/app/models/verification/residence.rb b/app/models/verification/residence.rb index 1f5ce4bfd..661659a01 100644 --- a/app/models/verification/residence.rb +++ b/app/models/verification/residence.rb @@ -78,12 +78,7 @@ class Verification::Residence end def genre - case @census_api_response.genre - when "Varón" - "male" - when "Mujer" - "female" - end + @census_api_response.genre end private @@ -95,7 +90,7 @@ class Verification::Residence def residency_valid? @census_api_response.valid? && @census_api_response.postal_code == postal_code && - @census_api_response.date_of_birth == date_to_string(date_of_birth) + @census_api_response.date_of_birth == date_of_birth end def clean_document_number diff --git a/lib/active_model/dates.rb b/lib/active_model/dates.rb index a519e3840..5dd8e971c 100644 --- a/lib/active_model/dates.rb +++ b/lib/active_model/dates.rb @@ -13,13 +13,9 @@ module ActiveModel::Dates attrs.except("#{field}(1i)", "#{field}(2i)", "#{field}(3i)") end - def date_to_string(date) - date.strftime("%d-%m-%Y") - end - def string_to_date(value) day, month, year = value.split("-") Date.new(year.to_i, month.to_i, day.to_i) end -end \ No newline at end of file +end From 708b77721c06cc667580b1b6841f830bd86fa997 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 12 Apr 2016 18:22:38 +0200 Subject: [PATCH 8/9] aligns rake task with Census API response --- lib/tasks/users.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake index 50f96e592..18dad53bc 100644 --- a/lib/tasks/users.rake +++ b/lib/tasks/users.rake @@ -46,7 +46,7 @@ namespace :users do begin response = CensusApi.new.call(u.document_type, u.document_number) if response.valid? - u.genre = response.genre == 'Varón' ? 'male' : 'female' + u.genre = response.genre u.date_of_birth = response.date_of_birth.to_datetime u.save print "." From 3880ef9e24f9e136f4e6e430d098dcfea1873d7d Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 12 Apr 2016 18:35:23 +0200 Subject: [PATCH 9/9] aligns document verification with census API --- app/models/verification/management/document.rb | 4 ++-- lib/active_model/dates.rb | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/app/models/verification/management/document.rb b/app/models/verification/management/document.rb index ee9e5462d..481073397 100644 --- a/app/models/verification/management/document.rb +++ b/app/models/verification/management/document.rb @@ -32,7 +32,7 @@ class Verification::Management::Document end def under_sixteen?(response) - 16.years.ago < string_to_date(response.date_of_birth) + 16.years.ago < response.date_of_birth end def verified? @@ -43,4 +43,4 @@ class Verification::Management::Document user.update(verified_at: Time.now) if user? end -end \ No newline at end of file +end diff --git a/lib/active_model/dates.rb b/lib/active_model/dates.rb index 5dd8e971c..f9605ddbd 100644 --- a/lib/active_model/dates.rb +++ b/lib/active_model/dates.rb @@ -13,9 +13,4 @@ module ActiveModel::Dates attrs.except("#{field}(1i)", "#{field}(2i)", "#{field}(3i)") end - def string_to_date(value) - day, month, year = value.split("-") - Date.new(year.to_i, month.to_i, day.to_i) - end - end