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:
7
lib/census_caller.rb
Normal file
7
lib/census_caller.rb
Normal 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
68
lib/local_census.rb
Normal 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
|
||||
Reference in New Issue
Block a user