Skip to content

Commit 83fbc3d

Browse files
committed
Resolve remaining review cleanups
1 parent ac09448 commit 83fbc3d

File tree

8 files changed

+124
-11
lines changed

8 files changed

+124
-11
lines changed

lib/uploadcare/api/rest/document_conversions.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def convert(paths:, options: {}, request_options: {})
3535
body = { paths: paths }
3636
body[:store] = normalize_bool_param(options[:store]) if options.key?(:store)
3737
body[:save_in_group] = normalize_bool_param(options[:save_in_group]) if options.key?(:save_in_group)
38+
body.compact!
3839

3940
rest.post(path: '/convert/document/', params: body, headers: {}, request_options: request_options)
4041
end
@@ -53,10 +54,11 @@ def status(token:, request_options: {})
5354
private
5455

5556
def normalize_bool_param(value)
56-
case value
57+
normalized = value.is_a?(String) ? value.strip.downcase : value
58+
59+
case normalized
5760
when true, 1, '1', 'true' then '1'
5861
when false, 0, '0', 'false' then '0'
59-
else value ? '1' : '0'
6062
end
6163
end
6264
end

lib/uploadcare/internal/upload_io.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ def self.wrap(source, filename: nil)
1818
end
1919

2020
def self.path_backed?(source)
21-
source.respond_to?(:path) && source.path && ::File.exist?(source.path)
21+
source.respond_to?(:path) &&
22+
source.path &&
23+
::File.file?(source.path) &&
24+
::File.readable?(source.path)
2225
end
2326

2427
def self.extract_filename(source)

lib/uploadcare/resources/file.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
# and instance methods (store, delete, reload, convert) for working with files.
77
#
88
# @example Finding a file
9-
# file = Uploadcare::File.find(uuid: "file-uuid")
9+
# client = Uploadcare.client
10+
# file = client.files.find(uuid: "file-uuid")
1011
# file.original_filename # => "photo.jpg"
1112
#
1213
# @example Uploading
13-
# file = Uploadcare::File.upload(File.open("photo.jpg"), store: true)
14+
# client = Uploadcare.client
15+
# file = client.files.upload(::File.open("photo.jpg"), store: true)
1416
#
1517
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File
1618
class Uploadcare::Resources::File < Uploadcare::Resources::BaseResource
@@ -288,7 +290,12 @@ def convert_to_video(params: {}, options: {}, request_options: {})
288290
def uuid
289291
return @uuid if @uuid
290292

291-
source = @url || @original_file_url
293+
source =
294+
if @url
295+
@url
296+
elsif uploadcare_cdn_url?(@original_file_url)
297+
@original_file_url
298+
end
292299
return @uuid unless source
293300

294301
@uuid = source[/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/]
@@ -298,13 +305,18 @@ def uuid
298305
#
299306
# @return [String]
300307
def cdn_url
301-
return @url if @url
308+
return @url if uploadcare_cdn_url?(@url)
309+
return @original_file_url if uploadcare_cdn_url?(@original_file_url)
302310

303311
"#{config.cdn_base}#{uuid}/"
304312
end
305313

306314
private
307315

316+
def uploadcare_cdn_url?(value)
317+
value.to_s.start_with?(config.cdn_base.to_s)
318+
end
319+
308320
def convert_file(params, converter, options = {}, request_options: {})
309321
raise ArgumentError, 'The first argument must be a Hash' unless params.is_a?(Hash)
310322

lib/uploadcare/resources/file_metadata.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def index(uuid: nil, request_options: {})
2020
response = Uploadcare::Result.unwrap(
2121
client.api.rest.file_metadata.index(uuid: uuid || @uuid, request_options: request_options)
2222
)
23-
@metadata = response if response.is_a?(Hash)
23+
@metadata = response.is_a?(Hash) ? response.transform_keys(&:to_s) : {}
2424
self
2525
end
2626

