File: uri.rb

package info (click to toggle)
ruby-json-schema 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 748 kB
  • ctags: 656
  • sloc: ruby: 5,380; makefile: 6
file content (68 lines) | stat: -rw-r--r-- 1,750 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
62
63
64
65
66
67
68
require 'addressable/uri'

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

      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

      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

      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

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

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

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

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

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