Skip to content

Commit

Permalink
Removed all references to Addressable outside JSON::Util::URI
Browse files Browse the repository at this point in the history
Hopefully this will make it easier to change in future
  • Loading branch information
Iain Beeston committed Dec 24, 2015
1 parent 8c1f270 commit ef36232
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lib/json-schema/attributes/formats/uri.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'json-schema/attribute'
require 'addressable/uri'
require 'json-schema/errors/uri_error'

module JSON
class Schema
class UriFormat < FormatAttribute
Expand All @@ -8,7 +9,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
error_message = "The property '#{build_fragment(fragments)}' must be a valid URI"
begin
JSON::Util::URI.parse(data)
rescue Addressable::URI::InvalidURIError
rescue JSON::Schema::UriError
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def self.get_referenced_uri_and_schema(s, current_schema, validator)
fragment_path = ''
fragments.each do |fragment|
if fragment && fragment != ''
fragment = Addressable::URI.unescape(fragment.gsub('~0', '~').gsub('~1', '/'))
fragment = JSON::Util::URI.unescaped_uri(fragment.gsub('~0', '~').gsub('~1', '/'))
if target_schema.is_a?(Array)
target_schema = target_schema[fragment.to_i]
else
Expand Down
6 changes: 6 additions & 0 deletions lib/json-schema/errors/uri_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module JSON
class Schema
class UriError < StandardError
end
end
end
7 changes: 2 additions & 5 deletions lib/json-schema/schema/reader.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'open-uri'
require 'addressable/uri'
require 'pathname'

module JSON
Expand Down Expand Up @@ -62,7 +61,7 @@ def initialize(options = {})
def read(location)
uri = JSON::Util::URI.parse(location.to_s)
body = if uri.scheme.nil? || uri.scheme == 'file'
uri = Addressable::URI.convert_path(uri.path)
uri = JSON::Util::URI.file_uri(uri)
read_file(Pathname.new(uri.path).expand_path)
else
read_uri(uri)
Expand All @@ -71,8 +70,6 @@ def read(location)
JSON::Schema.new(JSON::Validator.parse(body), uri)
end

# @param uri [Addressable::URI]
# @return [Boolean]
def accept_uri?(uri)
if @accept_uri.respond_to?(:call)
@accept_uri.call(uri)
Expand Down Expand Up @@ -103,7 +100,7 @@ def read_uri(uri)

def read_file(pathname)
if accept_file?(pathname)
File.read(Addressable::URI.unescape(pathname.to_s))
File.read(JSON::Util::URI.unescaped_uri(pathname.to_s))
else
raise JSON::Schema::ReadRefused.new(pathname.to_s, :file)
end
Expand Down
18 changes: 17 additions & 1 deletion lib/json-schema/util/uri.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'addressable/uri'

module JSON
module Util
module URI
Expand All @@ -13,7 +15,7 @@ def self.normalized_uri(uri, base_path = Dir.pwd)
if normalized_uri.relative?
data = normalized_uri
data = File.join(base_path, data) if data.path[0,1] != "/"
normalized_uri = Addressable::URI.convert_path(data)
normalized_uri = file_uri(data)
end
@normalize_cache[uri] = normalized_uri.freeze
end
Expand All @@ -26,6 +28,8 @@ def self.parse(uri)

@parse_cache ||= {}
@parse_cache[uri] ||= Addressable::URI.parse(uri).freeze
rescue Addressable::URI::InvalidURIError => e
raise JSON::Schema::UriError.new(e.message)
end

def self.strip_fragment(uri)
Expand All @@ -36,6 +40,18 @@ def self.strip_fragment(uri)
parsed_uri.merge(:fragment => "")
end
end

def self.file_uri(uri)
parsed_uri = parse(uri)

Addressable::URI.convert_path(parsed_uri.path)
end

def self.unescaped_uri(uri)
parsed_uri = parse(uri)

Addressable::URI.unescape(parsed_uri.path)
end
end
end
end
3 changes: 1 addition & 2 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'addressable/uri'
require 'open-uri'
require 'pathname'
require 'bigdecimal'
Expand Down Expand Up @@ -609,7 +608,7 @@ def custom_open(uri)
end
else
begin
File.read(Addressable::URI.unescape(uri.path))
File.read(JSON::Util::URI.unescaped_uri(uri))
rescue SystemCallError => e
raise JSON::Schema::JsonLoadError, e.message
end
Expand Down

0 comments on commit ef36232

Please sign in to comment.