File: url.rb

package info (click to toggle)
ruby-hocon 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ruby: 7,903; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 930 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
# encoding: utf-8

require 'uri'
require_relative '../../hocon/impl'

# There are several places in the Java codebase that
# use Java's URL constructor, and rely on it to throw
# a `MalformedURLException` if the URL isn't valid.
#
# Ruby doesn't really have a similar constructor /
# validator, so this is a little shim to hopefully
# make the ported code match up with the upstream more
# closely.
class Hocon::Impl::Url
  class MalformedUrlError < StandardError
    def initialize(msg, cause = nil)
      super(msg)
      @cause = cause
    end
  end

  def initialize(url)
    begin
      # URI::parse wants a string
      @url = URI.parse(url.to_s)
      if !(@url.kind_of?(URI::HTTP))
        raise MalformedUrlError, "Unrecognized URL: '#{url}'"
      end
    rescue URI::InvalidURIError => e
      raise MalformedUrlError.new("Unrecognized URL: '#{url}' (error: #{e})", e)
    end
  end

  def to_s
    @url.to_s
  end
end