Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Back-end_Server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Agendador
Back-end_Server
Commits
882e7585
Commit
882e7585
authored
Aug 07, 2018
by
Rafael Ravedutti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Treat special cases for CEPs
Signed-off-by:
Rafael Ravedutti
<
rrlmachado@inf.ufpr.br
>
parent
ee6a646b
Pipeline
#17074
passed with stages
in 3 minutes and 41 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
85 additions
and
33 deletions
+85
-33
app/models/address.rb
app/models/address.rb
+2
-0
app/models/citizen.rb
app/models/citizen.rb
+13
-9
app/models/city_hall.rb
app/models/city_hall.rb
+4
-0
app/models/service_place.rb
app/models/service_place.rb
+27
-23
db/migrate/20180803163556_add_number_to_addresses.rb
db/migrate/20180803163556_add_number_to_addresses.rb
+5
-0
db/schema.rb
db/schema.rb
+2
-1
lib/cep_finder.rb
lib/cep_finder.rb
+32
-0
No files found.
app/models/address.rb
View file @
882e7585
...
...
@@ -40,6 +40,7 @@ class Address < ApplicationRecord
address
=
Address
.
new
(
zipcode:
result
[
:zipcode
],
address:
result
[
:address
],
number:
result
[
:number
],
neighborhood:
result
[
:neighborhood
],
city_id:
get_city_id
(
zipcode
),
state_id:
get_state_id
(
zipcode
),
...
...
@@ -49,6 +50,7 @@ class Address < ApplicationRecord
else
address
.
zipcode
=
result
[
:zipcode
]
address
.
address
=
result
[
:address
]
address
.
number
=
result
[
:number
]
address
.
neighborhood
=
result
[
:neighborhood
]
address
.
city_id
=
get_city_id
(
zipcode
)
address
.
state_id
=
get_state_id
(
zipcode
)
...
...
app/models/citizen.rb
View file @
882e7585
...
...
@@ -40,7 +40,7 @@ class Citizen < ApplicationRecord
validates_length_of
:rg
,
maximum:
13
validates_length_of
:address_number
,
within:
0
..
10
,
allow_blank:
true
validates_numericality_of
:address_number
,
only_integer:
true
,
validates_numericality_of
:address_number
,
only_integer:
true
,
allow_blank:
true
validates_format_of
:name
,
...
...
@@ -64,16 +64,16 @@ class Citizen < ApplicationRecord
before_validation
:set_address
# Scopes #
scope
:all_active
,
->
{
where
(
active:
true
,
responsible_id:
nil
)
scope
:all_active
,
->
{
where
(
active:
true
,
responsible_id:
nil
)
}
scope
:local
,
->
(
city_id
)
{
where
(
city_id:
city_id
)
scope
:local
,
->
(
city_id
)
{
where
(
city_id:
city_id
)
}
scope
:dependants
,
->
{
where
(
responsible_id:
self
.
id
)
scope
:dependants
,
->
{
where
(
responsible_id:
self
.
id
)
}
...
...
@@ -170,7 +170,7 @@ class Citizen < ApplicationRecord
# @params params [ActionController::Parameters] Parameters for searching
# @params npage [String] number of page to be returned
# @params permission [String] Permission of current user
# @return [ActiveRecords] filtered citizens
# @return [ActiveRecords] filtered citizens
def
self
.
filter
(
params
,
npage
,
permission
)
return
search
(
search_params
(
params
,
permission
),
npage
)
end
...
...
@@ -192,7 +192,7 @@ class Citizen < ApplicationRecord
filter
=
{
"name"
=>
"name_cont"
,
"cpf"
=>
"cpf_eq"
,
"s"
=>
"s"
}
end
return
filter_search_params
(
params
,
filter
,
sortable
)
return
filter_search_params
(
params
,
filter
,
sortable
)
end
...
...
@@ -220,6 +220,10 @@ class Citizen < ApplicationRecord
self
.
address_street
=
address
.
address
end
if
not
address
.
number
.
nil?
self
.
address_number
=
address
.
number
end
if
not
address
.
neighborhood
.
empty?
self
.
neighborhood
=
address
.
neighborhood
end
...
...
app/models/city_hall.rb
View file @
882e7585
...
...
@@ -149,6 +149,10 @@ class CityHall < ApplicationRecord
self
.
city_id
=
address
.
city_id
self
.
address_street
=
address
.
address
self
.
neighborhood
=
address
.
neighborhood
if
not
address
.
number
.
nil?
self
.
address_number
=
address
.
number
end
else
self
.
errors
[
"cep"
]
<<
"
#{
self
.
cep
}
is invalid."
return
false
...
...
app/models/service_place.rb
View file @
882e7585
...
...
@@ -33,7 +33,7 @@ class ServicePlace < ApplicationRecord
validates_presence_of
:cep
validates_length_of
:name
,
maximum:
255
validates_length_of
:address_number
,
validates_length_of
:address_number
,
within:
0
..
10
,
allow_blank:
true
...
...
@@ -48,7 +48,7 @@ class ServicePlace < ApplicationRecord
where
(
active:
true
)
}
scope
:local_city_hall
,
->
(
city_hall_id
)
{
scope
:local_city_hall
,
->
(
city_hall_id
)
{
where
(
city_hall_id:
city_hall_id
)
}
...
...
@@ -59,7 +59,7 @@ class ServicePlace < ApplicationRecord
delegate
:state_name
,
to: :city
# Get every available schedule from the current service_place given a
# Get every available schedule from the current service_place given a
# service_type
#
# @param service_type_id [Integer] id from specified service_type
...
...
@@ -83,14 +83,14 @@ class ServicePlace < ApplicationRecord
end
# Returns json response to index service_types
# Returns json response to index service_types
# @return [Json] response
def
self
.
index_response
self
.
all
.
as_json
(
only:
[
:id
,
:name
,
:cep
,
:active
,
:neighborhood
,
:phone1
],
methods:
%w(city_hall_name city_name state_name)
)
end
# @return [Json] detailed service_type's data
def
complete_info_response
...
...
@@ -118,7 +118,7 @@ class ServicePlace < ApplicationRecord
:ibge_code
,
:created_at
,
:updated_at
])})
.
merge
({
address:
address
.
as_json
(
except:
[
:created_at
,
:updated_at
,
:state_id
,
:city_id
:created_at
,
:updated_at
,
:state_id
,
:city_id
,
:number
])})
end
...
...
@@ -126,14 +126,14 @@ class ServicePlace < ApplicationRecord
# Get every service_place and its schedules given a service_type
#
# @param service_type [ServiceType] specified ServiceType
# @return [Json] array containing the reponses from get_schedules for every
# @return [Json] array containing the reponses from get_schedules for every
# service_place
def
self
.
get_schedule_response
(
service_type
)
service_places
=
ServicePlace
.
where
(
active:
true
)
.
find
(
service_type
.
service_place_ids
)
# Add reponses from get_schedules obtained from every service_place
# containing the specified service_type
# containing the specified service_type
response
=
[].
as_json
for
i
in
service_places
response
<<
i
.
get_schedules
(
service_type
.
id
)
...
...
@@ -162,46 +162,46 @@ class ServicePlace < ApplicationRecord
case
permission
when
"adm_c3sl"
sortable
=
[
"name"
,
"cep"
,
"city_hall_name"
,
"active"
,
"name"
,
"cep"
,
"city_hall_name"
,
"active"
,
"neighborhood"
]
filter
=
{
"name"
=>
"name_cont"
,
"active"
=>
"active_eq"
,
"name"
=>
"name_cont"
,
"active"
=>
"active_eq"
,
"neighborhood"
=>
"neighborhood_cont"
,
"cep"
=>
"cep_cont"
,
"city_hall_id"
=>
"city_hall_id_eq"
,
"cep"
=>
"cep_cont"
,
"city_hall_id"
=>
"city_hall_id_eq"
,
"s"
=>
"s"
}
when
"adm_prefeitura"
sortable
=
[
"name"
,
"name"
,
"cep"
,
"active"
,
"active"
,
"neighborhood"
]
filter
=
{
"name"
=>
"name_cont"
,
"active"
=>
"active_eq"
,
"name"
=>
"name_cont"
,
"active"
=>
"active_eq"
,
"neighborhood"
=>
"neighborhood_cont"
,
"cep"
=>
"cep_cont"
,
"cep"
=>
"cep_cont"
,
"role"
=>
"professionals_service_places_role_eq"
,
"s"
=>
"s"
}
end
return
filter_search_params
(
params
,
filter
,
sortable
)
return
filter_search_params
(
params
,
filter
,
sortable
)
end
# Method called when creating a service_place. It associates
# Method called when creating a service_place. It associates
# the address to the service place given a cep
def
create_service_place
address
=
Address
.
get_address
(
self
.
cep
)
...
...
@@ -223,6 +223,10 @@ class ServicePlace < ApplicationRecord
self
.
address_street
=
address
.
address
self
.
neighborhood
=
address
.
neighborhood
if
not
address
.
number
.
nil?
self
.
address_number
=
address
.
number
end
else
self
.
errors
[
"cep"
]
<<
"
#{
self
.
cep
}
is invalid."
return
false
...
...
db/migrate/20180803163556_add_number_to_addresses.rb
0 → 100644
View file @
882e7585
class
AddNumberToAddresses
<
ActiveRecord
::
Migration
[
5.0
]
def
change
add_column
:addresses
,
:number
,
:integer
end
end
db/schema.rb
View file @
882e7585
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
20180
511112959
)
do
ActiveRecord
::
Schema
.
define
(
version:
20180
803163556
)
do
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
...
...
@@ -52,6 +52,7 @@ ActiveRecord::Schema.define(version: 20180511112959) do
t
.
datetime
"updated_at"
,
null:
false
t
.
integer
"city_id"
t
.
integer
"state_id"
t
.
integer
"number"
t
.
index
[
"city_id"
],
name:
"index_addresses_on_city_id"
,
using: :btree
t
.
index
[
"state_id"
],
name:
"index_addresses_on_state_id"
,
using: :btree
end
...
...
lib/cep_finder.rb
View file @
882e7585
...
...
@@ -16,6 +16,10 @@
require
'curb'
require
'nokogiri'
def
is_number?
string
true
if
Float
(
string
)
rescue
false
end
module
Agendador
module
CEP
class
Finder
...
...
@@ -72,6 +76,34 @@ module Agendador
address
[
:neighborhood
]
=
doc
.
xpath
(
"//bairro"
).
text
address
[
:city
]
=
doc
.
xpath
(
"//cidade"
).
text
address
[
:state
]
=
doc
.
xpath
(
"//estado"
).
text
address
[
:number
]
=
nil
# Get the last three digits from the zipcode
zipcode_suffix
=
address
[
:zipcode
].
split
(
//
).la
s
t
(
3
).
join
# Check specific cases of CEP codes that may have a number included
# in its address field, the following address shows more details:
# https://www.correios.com.br/precisa-de-ajuda/
# o-que-e-cep-e-por-que-usa-lo/estrutura-do-cep
if
zipcode_suffix
.
to_i
>=
900
and
zipcode_suffix
.
to_i
<=
969
# Split the address by comma
address_splitted
=
address
[
:address
].
split
(
','
)
# If there is more elements in the array, so there's a comma in the
# address (probably separating address and number)
if
address_splitted
.
length
>
1
# Get possible number from address
number
=
address_splitted
.
last
.
strip
# Verify if number is numeric, if it is, change the address to
# the part of address without the number and set the number
# field in the address
if
is_number?
(
number
)
address
[
:address
]
=
address_splitted
[
0
].
strip
address
[
:number
]
=
number
.
to_i
end
end
end
end
return
address
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment