Module: JSON::Util::URI

Defined in:
lib/json-schema/util/uri.rb

Constant Summary collapse

SUPPORTED_PROTOCOLS =
%w(http https ftp tftp sftp ssh svn+ssh telnet nntp gopher wais ldap prospero)

Class Method Summary collapse

Class Method Details

.absolutize_ref(ref, base) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/json-schema/util/uri.rb', line 26

def self.absolutize_ref(ref, base)
  ref_uri = strip_fragment(ref.dup)

  return ref_uri if ref_uri.absolute?
  return parse(base) if ref_uri.path.empty?

  uri = strip_fragment(base.dup).join(ref_uri.path)
  normalized_uri(uri)
end

.clear_cacheObject



104
105
106
107
# File 'lib/json-schema/util/uri.rb', line 104

def self.clear_cache
  @parse_cache = {}
  @normalize_cache = {}
end

.file_uri(uri) ⇒ Object



88
89
90
91
92
# File 'lib/json-schema/util/uri.rb', line 88

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

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

.normalize_ref(ref, base) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/json-schema/util/uri.rb', line 36

def self.normalize_ref(ref, base)
  ref_uri = parse(ref)
  base_uri = parse(base)

  ref_uri.defer_validation do
    if ref_uri.relative?
      ref_uri.merge!(base_uri)

      # Check for absolute path
      path, fragment = ref.to_s.split("#")
      if path.nil? || path == ''
        ref_uri.path = base_uri.path
      elsif path[0,1] == "/"
        ref_uri.path = Pathname.new(path).cleanpath.to_s
      else
        ref_uri.join!(path)
      end

      ref_uri.fragment = fragment
    end

    ref_uri.fragment = "" if ref_uri.fragment.nil? || ref_uri.fragment.empty?
  end

  ref_uri
end

.normalized_uri(uri, base_path = Dir.pwd) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/json-schema/util/uri.rb', line 8

def self.normalized_uri(uri, base_path = Dir.pwd)
  @normalize_cache ||= {}
  normalized_uri = @normalize_cache[uri]

  if !normalized_uri
    normalized_uri = parse(uri)
    # Check for absolute path
    if normalized_uri.relative?
      data = normalized_uri
      data = File.join(base_path, data) if data.path[0,1] != "/"
      normalized_uri = file_uri(data)
    end
    @normalize_cache[uri] = normalized_uri.freeze
  end

  normalized_uri
end

.parse(uri) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/json-schema/util/uri.rb', line 63

def self.parse(uri)
  if uri.is_a?(Addressable::URI)
    return uri.dup
  else
    @parse_cache ||= {}
    parsed_uri = @parse_cache[uri]
    if parsed_uri
      parsed_uri.dup
    else
      @parse_cache[uri] = Addressable::URI.parse(uri)
    end
  end
rescue Addressable::URI::InvalidURIError => e
  raise JSON::Schema::UriError.new(e.message)
end

.strip_fragment(uri) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/json-schema/util/uri.rb', line 79

def self.strip_fragment(uri)
  parsed_uri = parse(uri)
  if parsed_uri.fragment.nil? || parsed_uri.fragment.empty?
    parsed_uri
  else
    parsed_uri.merge(:fragment => "")
  end
end

.unescape_uri(uri) ⇒ Object



94
95
96
# File 'lib/json-schema/util/uri.rb', line 94

def self.unescape_uri(uri)
  Addressable::URI.unescape(uri)
end

.unescaped_path(uri) ⇒ Object



98
99
100
101
102
# File 'lib/json-schema/util/uri.rb', line 98

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

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