From 1952a38f744b128d7cddd6917d79d14fbb636322 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Wed, 5 Oct 2022 13:00:45 +0200 Subject: [PATCH] Set default convert language to English Fix #166 --- lib/app.rb | 6 ++++-- spec/lib/app_spec.rb | 29 ++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index 6d537a4..c69df1b 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -196,7 +196,8 @@ class App < Sinatra::Base end body = params[:file][:tempfile].read - content = Converter.new(logger:@logger).convert_file( params[:action], body, params[:language] ) + language = params.fetch(:language, 'en') + content = Converter.new(logger:@logger).convert_file( params[:action], body, language ) content_type content.mime_type content rescue StandardError => e @@ -221,7 +222,8 @@ class App < Sinatra::Base else raise DocumentNotFound.new "Please specify either 'file' or 'url' POST variable" end - path = LegacyConverter.new.convert_file params[:action], body, params[:language] + language = params.fetch(:language, 'en') + path = LegacyConverter.new.convert_file params[:action], body, language converted_url = @legacy_url_base + path content_type 'application/json' { diff --git a/spec/lib/app_spec.rb b/spec/lib/app_spec.rb index 16e4545..38de9ec 100644 --- a/spec/lib/app_spec.rb +++ b/spec/lib/app_spec.rb @@ -263,7 +263,7 @@ def show_backtrace response action: 'pdf', file: Rack::Test::UploadedFile.new(__FILE__, 'application/ruby'), } - expect(foo).to receive(:convert_file).with(params[:action],String,nil) { "%PDF-1.4" } + expect(foo).to receive(:convert_file).with(params[:action],String,'en') { "%PDF-1.4" } post "/convert", params expect(last_response.status).to eq 200 expect(last_response.content_type).to eq 'application/pdf; charset=us-ascii' @@ -284,6 +284,17 @@ def show_backtrace response expect(body).to be_a Hash expect(body['description']).to eq 'Argh' end + it 'allows to specify language' do + foo = double(Colore::Converter) + allow(Colore::Converter).to receive(:new) { foo } + params = { + action: 'pdf', + file: Rack::Test::UploadedFile.new(__FILE__, 'application/ruby'), + language: 'fr' + } + expect(foo).to receive(:convert_file).with(params[:action],String,'fr') { "%PDF-1.4" } + post "/convert", params + end end context 'POST /legacy/convert' do @@ -294,7 +305,7 @@ def show_backtrace response action: 'pdf', file: Rack::Test::UploadedFile.new(__FILE__, 'application/ruby'), } - expect(foo).to receive(:convert_file).with(params[:action],String,nil) { 'foobar' } + expect(foo).to receive(:convert_file).with(params[:action],String,'en') { 'foobar' } post "/#{Colore::LegacyConverter::LEGACY}/convert", params expect(last_response.status).to eq 200 expect(last_response.content_type).to eq 'application/json' @@ -310,7 +321,7 @@ def show_backtrace response url: 'http://localhost/foo/bar', } expect(Net::HTTP).to receive(:get).with(URI(params[:url])) { 'The quick brown flox' } - expect(foo).to receive(:convert_file).with(params[:action],String,nil) { 'foobar' } + expect(foo).to receive(:convert_file).with(params[:action],String,'en') { 'foobar' } post "/#{Colore::LegacyConverter::LEGACY}/convert", params expect(last_response.status).to eq 200 expect(last_response.content_type).to eq 'application/json' @@ -333,6 +344,18 @@ def show_backtrace response expect(body).to be_a Hash expect(body['error']).to eq 'Argh' end + it 'allows to specify language' do + foo = double(Colore::LegacyConverter) + allow(Colore::LegacyConverter).to receive(:new) { foo } + params = { + action: 'pdf', + url: 'http://localhost/foo/bar', + language: 'fr' + } + expect(Net::HTTP).to receive(:get).with(URI(params[:url])) { 'The quick brown flox' } + expect(foo).to receive(:convert_file).with(params[:action],String,'fr') { 'foobar' } + post "/#{Colore::LegacyConverter::LEGACY}/convert", params + end end context 'GET /legacy/:file_id' do