Skip to content

Commit

Permalink
Set default convert language to English
Browse files Browse the repository at this point in the history
Fix #166
  • Loading branch information
tagliala committed Oct 5, 2022
1 parent 1f203a5 commit 661cea8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
{
Expand Down
29 changes: 26 additions & 3 deletions spec/lib/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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
Expand Down

0 comments on commit 661cea8

Please sign in to comment.