Skip to content

Commit 0ba0d00

Browse files
Merge pull request #19 from ConvertAPI/feature/archive-format
Adjust from format when archiving
2 parents 88aadbf + 17dd2d0 commit 0ba0d00

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

lib/convert_api/format_detector.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
module ConvertApi
22
class FormatDetector
3-
def initialize(resource)
3+
ANY_FORMAT = 'any'
4+
5+
def initialize(resource, to_format)
46
@resource = resource
7+
@to_format = to_format
58
end
69

710
def run
11+
return ANY_FORMAT if archive?
812
return @resource.file_ext.downcase if @resource.is_a?(UploadIO)
913

10-
format_from_path
14+
format_from_path || raise(FormatError, 'Unable to detect format')
1115
end
1216

1317
private
1418

19+
def archive?
20+
@to_format.to_s.downcase == 'zip'
21+
end
22+
1523
def format_from_path
1624
extension = File.extname(path).downcase
17-
format = extension[1..-1]
18-
format || raise(FormatError, 'Unable to detect format')
25+
extension[1..-1]
1926
end
2027

2128
def path

lib/convert_api/task.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def detect_format(params)
6666

6767
resource = params[:File] || Array(params[:Files]).first
6868

69-
FormatDetector.new(resource).run
69+
FormatDetector.new(resource, @to_format).run
7070
end
7171

7272
def config

spec/convert_api/format_detector_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
RSpec.describe ConvertApi::FormatDetector, '#run' do
2-
subject { described_class.new(resource).run }
2+
subject { described_class.new(resource, to_format).run }
3+
4+
let(:to_format) { 'pdf' }
35

46
context 'with file name' do
57
let(:resource) { 'test.txt' }
68
it { is_expected.to eq('txt') }
79
end
810

11+
context 'when archiving' do
12+
let(:resource) { 'test.txt' }
13+
let(:to_format) { 'zip' }
14+
it { is_expected.to eq('any') }
15+
end
16+
917
context 'with file path' do
1018
let(:resource) { '/some/path/test.txt' }
1119
it { is_expected.to eq('txt') }

spec/convert_api_spec.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@
5151

5252
it_behaves_like 'successful conversion'
5353

54-
context 'with web resource' do
55-
let(:params) { { Url: 'http://convertapi.com' } }
56-
57-
it_behaves_like 'successful conversion'
58-
end
59-
6054
context 'with web resource' do
6155
let(:from_format) { 'web' }
6256
let(:params) { { Url: 'http://convertapi.com' } }

0 commit comments

Comments
 (0)