File tree Expand file tree Collapse file tree 4 files changed +21
-12
lines changed
Expand file tree Collapse file tree 4 files changed +21
-12
lines changed Original file line number Diff line number Diff line change 11module 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11RSpec . 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' ) }
Original file line number Diff line number Diff line change 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' } }
You can’t perform that action at this time.
0 commit comments