gzip demarches publiques export
This commit is contained in:
parent
7578c278a4
commit
d294feabe3
2 changed files with 31 additions and 15 deletions
|
@ -1,27 +1,34 @@
|
||||||
class DemarchesPubliquesExportService
|
class DemarchesPubliquesExportService
|
||||||
attr_reader :io
|
attr_reader :gzip_filename
|
||||||
def initialize(io)
|
|
||||||
@io = io
|
def initialize(gzip_filename)
|
||||||
|
@gzip_filename = gzip_filename
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
|
Zlib::GzipWriter.open(gzip_filename) do |gz|
|
||||||
|
generate_json(gz)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def generate_json(io)
|
||||||
end_cursor = nil
|
end_cursor = nil
|
||||||
first = true
|
first = true
|
||||||
write_array_opening
|
write_array_opening(io)
|
||||||
loop do
|
loop do
|
||||||
write_demarches_separator if !first
|
write_demarches_separator(io) if !first
|
||||||
execute_query(cursor: end_cursor)
|
execute_query(cursor: end_cursor)
|
||||||
end_cursor = last_cursor
|
end_cursor = last_cursor
|
||||||
io.write(jsonify(demarches))
|
io.write(jsonify(demarches))
|
||||||
first = false
|
first = false
|
||||||
break if !has_next_page?
|
break if !has_next_page?
|
||||||
end
|
end
|
||||||
write_array_closing
|
write_array_closing(io)
|
||||||
io.close
|
io.close
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def execute_query(cursor: nil)
|
def execute_query(cursor: nil)
|
||||||
result = API::V2::Schema.execute(query, variables: { cursor: cursor }, context: { internal_use: true })
|
result = API::V2::Schema.execute(query, variables: { cursor: cursor }, context: { internal_use: true })
|
||||||
raise DemarchesPubliquesExportService::Error.new(result["errors"]) if result["errors"]
|
raise DemarchesPubliquesExportService::Error.new(result["errors"]) if result["errors"]
|
||||||
|
@ -83,15 +90,15 @@ class DemarchesPubliquesExportService
|
||||||
demarches.map(&:to_json).join(',')
|
demarches.map(&:to_json).join(',')
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_array_opening
|
def write_array_opening(io)
|
||||||
io.write('[')
|
io.write('[')
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_array_closing
|
def write_array_closing(io)
|
||||||
io.write(']')
|
io.write(']')
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_demarches_separator
|
def write_demarches_separator(io)
|
||||||
io.write(',')
|
io.write(',')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
describe DemarchesPubliquesExportService do
|
describe DemarchesPubliquesExportService do
|
||||||
let(:procedure) { create(:procedure, :published, :with_service, :with_type_de_champ) }
|
let(:procedure) { create(:procedure, :published, :with_service, :with_type_de_champ) }
|
||||||
let!(:dossier) { create(:dossier, procedure: procedure) }
|
let!(:dossier) { create(:dossier, procedure: procedure) }
|
||||||
let(:io) { StringIO.new }
|
let(:gzip_filename) { "demarches.json.gz" }
|
||||||
|
|
||||||
|
after { FileUtils.rm(gzip_filename) }
|
||||||
|
|
||||||
describe 'call' do
|
describe 'call' do
|
||||||
it 'generate json for all closed procedures' 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(deflat_gzip(gzip_filename))[0]
|
||||||
expect(JSON.parse(io.string)[0]
|
|
||||||
.deep_symbolize_keys)
|
.deep_symbolize_keys)
|
||||||
.to eq(expected_result)
|
.to eq(expected_result)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'raises exception when procedure with bad data' do
|
it 'raises exception when procedure with bad data' do
|
||||||
procedure.libelle = nil
|
procedure.libelle = nil
|
||||||
procedure.save(validate: false)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue