Created new entry point CensusCaller

Here's a better alternative than the first one. Added a new abstraction level from which are performed both types of census calls, while the logic of those is managed in their own library.
This commit is contained in:
María Checa
2017-07-10 12:22:30 +02:00
parent 7e701db608
commit 089c690b49
7 changed files with 87 additions and 32 deletions

View File

@@ -102,7 +102,7 @@ class Officing::Residence
private
def call_census_api
@census_api_response = CensusApi.new.call(document_type, document_number)
@census_api_response = CensusCaller.new.call(document_type, document_number)
end
def residency_valid?

View File

@@ -35,7 +35,7 @@ class Poll
end
def census_api_response
@census_api_response ||= CensusApi.new.call(document_type, document_number)
@census_api_response ||= CensusCaller.new.call(document_type, document_number)
end
def fill_stats_fields

View File

@@ -69,7 +69,7 @@ class Signature < ActiveRecord::Base
def in_census?
document_types.detect do |document_type|
response = CensusApi.new.call(document_type, document_number)
response = CensusCaller.new.call(document_type, document_number)
if response.valid?
@census_api_response = response
true
@@ -94,4 +94,4 @@ class Signature < ActiveRecord::Base
%w(1 2 3 4)
end
end
end

View File

@@ -18,7 +18,7 @@ class Verification::Management::Document
end
def in_census?
response = CensusApi.new.call(document_type, document_number)
response = CensusCaller.new.call(document_type, document_number)
response.valid? && valid_age?(response)
end

View File

@@ -29,21 +29,12 @@ class Verification::Residence
user.take_votes_if_erased_document(document_number, document_type)
if @census_data.class.name === 'LocalCensusRecord'
user.update(document_number: document_number,
document_type: document_type,
date_of_birth: date_of_birth.to_datetime,
residence_verified_at: Time.current)
@census_data.update(user_id: user)
else
user.update(document_number: document_number,
document_type: document_type,
geozone: self.geozone,
date_of_birth: date_of_birth.to_datetime,
gender: gender,
residence_verified_at: Time.current)
end
user.update(document_number: document_number,
document_type: document_type,
geozone: self.geozone,
date_of_birth: date_of_birth.to_datetime,
gender: gender,
residence_verified_at: Time.current)
end
def allowed_age
@@ -80,18 +71,7 @@ class Verification::Residence
private
def retrieve_census_data
response = call_census_api
response = local_census_record_query unless response.valid?
@census_data = response
end
def call_census_api
CensusApi.new.call(document_type, document_number)
end
def local_census_record_query
LocalCensusRecord.find_by(document_type: document_type, document_number: document_number)
@census_data = CensusCaller.new.call(document_type, document_number)
end
def residency_valid?

7
lib/census_caller.rb Normal file
View File

@@ -0,0 +1,7 @@
class CensusCaller
def call(document_type, document_number)
response = CensusApi.new.call(document_type, document_number)
response = LocalCensus.new.call(document_type, document_number) unless response.valid?
end
end

68
lib/local_census.rb Normal file
View File

@@ -0,0 +1,68 @@
include DocumentParser
class LocalCensus
def call(document_type, document_number)
record = nil
get_document_number_variants(document_type, document_number).each do |variant|
record = Response.new(get_record(document_type, variant))
return record if record.valid?
end
record
end
class Response
def initialize(body)
@body = body
end
def valid?
@body.valid?
rescue NoMethodError
false
end
def date_of_birth
@body.date_of_birth
end
def postal_code
@body.postal_code
end
def district_code
@body.district_code rescue nil
end
def gender
case @body.gender
when "Varón"
"male"
when "Mujer"
"female"
end
rescue NoMethodError
nil
end
def name
"#{@body.nombre} #{@body.apellido1}" rescue nil
end
private
def data
@body.attributes
end
end
private
def get_record(document_type, document_number)
LocalCensusRecord.find_by(document_type: document_type, document_number: document_number)
end
def is_dni?(document_type)
document_type.to_s == "1"
end
end