@@ -72,9 +72,12 @@ def update(key:, value:, uuid: nil, request_options: {})
7272
# @param request_options [Hash] Request options
7373
# @return [String] Metadata value
7474
def show(key:, uuid: nil, request_options: {})
75-
Uploadcare::Result.unwrap(
76-
client.api.rest.file_metadata.show(uuid: uuid || @uuid, key: key, request_options: request_options)
75+
target_uuid = uuid || @uuid
76+
result = Uploadcare::Result.unwrap(
77+
client.api.rest.file_metadata.show(uuid: target_uuid, key: key, request_options: request_options)
7778
)
79+
@metadata[key.to_s] = result if target_uuid == @uuid
80+
result
7881
end
7982

8083
# Delete a metadata key.

spec/uploadcare/api/rest/document_conversions_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,37 @@
154154

155155
expect(stub).to have_been_requested
156156
end
157+
158+
it 'normalizes case-insensitive string booleans' do
159+
stub = stub_request(:post, 'https://api.uploadcare.com/convert/document/')
160+
.with(body: hash_including('store' => '0', 'save_in_group' => '1'))
161+
.to_return(
162+
status: 200,
163+
body: { result: [], problems: {} }.to_json,
164+
headers: { 'Content-Type' => 'application/json' }
165+
)
166+
167+
document_conversions.convert(paths: [conversion_path], options: { store: 'False', save_in_group: 'TRUE' })
168+
169+
expect(stub).to have_been_requested
170+
end
171+
172+
it 'omits unsupported boolean-like values' do
173+
stub = stub_request(:post, 'https://api.uploadcare.com/convert/document/')
174+
.with do |request|
175+
body = JSON.parse(request.body)
176+
body['paths'] == [conversion_path] && !body.key?('store')
177+
end
178+
.to_return(
179+
status: 200,
180+
body: { result: [], problems: {} }.to_json,
181+
headers: { 'Content-Type' => 'application/json' }
182+
)
183+
184+
document_conversions.convert(paths: [conversion_path], options: { store: 'no' })
185+
186+
expect(stub).to have_been_requested
187+
end
157188
end
158189

159190
describe '#status' do

spec/uploadcare/internal/upload_io_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,18 @@
5050
described_class.wrap('not-io')
5151
end.to raise_error(ArgumentError, /readable IO/)
5252
end
53+
54+
it 'does not treat directories as path-backed files' do
55+
Dir.mktmpdir do |directory|
56+
source = StringIO.new('stream-data')
57+
source.define_singleton_method(:path) { directory }
58+
59+
wrapped = described_class.wrap(source, filename: 'directory.txt')
60+
61+
expect(File.file?(wrapped.path)).to eq(true)
62+
expect(wrapped.original_filename).to eq('directory.txt')
63+
wrapped.close!
64+
end
65+
end
5366
end
5467
end

spec/uploadcare/resources/file_metadata_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@
9494
expect(metadata_instance['key1']).to eq('value1')
9595
expect(metadata_instance.to_h).to eq({ 'key1' => 'value1' })
9696
end
97+
98+
it 'normalizes metadata keys to strings' do
99+
allow(rest_metadata).to receive(:index)
100+
.with(uuid: file_uuid, request_options: {})
101+
.and_return(Uploadcare::Result.success({ key1: 'value1' }))
102+
103+
metadata_instance.index
104+
105+
expect(metadata_instance['key1']).to eq('value1')
106+
expect(metadata_instance.to_h).to eq({ 'key1' => 'value1' })
107+
end
97108
end
98109

99110
describe '#[] and #[]=' do
@@ -150,6 +161,17 @@
150161

151162
result = metadata_instance.show(key: 'color')
152163
expect(result).to eq('green')
164+
expect(metadata_instance['color']).to eq('green')
165+
end
166+
167+
it 'does not update local metadata when uuid differs' do
168+
allow(rest_metadata).to receive(:show)
169+
.with(uuid: 'other-uuid', key: 'color', request_options: {})
170+
.and_return(Uploadcare::Result.success('green'))
171+
172+
result = metadata_instance.show(key: 'color', uuid: 'other-uuid')
173+
expect(result).to eq('green')
174+
expect(metadata_instance['color']).to be_nil
153175
end
154176
end
155177

spec/uploadcare/resources/file_spec.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,22 @@
404404
expect(file.uuid).to eq(file_uuid)
405405
end
406406

407+
it 'does not extract uuid from non-Uploadcare original_file_url' do
408+
file = described_class.new(
409+
{ 'original_file_url' => "https://example.com/#{file_uuid}/photo.jpg" },
410+
client
411+
)
412+
expect(file.uuid).to be_nil
413+
end
414+
407415
it 'returns nil when no uuid source is available' do
408416
file = described_class.new({}, client)
409417
expect(file.uuid).to be_nil
410418
end
411419
end
412420

413421
describe '#cdn_url' do
414-
it 'returns the url attribute when set' do
422+
it 'returns the CDN url attribute when set' do
415423
file = described_class.new(file_attrs, client)
416424
expect(file.cdn_url).to eq("https://ucarecdn.com/#{file_uuid}/")
417425
end
@@ -420,5 +428,24 @@
420428
file = described_class.new({ 'uuid' => file_uuid }, client)
421429
expect(file.cdn_url).to eq("https://ucarecdn.com/#{file_uuid}/")
422430
end
431+
432+
it 'falls back to Uploadcare original_file_url when url is not a CDN URL' do
433+
file = described_class.new(
434+
{
435+
'url' => "https://api.uploadcare.com/files/#{file_uuid}/",
436+
'original_file_url' => "https://ucarecdn.com/#{file_uuid}/photo.jpg"
437+
},
438+
client
439+
)
440+
expect(file.cdn_url).to eq("https://ucarecdn.com/#{file_uuid}/photo.jpg")
441+
end
442+
443+
it 'ignores non-Uploadcare original_file_url values' do
444+
file = described_class.new(
445+
{ 'uuid' => file_uuid, 'original_file_url' => 'https://example.com/photo.jpg' },
446+
client
447+
)
448+
expect(file.cdn_url).to eq("https://ucarecdn.com/#{file_uuid}/")
449+
end
423450
end
424451
end

0 commit comments

Comments
 (0)