Skip to content

Commit 9821a80

Browse files
committed
Fix cname cache scoping and tighten error handling
1 parent 6b7f9ed commit 9821a80

5 files changed

Lines changed: 18 additions & 12 deletions

File tree

lib/uploadcare/cname_generator.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class CnameGenerator
1111

1212
class << self
1313
def cdn_base_postfix
14-
@cdn_base_postfix ||= begin
14+
@cdn_base_postfix_cache ||= {}
15+
key = [Uploadcare.configuration.cdn_base_postfix, Uploadcare.configuration.public_key]
16+
@cdn_base_postfix_cache[key] ||= begin
1517
uri = URI.parse(Uploadcare.configuration.cdn_base_postfix)
1618
uri.host = "#{generate_cname}.#{uri.host}"
1719
uri.to_s
@@ -28,10 +30,11 @@ def generate_cname
2830

2931
# Generate CNAME prefix
3032
def custom_cname
31-
@custom_cname ||= begin
32-
public_key = Uploadcare.configuration.public_key
33-
raise Uploadcare::Exception::ConfigurationError, "Invalid public_key: #{public_key}" if public_key.nil?
33+
@custom_cname_cache ||= {}
34+
public_key = Uploadcare.configuration.public_key
35+
raise Uploadcare::Exception::ConfigurationError, "Invalid public_key: #{public_key}" if public_key.nil?
3436

37+
@custom_cname_cache[public_key] ||= begin
3538
sha256_hex = Digest::SHA256.hexdigest(public_key)
3639
sha256_hex = sha256_hex.to_i(16)
3740
sha256_base36 = sha256_hex.to_s(36)

lib/uploadcare/error_handler.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module ErrorHandler
2222
# @raise [Uploadcare::Exception::RequestError] for other error statuses
2323
def handle_error(error)
2424
response = error.response
25+
return raise Exception::RequestError, error.message if response.nil?
26+
2527
catch_upload_errors(response)
2628

2729
error_message = extract_error_message(response)

lib/uploadcare/param/upload/upload_params_generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def store_value(store)
3333

3434
def metadata(options:)
3535
return {} if options[:metadata].nil?
36+
raise ArgumentError, 'metadata must be a hash' unless options[:metadata].is_a?(Hash)
3637

3738
options[:metadata].each_with_object({}) do |(k, v), res|
3839
res.merge!("metadata[#{k}]" => v.to_s)

spec/uploadcare/cname_generator_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
RSpec.describe Uploadcare::CnameGenerator do
66
before do
77
# Reset memoized variables between tests
8-
described_class.instance_variable_set(:@custom_cname, nil)
9-
described_class.instance_variable_set(:@cdn_base_postfix, nil)
8+
described_class.instance_variable_set(:@custom_cname_cache, nil)
9+
described_class.instance_variable_set(:@cdn_base_postfix_cache, nil)
1010
# Reset configuration
1111
Uploadcare.instance_variable_set(:@configuration, nil)
1212
end
@@ -33,7 +33,7 @@
3333
end
3434

3535
it 'handles different CDN bases' do
36-
described_class.instance_variable_set(:@cdn_base_postfix, nil)
36+
described_class.instance_variable_set(:@cdn_base_postfix_cache, nil)
3737
allow(Uploadcare.configuration).to receive(:cdn_base_postfix).and_return('https://example.com')
3838
allow(described_class).to receive(:custom_cname).and_return('xyz789')
3939

@@ -50,7 +50,7 @@
5050
end
5151

5252
it 'handles CDN base with path' do
53-
described_class.instance_variable_set(:@cdn_base_postfix, nil)
53+
described_class.instance_variable_set(:@cdn_base_postfix_cache, nil)
5454
allow(Uploadcare.configuration).to receive(:cdn_base_postfix).and_return('https://cdn.example.com/path/')
5555
allow(described_class).to receive(:custom_cname).and_return('prefix123')
5656

@@ -72,7 +72,7 @@
7272

7373
invalid_urls.each do |invalid_url|
7474
# Reset memoization for each test
75-
described_class.instance_variable_set(:@cdn_base_postfix, nil)
75+
described_class.instance_variable_set(:@cdn_base_postfix_cache, nil)
7676
allow(Uploadcare.configuration).to receive(:cdn_base_postfix).and_return(invalid_url)
7777
allow(described_class).to receive(:generate_cname).and_return('test123')
7878

@@ -110,7 +110,7 @@
110110
result1 = described_class.send(:custom_cname)
111111

112112
# Reset memoization
113-
described_class.instance_variable_set(:@custom_cname, nil)
113+
described_class.instance_variable_set(:@custom_cname_cache, nil)
114114

115115
allow(Uploadcare.configuration).to receive(:public_key).and_return('key2')
116116
result2 = described_class.send(:custom_cname)
@@ -213,7 +213,7 @@
213213
]
214214

215215
test_cases.each do |cdn_base|
216-
described_class.instance_variable_set(:@cdn_base_postfix, nil)
216+
described_class.instance_variable_set(:@cdn_base_postfix_cache, nil)
217217
allow(Uploadcare.configuration).to receive(:cdn_base_postfix).and_return(cdn_base)
218218
allow(described_class).to receive(:custom_cname).and_return('test123')
219219

spec/uploadcare/param/upload/upload_params_generator_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353

5454
expect do
5555
described_class.call(options: { metadata: 'nope' }, config: config)
56-
end.to raise_error(NoMethodError)
56+
end.to raise_error(ArgumentError, 'metadata must be a hash')
5757
end
5858
end

0 commit comments

Comments
 (0)