Skip to content

Commit

Permalink
Rename methods back to reduce diff
Browse files Browse the repository at this point in the history
  • Loading branch information
bolshakov authored and bastelfreak committed Aug 19, 2024
1 parent 7c5d323 commit a16e11b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def self.get_referenced_uri_and_schema(s, current_schema, validator)
if ref_schema
# Perform fragment resolution to retrieve the appropriate level for the schema
target_schema = ref_schema.schema
fragments = JSON::Util::URI.parse(JSON::Util::URI.unescape(temp_uri)).fragment.split('/')
fragments = JSON::Util::URI.parse(JSON::Util::URI.unescape_uri(temp_uri)).fragment.split('/')
fragment_path = ''
fragments.each do |fragment|
if fragment && fragment != ''
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/schema/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def read_uri(uri)

def read_file(pathname)
if accept_file?(pathname)
File.read(JSON::Util::URI.unescape_path(pathname.to_s))
File.read(JSON::Util::URI.unescaped_path(pathname.to_s))
else
raise JSON::Schema::ReadRefused.new(pathname.to_s, :file)
end
Expand Down
16 changes: 9 additions & 7 deletions lib/json-schema/util/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class URI < Addressable::URI
SUPPORTED_PROTOCOLS = %w(http https ftp tftp sftp ssh svn+ssh telnet nntp gopher wais ldap prospero)

class << self
alias unescape_uri unescape

# @param uri [String, Addressable::URI]
# @return [Addressable::URI, nil]
def parse(uri)
Expand All @@ -24,8 +26,8 @@ def file_uri(uri)

# @param uri [String, Addressable::URI
# @return [String]
def unescape_path(uri)
parse(uri).unescape_path
def unescaped_path(uri)
parse(uri).unescaped_path
end

# Strips the fragment from the URI.
Expand All @@ -37,8 +39,8 @@ def strip_fragment(uri)

# @param uri [String, Addressable::URI]
# @return [Addressable::URI]
def normalize_uri(uri, base_path = Dir.pwd)
parse(uri).normalize_uri(base_path)
def normalized_uri(uri, base_path = Dir.pwd)
parse(uri).normalized_uri(base_path)
end

# Normalizes the reference URI based on the provided base URI
Expand All @@ -58,7 +60,7 @@ def absolutize_ref(ref, base)
# Unencodes any percent encoded characters within a path component.
#
# @return [String]
def unescape_path
def unescaped_path
self.class.unescape_component(path)
end

Expand All @@ -76,7 +78,7 @@ def strip_fragment
#
# @param base_path [String] the base path to use for relative URIs. Defaults to the current working directory.
# @return [Addressable::URI] the normalized URI or nil
def normalize_uri(base_path = Dir.pwd)
def normalized_uri(base_path = Dir.pwd)
if relative?
if path[0, 1] == '/'
self.class.file_uri(self)
Expand Down Expand Up @@ -122,7 +124,7 @@ def absolutize_ref(base)
if ref.absolute?
ref
else
self.class.strip_fragment(base).join(ref.path).normalize_uri
self.class.strip_fragment(base).join(ref.path).normalized_uri
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def schema_loaded?(schema_uri)
end

def schema_key_for(uri)
key = Util::URI.normalize_uri(uri).to_s
key = Util::URI.normalized_uri(uri).to_s
key.end_with?('#') ? key : "#{key}#"
end

Expand Down Expand Up @@ -534,7 +534,7 @@ def initialize_schema(schema, default_validator)
self.class.add_schema(schema)
rescue JSON::Schema::JsonParseError
# Build a uri for it
schema_uri = Util::URI.normalize_uri(schema)
schema_uri = Util::URI.normalized_uri(schema)
if !self.class.schema_loaded?(schema_uri)
schema = @options[:schema_reader].read(schema_uri)
schema = JSON::Schema.stringify(schema)
Expand Down Expand Up @@ -574,7 +574,7 @@ def initialize_data(data)
if @options[:json]
data = self.class.parse(data)
elsif @options[:uri]
json_uri = Util::URI.normalize_uri(data)
json_uri = Util::URI.normalized_uri(data)
data = self.class.parse(custom_open(json_uri))
elsif data.is_a?(String)
begin
Expand All @@ -583,7 +583,7 @@ def initialize_data(data)
data = strict_convert ? data : self.class.parse(data)
rescue JSON::Schema::JsonParseError
begin
json_uri = Util::URI.normalize_uri(data)
json_uri = Util::URI.normalized_uri(data)
data = self.class.parse(custom_open(json_uri))
rescue JSON::Schema::JsonLoadError, JSON::Schema::UriError
# Silently discard the error - use the data as-is
Expand All @@ -595,7 +595,7 @@ def initialize_data(data)
end

def custom_open(uri)
uri = Util::URI.normalize_uri(uri) if uri.is_a?(String)
uri = Util::URI.normalized_uri(uri) if uri.is_a?(String)
if uri.absolute? && Util::URI::SUPPORTED_PROTOCOLS.include?(uri.scheme)
begin
URI.open(uri.to_s).read
Expand All @@ -604,7 +604,7 @@ def custom_open(uri)
end
else
begin
File.read(JSON::Util::URI.unescape_path(uri))
File.read(JSON::Util::URI.unescaped_path(uri))
rescue SystemCallError => e
raise JSON::Schema::JsonLoadError, e.message
end
Expand Down
12 changes: 6 additions & 6 deletions test/uri_util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def test_normalized_uri
uri = Addressable::URI.new(scheme: 'https',
host: 'www.google.com',
path: 'search',)
assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home')
assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
end

def test_normalized_uri_with_empty_fragment
Expand All @@ -15,7 +15,7 @@ def test_normalized_uri_with_empty_fragment
host: 'www.google.com',
path: 'search',
fragment: nil,)
assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home')
assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
end

def test_normalized_uri_with_fragment
Expand All @@ -24,31 +24,31 @@ def test_normalized_uri_with_fragment
host: 'www.google.com',
path: 'search',
fragment: 'foo',)
assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home')
assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
end

def test_normalized_uri_for_absolute_path
str = '/foo/bar.json'
uri = Addressable::URI.new(scheme: 'file',
host: '',
path: '/foo/bar.json',)
assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home')
assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
end

def test_normalized_uri_for_relative_path
str = 'foo/bar.json'
uri = Addressable::URI.new(scheme: 'file',
host: '',
path: '/home/foo/bar.json',)
assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home')
assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
end

def test_normalized_uri_for_file_path_with_host
str = 'file://localhost/foo/bar.json'
uri = Addressable::URI.new(scheme: 'file',
host: 'localhost',
path: '/foo/bar.json',)
assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home')
assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
end

def test_uri_parse
Expand Down

0 comments on commit a16e11b

Please sign in to comment.