gzip demarches publiques export

This commit is contained in:
Christophe Robillard 2022-07-13 10:41:22 +02:00
parent 7578c278a4
commit d294feabe3
2 changed files with 31 additions and 15 deletions

View file

@ -1,27 +1,34 @@
class DemarchesPubliquesExportService
attr_reader :io
def initialize(io)
@io = io
attr_reader :gzip_filename
def initialize(gzip_filename)
@gzip_filename = gzip_filename
end
def call
Zlib::GzipWriter.open(gzip_filename) do |gz|
generate_json(gz)
end
end
private
def generate_json(io)
end_cursor = nil
first = true
write_array_opening
write_array_opening(io)
loop do
write_demarches_separator if !first
write_demarches_separator(io) if !first
execute_query(cursor: end_cursor)
end_cursor = last_cursor
io.write(jsonify(demarches))
first = false
break if !has_next_page?
end
write_array_closing
write_array_closing(io)
io.close
end
private
def execute_query(cursor: nil)
result = API::V2::Schema.execute(query, variables: { cursor: cursor }, context: { internal_use: true })
raise DemarchesPubliquesExportService::Error.new(result["errors"]) if result["errors"]
@ -83,15 +90,15 @@ class DemarchesPubliquesExportService
demarches.map(&:to_json).join(',')
end
def write_array_opening
def write_array_opening(io)
io.write('[')
end
def write_array_closing
def write_array_closing(io)
io.write(']')
end
def write_demarches_separator
def write_demarches_separator(io)
io.write(',')
end
end

View file

@ -1,7 +1,9 @@
describe DemarchesPubliquesExportService do
let(:procedure) { create(:procedure, :published, :with_service, :with_type_de_champ) }
let!(:dossier) { create(:dossier, procedure: procedure) }
let(:io) { StringIO.new }
let(:gzip_filename) { "demarches.json.gz" }
after { FileUtils.rm(gzip_filename) }
describe 'call' do
it 'generate json for all closed procedures' do
@ -31,17 +33,24 @@ describe DemarchesPubliquesExportService do
]
}
}
DemarchesPubliquesExportService.new(gzip_filename).call
DemarchesPubliquesExportService.new(io).call
expect(JSON.parse(io.string)[0]
expect(JSON.parse(deflat_gzip(gzip_filename))[0]
.deep_symbolize_keys)
.to eq(expected_result)
end
it 'raises exception when procedure with bad data' do
procedure.libelle = nil
procedure.save(validate: false)
expect { DemarchesPubliquesExportService.new(io).call }.to raise_error(DemarchesPubliquesExportService::Error)
expect { DemarchesPubliquesExportService.new(gzip_filename).call }.to raise_error(DemarchesPubliquesExportService::Error)
end
end
def deflat_gzip(gzip_filename)
Zlib::GzipReader.open(gzip_filename) do |gz|
return gz.read
end
end
end