Class: Eymiha::UrlNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/eymiha/url/url_normalizer.rb

Overview

A UrlNormalizer normalizes URLs. They are validated using the URI class except when they can be identified as file resources. When so, if the designated file’s name starts with a slash (or a drive identifier followed by a slash on a windows box) the path is used verbatim. If not, the named file is assumed to be relative to a caller-supplied base directory.

Constant Summary collapse

@@using_pc_filesystem =
Config::CONFIG['target_vendor'] == "pc"
@@relative_base =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUrlNormalizer

Creates a new instance using the default direcory for normalizing relative file resources.



48
49
50
# File 'lib/eymiha/url/url_normalizer.rb', line 48

def initialize()
  @relative_base = @@relative_base
end

Instance Attribute Details

#relative_baseObject

Returns the value of attribute relative_base.



44
45
46
# File 'lib/eymiha/url/url_normalizer.rb', line 44

def relative_base
  @relative_base
end

Class Method Details

.absolute_file_path(file_url) ⇒ Object

grafts on the drive designator for an absolute file if not present and on a pc filesystem



92
93
94
95
# File 'lib/eymiha/url/url_normalizer.rb', line 92

def self.absolute_file_path file_url
  (@@using_pc_filesystem && (file_url =~ /^\//)) ?
    "C:#{file_url}" : file_url
end

.absolute_file_path?(file_url) ⇒ Boolean

Returns true if the given URL file resource is absolute.

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/eymiha/url/url_normalizer.rb', line 84

def self.absolute_file_path? file_url
  @@using_pc_filesystem ?
    ((file_url =~ /^[A-Za-z]:\//) or (file_url =~ /^\//)) :
    (!(file_url =~ /^[A-Za-z]:/) and (file_url =~ /^\//))
end

.file_url?(url) ⇒ Boolean

Returns true if the given URL is a file resource.

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/eymiha/url/url_normalizer.rb', line 60

def self.file_url? url
  (url =~ /^file:/) or
    (url =~ /^\//) or
    ((url =~ /^[A-Za-z]:/) and @@using_pc_filesystem) or
    !(url =~ /:/)
end

.relative_baseObject

Returns the default directory for normalizing relative file resources.



40
41
42
# File 'lib/eymiha/url/url_normalizer.rb', line 40

def self.relative_base
  @@relative_base
end

.relative_base=(relative_base) ⇒ Object

Sets and returns the default directory for normalizing relative file resources. The directory is validated prior to assignment - if invalid a UrlException will be raised.



31
32
33
34
35
36
37
# File 'lib/eymiha/url/url_normalizer.rb', line 31

def self.relative_base=(relative_base)
  if valid_relative_base? relative_base
    @@relative_base = relative_base
  else
    raise UrlException, "Invalid relative file base '#{relative_base}'"
  end
end

.valid_relative_base?(relative_base) ⇒ Boolean

Returns true if the given relative_base is valid. It should start and end with a slash (or start with a drive designator and a slash and end with a slash on a PC).

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/eymiha/url/url_normalizer.rb', line 22

def self.valid_relative_base? relative_base
  ((@@using_pc_filesystem and (relative_base =~ /^[A-Za-z]:\//)) or
   (!@@using_pc_filesystem and (relative_base =~ /^\//))) and
    (relative_base =~ /\/$/)
end

Instance Method Details

#normalize(url, relative_base = nil) ⇒ Object

Returns a normalized URL relative to the given base if it is a relative file resource.



54
55
56
57
# File 'lib/eymiha/url/url_normalizer.rb', line 54

def normalize(url,relative_base=nil)
  self.relative_base = relative_base if relative_base
  (self.class.file_url? url) ? normalize_file(url) : URI.parse(url).to_s
end

#normalize_file(file_url) ⇒ Object

Normalizes and returns a relative or absolute URL file resource.



68
69
70
# File 'lib/eymiha/url/url_normalizer.rb', line 68

def normalize_file file_url
  "file://#{normalize_file_path((file_url =~ /file:\/\//) ? $' : file_url)}"
end

#normalize_file_path(file_url) ⇒ Object

Returns an absolute path for the given URL file resource. This is either the incoming URL if absolute, or the URL anchored at the relative base if relative.



100
101
102
103
104
105
106
107
108
# File 'lib/eymiha/url/url_normalizer.rb', line 100

def normalize_file_path file_url
  if self.class.absolute_file_path? file_url
    self.class.absolute_file_path file_url
  elsif @relative_base != nil
    "#{relative_base}#{file_url}"
  else
    raise UrlException, "no relative file base was configured"
  end
end