Class: Microformats::AbsoluteUri

Inherits:
Object
  • Object
show all
Defined in:
lib/microformats/absolute_uri.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relative, base: nil) ⇒ AbsoluteUri

Returns a new instance of AbsoluteUri.



5
6
7
8
9
10
# File 'lib/microformats/absolute_uri.rb', line 5

def initialize(relative, base: nil)
  @base = base
  @relative = relative
  @base = base.strip unless base.nil?
  @relative = relative.strip unless relative.nil?
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



3
4
5
# File 'lib/microformats/absolute_uri.rb', line 3

def base
  @base
end

#relativeObject

Returns the value of attribute relative.



3
4
5
# File 'lib/microformats/absolute_uri.rb', line 3

def relative
  @relative
end

Instance Method Details

#absolutizeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/microformats/absolute_uri.rb', line 12

def absolutize
  return relative if base.nil?
  return base if relative.nil? || relative == ''
  return relative if relative =~ %r{^https?://}
  return base + relative if relative =~ /^#/

  uri = URI.parse(relative)
  uri = URI.join(base.to_s, relative.to_s) if base && !uri.absolute?

  uri.normalize!
  uri.to_s
rescue URI::BadURIError, URI::InvalidURIError
  relative.to_s
end