-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
477c0a7
commit 7d7670b
Showing
14 changed files
with
1,527 additions
and
1 deletion.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
app/jobs/upvs/fetch_all_public_authority_edesks_list_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
class Upvs::FetchAllPublicAuthorityEdesksListJob < ApplicationJob | ||
queue_as :upvs | ||
|
||
def perform(url, downloader: HarvesterUtils::Downloader) | ||
csv_file = downloader.download_file(url) | ||
csv_options = { col_sep: File.open(csv_file) { |f| f.readline }.include?(';') ? ';' : ',', headers: true } | ||
|
||
TemporaryAllPublicAuthorityEdesk.transaction do | ||
TemporaryAllPublicAuthorityEdesk.create_table! | ||
|
||
each_row_as_attributes(csv_file, csv_options) do |attributes| | ||
check_row_attributes(attributes) | ||
TemporaryAllPublicAuthorityEdesk.find_or_initialize_by(uri: attributes[:uri]).update!(attributes) | ||
end | ||
|
||
assert_known_edesks_existence! | ||
|
||
TemporaryAllPublicAuthorityEdesk.truncate_source_table! | ||
TemporaryAllPublicAuthorityEdesk.insert_to_source_table! | ||
end | ||
|
||
BetterUptimeApi.ping_heartbeat('UPVS_FETCH_ALL_EDESKS') | ||
end | ||
|
||
class TemporaryAllPublicAuthorityEdesk < TemporaryRecord | ||
def self.source | ||
Upvs::AllPublicAuthorityEdesk | ||
end | ||
end | ||
|
||
private | ||
|
||
def each_row_as_attributes(csv_file, csv_options) | ||
CSV.foreach(csv_file, csv_options) do |row| | ||
row = row.to_h.transform_keys { |k| k.to_s.gsub(/\p{Cf}|"/, '') } | ||
|
||
row[row.keys.first].sub!(/\A"/, '') | ||
row[row.keys.last].sub!(/"\z/, '') | ||
|
||
row = row.to_h.transform_keys { |k| k.to_s } | ||
row = row.transform_values { |v| v == 'NULL' ? nil : v } | ||
|
||
yield( | ||
cin: row.fetch('ICO'), | ||
uri: row.fetch('URI'), | ||
name: row.fetch('Nazov') | ||
) | ||
end | ||
end | ||
|
||
def check_row_attributes(attributes) | ||
cin, uri, name = attributes.slice(:cin, :uri, :name).values | ||
|
||
cin_without_leading_zeros = cin.sub(/^[0]+/,'') | ||
|
||
if name !~ /TEST/i | ||
raise "#{uri} does not match #{cin}" if uri !~ /ico:\/\/sk\/(0*)#{cin_without_leading_zeros}(_\d+)?/ | ||
end | ||
|
||
raise "Incorrect encoding" if name =~ /.*\\u.*/ | ||
end | ||
|
||
def assert_known_edesks_existence! | ||
repository = TemporaryAllPublicAuthorityEdesk | ||
repository.find_by!(uri: 'ico://sk/00151513', cin: '151513', name: 'Úrad vlády Slovenskej republiky') | ||
repository.find_by!(uri: 'ico://sk/00151513_10003', cin: '151513', name: 'Úrad vlády Slovenskej republiky - Petície') | ||
repository.find_by!(uri: 'ico://sk/00164381', cin: '164381', name: 'Ministerstvo školstva, vedy, výskumu a športu Slovenskej republiky') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Upvs | ||
class ResourceNotFoundError < RuntimeError | ||
end | ||
|
||
class FindAllPublicAuthorityEdeskListJob < ApplicationJob | ||
queue_as :upvs | ||
|
||
DATASET_URL = 'https://data.gov.sk/dataset/zoznam-vsetkych-elektronickych-schranok-ovm' | ||
|
||
def perform(downloader: HarvesterUtils::Downloader, fetch_job: Upvs::FetchAllPublicAuthorityEdesksListJob) | ||
html = downloader.download(DATASET_URL) | ||
doc = Nokogiri::HTML.parse(html) | ||
resource_link = doc.search('.resource-item .dropdown-menu a').detect do |a| | ||
a['href'].include?('.csv') | ||
end | ||
|
||
if resource_link | ||
fetch_job.perform_later(resource_link['href']) | ||
else | ||
raise ResourceNotFoundError | ||
end | ||
|
||
# TODO add hearbeat | ||
BetterUptimeApi.ping_heartbeat('UPVS_FIND_ALL_EDESKS') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Upvs::AllPublicAuthorityEdesk < ApplicationRecord | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20231106173059_create_upvs_all_public_authority_edesk.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateUpvsAllPublicAuthorityEdesk < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table 'upvs.all_public_authority_edesks' do |t| | ||
t.integer :cin, null: false, limit: 8 | ||
t.string :uri, null: false | ||
t.string :name, null: false | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index 'upvs.all_public_authority_edesks', :uri, unique: true | ||
add_index 'upvs.all_public_authority_edesks', :cin | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"ICO","URI","Nazov" | ||
"00332674","ico://sk/00332674","Obec Petrovce, okres Vranov nad Topľou" | ||
"30232295","ico://sk/30232295_71406","Fakulta politických vied a medzinárodných vzťahov Univerzity Mateja Bela v Banskej Bystrici" | ||
"00399418","ico://sk/00399418_10001","Materská škola" | ||
"00308161","ico://sk/00308161","Obec Lovce" | ||
"00327727","ico://sk/00327727_10002","Materská \u0161kola Ruská Nová Ves 24" | ||
"00151513","ico://sk/00151513","Úrad vlády Slovenskej republiky" | ||
"00151513","ico://sk/00151513_10003","Úrad vlády Slovenskej republiky - Petície" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"ICO","URI","Nazov" | ||
"00332674","ico://sk/00332674","Obec Petrovce, okres Vranov nad Topľou" | ||
"30232295","ico://sk/30232295_71406","Fakulta politických vied a medzinárodných vzťahov Univerzity Mateja Bela v Banskej Bystrici" | ||
"00399418","ico://sk/00399418_10001","Materská škola" | ||
"00308161","ico://sk/00308161","Obec Lovce" | ||
"00164381","ico://sk/00164381","Ministerstvo školstva, vedy, výskumu a športu Slovenskej republiky" | ||
"00151513","ico://sk/00151513","Úrad vlády Slovenskej republiky" | ||
"00151513","ico://sk/00151513_10003","Úrad vlády Slovenskej republiky - Petície" | ||
"214973","ico://sk/214973_10001","CRH (Slovensko) a.s." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"ICO","URI","Nazov" | ||
"00332674","ico://sk/00332674","Obec Petrovce, okres Vranov nad Topľou" | ||
"30232295","ico://sk/30232295_71406","Fakulta politických vied a medzinárodných vzťahov Univerzity Mateja Bela v Banskej Bystrici" | ||
"00399418","ico://sk/00399418_10001","Materská škola" | ||
"00308161","ico://sk/00308161","Obec Lovce" | ||
"166260","ico://sk/99166260","Úrad geodézie, kartografie a katastra Slovenskej republiky" | ||
"00151513","ico://sk/00151513","Úrad vlády Slovenskej republiky" | ||
"00151513","ico://sk/00151513_10003","Úrad vlády Slovenskej republiky - Petície" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"ICO","URI","Nazov" | ||
"00332674","ico://sk/00332674","Obec Petrovce, okres Vranov nad Topľou" | ||
"30232295","ico://sk/30232295_71406","Fakulta politických vied a medzinárodných vzťahov Univerzity Mateja Bela v Banskej Bystrici" | ||
"00399418","ico://sk/00399418_10001","Materská škola" | ||
"00308161","ico://sk/00308161","Obec Lovce" | ||
"00164381","ico://sk/00164381","Ministerstvo školstva, vedy, výskumu a športu Slovenskej republiky" | ||
"00151513","ico://sk/00151513","Úrad vlády Slovenskej republiky" | ||
"00151513","ico://sk/00151513_10003","Úrad vlády Slovenskej republiky - Petície" |
Oops, something went wrong.