Class: Docs::URL

Inherits:
URI::Generic
  • Object
show all
Defined in:
lib/docs/core/url.rb

Constant Summary collapse

PARSER =
URI::Parser.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ URL

Returns a new instance of URL


8
9
10
11
12
13
14
15
16
17
# File 'lib/docs/core/url.rb', line 8

def initialize(*args)
  if args.empty?
    super(*Array.new(9))
  elsif args.length == 1 && args.first.is_a?(Hash)
    args.first.assert_valid_keys URI::Generic::COMPONENT
    super(*args.first.values_at(*URI::Generic::COMPONENT))
  else
    super
  end
end

Class Method Details

.join(*args) ⇒ Object


24
25
26
# File 'lib/docs/core/url.rb', line 24

def self.join(*args)
  PARSER.join(*args)
end

.parse(url) ⇒ Object


19
20
21
22
# File 'lib/docs/core/url.rb', line 19

def self.parse(url)
  return url if url.kind_of? self
  new(*PARSER.split(url), PARSER)
end

Instance Method Details

#contains?(url, options = nil) ⇒ Boolean

Returns:

  • (Boolean)

84
85
86
# File 'lib/docs/core/url.rb', line 84

def contains?(url, options = nil)
  !!subpath_to(url, options)
end

#join(*args) ⇒ Object


28
29
30
# File 'lib/docs/core/url.rb', line 28

def join(*args)
  self.class.join self, *args
end

#merge(hash) ⇒ Object


41
42
43
44
# File 'lib/docs/core/url.rb', line 41

def merge(hash)
  return super unless hash.is_a? Hash
  dup.merge!(hash)
end

#merge!(hash) ⇒ Object


32
33
34
35
36
37
38
39
# File 'lib/docs/core/url.rb', line 32

def merge!(hash)
  return super unless hash.is_a? Hash
  hash.assert_valid_keys URI::Generic::COMPONENT
  hash.each_pair do |key, value|
    send "#{key}=", value
  end
  self
end

#normalized_pathObject


57
58
59
# File 'lib/docs/core/url.rb', line 57

def normalized_path
  path == '' ? '/' : path
end

#originObject


46
47
48
49
50
51
52
53
54
55
# File 'lib/docs/core/url.rb', line 46

def origin
  if scheme && host
    origin = "#{scheme}://#{host}"
    origin.downcase!
    origin << ":#{port}" if port
    origin
  else
    nil
  end
end

#relative_path_from(url) ⇒ Object


107
108
109
# File 'lib/docs/core/url.rb', line 107

def relative_path_from(url)
  self.class.parse(url).relative_path_to(self)
end

#relative_path_to(url) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/docs/core/url.rb', line 88

def relative_path_to(url)
  url = self.class.parse(url)
  return unless origin == url.origin

  base_dir = Pathname.new(normalized_path)
  base_dir = base_dir.parent unless path.end_with? '/'

  dest = url.normalized_path
  dest_dir = Pathname.new(dest)

  if dest.end_with? '/'
    dest_dir.relative_path_from(base_dir).to_s.tap do |result|
      result << '/' if result != '.'
    end
  else
    dest_dir.parent.relative_path_from(base_dir).join(::File.basename(dest)).to_s
  end
end

#subpath_from(url, options = nil) ⇒ Object


80
81
82
# File 'lib/docs/core/url.rb', line 80

def subpath_from(url, options = nil)
  self.class.parse(url).subpath_to(self, options)
end

#subpath_to(url, options = nil) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/docs/core/url.rb', line 61

def subpath_to(url, options = nil)
  url = self.class.parse(url)
  return unless origin == url.origin

  base = path
  dest = url.path

  if options && options[:ignore_case]
    base = base.downcase
    dest = dest.downcase
  end

  if base == dest
    ''
  elsif dest.start_with? File.join(base, '')
    url.path[(path.length)..-1]
  end
end