Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
venjob_nth
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ngô Trung Hưng
venjob_nth
Commits
0924d8d3
Commit
0924d8d3
authored
Aug 05, 2020
by
Ngô Trung Hưng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix bug import csv
parent
56cba0ab
Pipeline
#806
canceled with stages
in 0 seconds
Changes
5
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
57 deletions
+34
-57
app/models/company.rb
+1
-0
config/application.rb
+1
-0
lib/service/ftp_sever.rb
+3
-2
lib/service/import_data.rb
+26
-50
lib/tasks/csv.rake
+3
-5
No files found.
app/models/company.rb
View file @
0924d8d3
...
@@ -2,5 +2,6 @@
...
@@ -2,5 +2,6 @@
# Description/Explanation of Person class
# Description/Explanation of Person class
class
Company
<
ApplicationRecord
class
Company
<
ApplicationRecord
COMPANY_SECURITY
=
1
has_many
:jobs
has_many
:jobs
end
end
config/application.rb
View file @
0924d8d3
...
@@ -11,6 +11,7 @@ module Venjob
...
@@ -11,6 +11,7 @@ module Venjob
# Initialize configuration defaults for originally generated Rails version.
# Initialize configuration defaults for originally generated Rails version.
config
.
load_defaults
5.2
config
.
load_defaults
5.2
config
.
autoload_paths
<<
Rails
.
root
.
join
(
'lib/service'
)
# Settings in config/environments/* take precedence over those specified here.
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# -- all .rb files in that directory are automatically loaded after loading
...
...
lib/service/ftp.rb
→
lib/service/ftp
_sever
.rb
View file @
0924d8d3
# frozen_string_literal: true
# frozen_string_literal: true
require
'service/unzip'
require
'net/ftp'
require
'net/ftp'
require
'logger'
require
'logger'
require
'csv'
require
'csv'
...
@@ -30,9 +29,11 @@ class FtpSever
...
@@ -30,9 +29,11 @@ class FtpSever
def
download_csv
def
download_csv
Net
::
FTP
.
open
(
CONTENT_SERVER_DOMAIN_NAME
,
CONTENT_SERVER_USER_NAME
,
CONTENT_SERVER_USER_PASSWORD
)
do
|
ftp
|
Net
::
FTP
.
open
(
CONTENT_SERVER_DOMAIN_NAME
,
CONTENT_SERVER_USER_NAME
,
CONTENT_SERVER_USER_PASSWORD
)
do
|
ftp
|
ftp
.
getbinaryfile
(
NAME_CSV
)
ftp
.
getbinaryfile
(
NAME_CSV
)
Unzip
.
extract_zip
(
(
jobs_csv_path
)
,
Rails
.
root
.
join
(
'lib'
,
'csv'
))
Unzip
.
extract_zip
(
jobs_csv_path
,
Rails
.
root
.
join
(
'lib'
,
'csv'
))
File
.
delete
(
jobs_csv_path
)
if
File
.
exist?
(
jobs_csv_path
)
File
.
delete
(
jobs_csv_path
)
if
File
.
exist?
(
jobs_csv_path
)
logger
.
info
'Download & extract success'
logger
.
info
'Download & extract success'
rescue
StandardError
=>
e
logger
.
error
"Donwload csv have error:
#{
e
}
"
end
end
end
end
end
end
lib/service/import_data.rb
View file @
0924d8d3
# frozen_string_literal: true
# frozen_string_literal: true
require
'service/ftp'
# Import data from CSV
# Import data from CSV
class
ImportData
class
ImportData
COMPANY_SECURITY
=
1
attr_accessor
:csv
def
initialize
(
csv
)
@csv
=
csv
end
def
import_data
def
import_data
csv_data
=
FtpSever
.
new
.
data_csv
import_companies
import_industries_from
(
csv_data
)
import_jobs
import_cities_from
(
csv_data
)
import_companies_from
(
csv_data
)
import_jobs_from
(
csv_data
)
end
end
def
logger
def
logger
...
@@ -20,31 +19,7 @@ class ImportData
...
@@ -20,31 +19,7 @@ class ImportData
private
private
def
import_industries_from
(
csv
)
def
import_companies
industries
=
[]
industries
+=
csv
[
'category'
].
map
(
&
:strip
)
industries
.
each
do
|
val
|
val
.
gsub!
(
','
,
'/'
)
if
val
.
include?
(
','
)
val
.
gsub!
(
'/'
,
' / '
)
Industry
.
find_or_create_by
(
name:
val
)
end
rescue
StandardError
=>
e
logger
.
error
"Import_industries:
#{
e
}
"
end
def
import_cities_from
(
csv
)
city_names
=
csv
[
'work place'
].
uniq
.
select
(
&
:present?
)
cities
=
city_names
.
map
{
|
val
|
val
.
delete
(
'[]\"'
)
}
cities
.
each
do
|
val
|
next
if
val
.
blank?
City
.
find_or_create_by
(
name:
val
,
area:
City
.
areas
[
'domestic'
])
end
rescue
StandardError
=>
e
logger
.
error
"Import_cities:
#{
e
}
"
end
def
import_companies_from
(
csv
)
csv
[
'company name'
].
each_with_index
do
|
name
,
index
|
csv
[
'company name'
].
each_with_index
do
|
name
,
index
|
Company
.
find_or_create_by
(
name:
name
.
strip
)
do
|
company
|
Company
.
find_or_create_by
(
name:
name
.
strip
)
do
|
company
|
company
.
address
=
csv
[
'company address'
][
index
]
company
.
address
=
csv
[
'company address'
][
index
]
...
@@ -55,29 +30,30 @@ class ImportData
...
@@ -55,29 +30,30 @@ class ImportData
end
end
end
end
def
import_jobs_from
(
csv
)
def
import_jobs
csv
[
'name'
].
each_with_index
do
|
name
,
index
|
csv
.
each
do
|
val
|
desc
=
"
#{
csv
[
'requirement'
][
index
]
}
#{
(
csv
[
'description'
][
index
])
}
"
desc
=
"
#{
val
[
'requirement'
]
}
#{
(
val
[
'description'
])
}
"
company
=
Company
.
find_by
name:
csv
[
'company name'
][
index
].
to_s
.
strip
# Get ID Company to create job
company_id
=
company
.
try
(
:id
)
||
COMPANY_SECURITY
company
=
Company
.
find_by
name:
val
[
'company name'
].
strip
begin
company_id
=
company
.
try
(
:id
)
||
Company
::
COMPANY_SECURITY
job
=
Job
.
create
(
name:
name
,
# Get data city do create relationship city_jobs
city_names
=
val
[
'work place'
].
delete
(
'[]\"'
)
city
=
City
.
find_or_create_by
(
name:
city_names
.
strip
)
{
|
record
|
record
.
area
=
City
.
areas
[
'domestic'
]
}
# Get data Industry do create relationship industry_jobs
industry_name
=
val
[
'category'
].
gsub
(
','
,
'/'
).
gsub
(
'/'
,
' / '
)
industry
=
Industry
.
find_or_create_by
(
name:
industry_name
.
strip
)
Job
.
find_or_create_by
(
name:
val
[
'name'
],
company_id:
company_id
,
company_id:
company_id
,
level:
csv
[
'level'
][
index
],
level:
val
[
'level'
],
salary:
csv
[
'salary'
][
index
],
salary:
val
[
'salary'
],
create_date:
Time
.
now
,
create_date:
Time
.
now
,
description:
desc
)
description:
desc
)
do
|
job
|
city_names
=
csv
[
'work place'
][
index
].
to_s
.
delete
(
'[]\"'
)
city
=
City
.
find_or_create_by
(
name:
city_names
.
strip
)
{
|
record
|
record
.
area
=
DOMESTIC
}
job
.
cities
<<
city
job
.
cities
<<
city
industry_name
=
csv
[
'category'
][
index
].
to_s
.
gsub
(
','
,
'/'
).
gsub
(
'/'
,
' / '
)
industry
=
Industry
.
find_or_create_by
(
name:
industry_name
.
strip
)
job
.
industries
<<
industry
job
.
industries
<<
industry
puts
index
end
rescue
StandardError
=>
e
rescue
StandardError
=>
e
logger
.
error
"Import_jobs:
#{
e
}
"
logger
.
error
"Import_jobs:
#{
e
}
"
end
end
end
end
end
end
end
lib/tasks/csv.rake
View file @
0924d8d3
# frozen_string_literal: true
# frozen_string_literal: true
require
'service/import_data'
# Import data from csv
# Description/Explanation of Person class
namespace
:csv
do
namespace
:csv
do
task
import_csv: :environment
do
task
import_csv: :environment
do
csv
=
ImportData
.
new
data_csv
=
FtpSever
.
new
.
data_csv
csv
.
import_data
ImportData
.
new
(
data_csv
)
.
import_data
end
end
end
end
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