Merge pull request #1057 from consul/demographic-data

Add demographic data to users
This commit is contained in:
Juanjo Bazán
2016-04-12 19:03:57 +02:00
9 changed files with 58 additions and 19 deletions

View File

@@ -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
end

View File

@@ -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,10 @@ class Verification::Residence
@census_api_response.district_code
end
def genre
@census_api_response.genre
end
private
def call_census_api
@@ -84,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

View File

@@ -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

View File

@@ -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

View File

@@ -13,13 +13,4 @@ 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
end

View File

@@ -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
@@ -49,6 +52,15 @@ class CensusApi
data[:datos_vivienda][:item][:codigo_distrito]
end
def genre # "Varón" or "Mujer"
case data[:datos_habitante][:item][:descripcion_sexo]
when "Varón"
"male"
when "Mujer"
"female"
end
end
private
def data
@@ -86,7 +98,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", descripcion_sexo: "Varón" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}}
end
def is_dni?(document_type)

View File

@@ -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
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

View File

@@ -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

View File

@@ -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