From 0dcc17609848ae506dc2f57d6ba2506ae21a7bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sat, 19 Dec 2015 10:21:53 +0100 Subject: [PATCH 1/4] removes before_filter (deprecated in favor of before_action) --- app/controllers/moderation/proposals_controller.rb | 2 +- app/controllers/moderation/users_controller.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/moderation/proposals_controller.rb b/app/controllers/moderation/proposals_controller.rb index 7d48a7c07..4883bd378 100644 --- a/app/controllers/moderation/proposals_controller.rb +++ b/app/controllers/moderation/proposals_controller.rb @@ -4,7 +4,7 @@ class Moderation::ProposalsController < Moderation::BaseController has_filters %w{pending_flag_review all with_ignored_flag}, only: :index has_orders %w{flags created_at}, only: :index - before_filter :load_resources, only: [:index, :moderate] + before_action :load_resources, only: [:index, :moderate] load_and_authorize_resource diff --git a/app/controllers/moderation/users_controller.rb b/app/controllers/moderation/users_controller.rb index fc8b4488e..6ff22f94a 100644 --- a/app/controllers/moderation/users_controller.rb +++ b/app/controllers/moderation/users_controller.rb @@ -1,6 +1,6 @@ class Moderation::UsersController < Moderation::BaseController - before_filter :load_users, only: :index + before_action :load_users, only: :index load_and_authorize_resource From a83b14823f640eca6a3b43780988c5b01b86e035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sun, 20 Dec 2015 22:39:14 +0100 Subject: [PATCH 2/4] updates dependencies --- Gemfile.lock | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index bdee0b060..995cb76e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -65,7 +65,7 @@ GEM ancestry (2.1.0) activerecord (>= 3.0.0) arel (6.0.3) - ast (2.1.0) + ast (2.2.0) bcrypt (3.1.10) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) @@ -89,7 +89,7 @@ GEM capistrano-rvm (0.1.2) capistrano (~> 3.0) sshkit (~> 1.2) - capistrano3-delayed-job (1.4.2) + capistrano3-delayed-job (1.5.0) capistrano (>= 3.0.0) capybara (2.5.0) mime-types (>= 1.16) @@ -106,9 +106,9 @@ GEM cliver (0.3.2) cocaine (0.5.8) climate_control (>= 0.0.3, < 1.0) - coffee-rails (4.1.0) + coffee-rails (4.1.1) coffee-script (>= 2.2.0) - railties (>= 4.0.0, < 5.0) + railties (>= 4.0.0, < 5.1.x) coffee-script (2.4.1) coffee-script-source execjs @@ -244,9 +244,9 @@ GEM multi_json (~> 1.3) multi_xml (~> 0.5) rack (~> 1.2) - omniauth (1.2.2) + omniauth (1.3.1) hashie (>= 1.2, < 4) - rack (~> 1.0) + rack (>= 1.0, < 3) omniauth-facebook (3.0.0) omniauth-oauth2 (~> 1.2) omniauth-google-oauth2 (0.2.10) @@ -315,8 +315,8 @@ GEM redcarpet (3.3.3) referer-parser (0.3.0) request_store (1.2.1) - responders (2.1.0) - railties (>= 4.2.0, < 5) + responders (2.1.1) + railties (>= 4.2.0, < 5.1) rest-client (1.8.0) http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 3.0) From 57e72206d2bb6f7c6c795a1faffbfdb59ed6c876 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 21 Dec 2015 17:07:48 +0100 Subject: [PATCH 3/4] makes the census api try several variants before failing a verification --- lib/census_api.rb | 53 ++++++++++++++++++++++++++++++++++- spec/lib/census_api_spec.rb | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 spec/lib/census_api_spec.rb diff --git a/lib/census_api.rb b/lib/census_api.rb index 7078fa828..0eb3ee35d 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -1,7 +1,54 @@ class CensusApi def call(document_type, document_number) - Response.new(get_response_body(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 + + def get_document_number_variants(document_type, document_number) + # Delete all non-alphanumerics + document_number = document_number.to_s.gsub(/[^0-9A-Za-z]/i, '') + variants = [] + + if is_dni?(document_type) + # If a letter exists at the end, delete it and put it on the letter var + letter = document_number.last + if letter[/[A-Za-z]/] == letter + document_number = document_number[0..-2] + else + letter = nil + end + + document_number = document_number.last(8) # Keep only the last 7 digits + document_number = document_number.gsub(/^0+/, '') # Removes leading zeros + + # if the number has less than 7 digits, pad with zeros to the left and add each variant to the list + # For example, if the initial document_number is 1234, possible numbers should have + # [1234, 01234, 001234, 0001234] + possible_numbers = [] + possible_numbers << document_number unless document_number.blank? + while document_number.size < 8 + document_number = "0#{document_number}" + possible_numbers << document_number + end + + variants += possible_numbers + + # if a letter was given, try the numbers followed by the letter in upper and lowercase + if letter.present? then + possible_numbers.each do |number| + variants << number + letter.downcase << number + letter.upcase + end + end + else # not a DNI + variants << document_number + end + + variants end class Response @@ -60,4 +107,8 @@ class CensusApi 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"}}}}} end + + def is_dni?(document_type) + document_type.to_s == "1" + end end diff --git a/spec/lib/census_api_spec.rb b/spec/lib/census_api_spec.rb new file mode 100644 index 000000000..a49133d23 --- /dev/null +++ b/spec/lib/census_api_spec.rb @@ -0,0 +1,55 @@ +require 'rails_helper' + +describe CensusApi do + let(:api) { described_class.new } + + describe '#get_document_number_variants' do + it "trims and cleans up entry" do + expect(api.get_document_number_variants(2, ' 1 2@ 34')).to eq(['1234']) + end + + it "returns only one try for passports & residence cards" do + expect(api.get_document_number_variants(2, '1234')).to eq(['1234']) + expect(api.get_document_number_variants(3, '1234')).to eq(['1234']) + end + + it 'takes only the last 8 digits for dnis and resicence cards' do + expect(api.get_document_number_variants(1, '543212345678')).to eq(['12345678']) + end + + it 'tries all the dni variants padding with zeroes' do + expect(api.get_document_number_variants(1, '0123456')).to eq(['123456', '0123456', '00123456']) + expect(api.get_document_number_variants(1, '00123456')).to eq(['123456', '0123456', '00123456']) + end + + it 'adds upper and lowercase letter when the letter is present' do + expect(api.get_document_number_variants(1, '1234567A')).to eq(['1234567', '01234567', '1234567a', '1234567A', '01234567a', '01234567A']) + end + end + + 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"}}}}} } + + 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, "123456").and_return(invalid_body) + expect(api).to receive(:get_response_body).with(1, "0123456").and_return(valid_body) + + response = api.call(1, "123456") + + expect(response).to be_valid + expect(response.date_of_birth).to eq('1/1/1980') + end + + it "returns the last failed response" do + expect(api).to receive(:get_response_body).with(1, "00123456").and_return(invalid_body) + expect(api).to receive(:get_response_body).with(1, "123456").and_return(invalid_body) + expect(api).to receive(:get_response_body).with(1, "0123456").and_return(invalid_body) + response = api.call(1, "123456") + + expect(response).to_not be_valid + end + end + +end From d8e6584bfecb2a7477a0a28cf6833848a3ec71b9 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 21 Dec 2015 17:10:07 +0100 Subject: [PATCH 4/4] fixes comments --- lib/census_api.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/census_api.rb b/lib/census_api.rb index 0eb3ee35d..19381c16a 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -23,10 +23,10 @@ class CensusApi letter = nil end - document_number = document_number.last(8) # Keep only the last 7 digits + document_number = document_number.last(8) # Keep only the last x digits document_number = document_number.gsub(/^0+/, '') # Removes leading zeros - # if the number has less than 7 digits, pad with zeros to the left and add each variant to the list + # if the number has less digits than it should, pad with zeros to the left and add each variant to the list # For example, if the initial document_number is 1234, possible numbers should have # [1234, 01234, 001234, 0001234] possible_numbers = []