Skip to content

Commit d584063

Browse files
committed
(maint) Use more type dispatch
1 parent 1105f1f commit d584063

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

lib/puppet/functions/stdlib/loadjson.rb

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,19 @@
2626
# If URL is in the format of https://username@example.local/my_info.json
2727
URI_WITH_NAME_PATTERN = %r{(http\://|https\://)(.*)@(.*)}
2828

29-
dispatch :default_impl do
30-
param 'String', :path_or_uri
29+
dispatch :load_from_url do
30+
param 'Variant[Stdlib::HttpUrl, Stdlib::HttpsUrl]', :url
3131
optional_param Any, :default
3232
end
3333

34-
def default_impl(path_or_uri, default = nil)
35-
begin
36-
if path_or_uri.start_with?('http://', 'https://')
37-
load_from_uri(path_or_uri, default)
38-
39-
elsif File.exists?(path_or_uri)
40-
content = File.read(path_or_uri)
41-
42-
Puppet::Util::Json.load(content) || default
43-
else
44-
warning("Can't load '#{path_or_uri}' File does not exist!")
34+
dispatch :load_local do
35+
param 'Stdlib::Absolutepath', :path
36+
optional_param Any, :default
37+
end
4538

46-
default
47-
end
39+
def with_error_or_default(default = nil, &block)
40+
begin
41+
yield
4842
rescue StandardError => err
4943
if default
5044
default
@@ -54,31 +48,46 @@ def default_impl(path_or_uri, default = nil)
5448
end
5549
end
5650

57-
private
58-
def load_from_uri(uri, default)
59-
require 'open-uri'
51+
def load_local(path, default)
52+
with_error_or_default(default) do
53+
if File.exists?(path)
54+
content = File.read(path)
55+
56+
Puppet::Util::Json.load(content) || default
57+
else
58+
warning("Can't load '#{path}' File does not exist!")
6059

61-
username = ''
62-
password = ''
63-
if (match = uri.match(URI_WITH_NAME_AND_PASS_PATTERN))
64-
protocol, username, password, path = match.captures
65-
url = "#{protocol}#{path}"
66-
elsif (match = uri.match(URI_WITH_NAME_PATTERN))
67-
protocol, username, path = match.captures
68-
url = "#{protocol}#{path}"
69-
else
70-
url = uri
60+
default
61+
end
7162
end
63+
end
7264

73-
begin
74-
contents = OpenURI.open_uri(url, :http_basic_authentication => [username, password])
75-
rescue OpenURI::HTTPError => err
76-
res = err.io
77-
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
65+
def load_from_url(url, default)
66+
with_error_or_default(default) do
67+
require 'open-uri'
7868

79-
default
80-
end
69+
username = ''
70+
password = ''
71+
if (match = url.match(URI_WITH_NAME_AND_PASS_PATTERN))
72+
protocol, username, password, path = match.captures
73+
url = "#{protocol}#{path}"
74+
elsif (match = url.match(URI_WITH_NAME_PATTERN))
75+
protocol, username, path = match.captures
76+
url = "#{protocol}#{path}"
77+
else
78+
url = url
79+
end
80+
81+
begin
82+
contents = OpenURI.open_uri(url, :http_basic_authentication => [username, password])
83+
rescue OpenURI::HTTPError => err
84+
res = err.io
85+
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
86+
87+
default
88+
end
8189

82-
Puppet::Util::Json.load(contents) || default
90+
Puppet::Util::Json.load(contents) || default
91+
end
8392
end
8493
end

0 commit comments

Comments
 (0)