Class: UriPathname

Inherits:
Object
  • Object
show all
Defined in:
lib/uri_pathname/version.rb,
lib/uri_pathname.rb

Overview

it’s only a version number

Defined Under Namespace

Modules: Version Classes: InvalidPathName, InvalidURI

Constant Summary collapse

PTH_SEP =

used to swap chars from and to ‘/’ while converting to

'_|_'
HOST_SEP =

pathname and viceversa. To work it should be a String that doesn’t exist in URIs and that can be used in pathnames.

'__|'
NO_PTH =

used to separate hostname from the path

'_NOPATH_'
BASE_DIR =

To work it should be a String that doesn’t exist in uris

''
HOST_2_DIR =

dir prepended to all pathnames

true
DEFAULT_ATTRS =

default attributes

{
  :pth_sep => PTH_SEP,
  :host_sep => HOST_SEP,
  :no_pth => NO_PTH,
  :base_dir => BASE_DIR,
  :host_2_dir => HOST_2_DIR
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UriPathname

Initializes an UriPathname that can be used to convert between URIs and pathnames. options is a hash that can contain any of:

:pth_sep => String that will be used to substitute '/' inside URI's path and queries (default UriPathname::PTH_SEP)
:host_sep => String that will be used to substitute '/' between host and path (default UriPathname::HOST_SEP)
:no_pth => String that will be used as a path placeholder when no URI's path exists, (default UriPathname::NO_PTH)
:base_dir => String containing base directory prepended to any pathname, (default UriPathname::DEFAULT_DIR)
:host_2_dir => If true, hostnames will become subdirectories (default UriPathname::HOST_2_DIR)


44
45
46
47
48
49
50
# File 'lib/uri_pathname.rb', line 44

def initialize(options = {})

  attributes = DEFAULT_ATTRS.merge options if options.is_a? Hash

  attributes.each { |k,v| instance_eval("@#{k}='#{v}'") if DEFAULT_ATTRS.keys.include?(k) }

end

Instance Method Details

#parse(pathname) ⇒ Object

splits up a pathname and returns an array where:

  1. the 3 first elements correspond to Pathname

- arr[0] = dirname
- arr[1] = basename (without extension)
- arr[2] = extension
  1. the last elements correspond to URI

- arr[3] = scheme
- arr[4] = hostname
- arr[5] = path
- arr[6] = query
- arr[7] = URI (complete)

returns nil if pathname doesn’t correspond to UriPathname format (see UriPathname::uri_to_pathname)



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/uri_pathname.rb', line 105

def parse(pathname)
  # raise(InvalidPathName, "pathname should be a String") \
  #   unless pathname.respond_to? :to_s
  return nil unless pathname.respond_to? :to_s
  complete_pathname = File.expand_path(pathname.to_s)

  # pathname parsed from complete_pathname
  extension = complete_pathname.slice!(/(\.[^\)\(\/]*)?$/)
  dirname, basename = File.split(complete_pathname)
  
  # further uri restored
  # hostname
  if @host_sep == '/'
    path_to_host, rest_of_thing = dirname, basename
    hostname = File.basename(path_to_host)
  else
    rest_of_thing = basename
    hostname, rest_of_thing = rest_of_thing.split(@host_sep)
  end
  # path, query and scheme
  return nil unless rest_of_thing =~ /(.*)\(([^\(]+)\)$/ and !hostname.empty?
  path_query, scheme= rest_of_thing.match(/(.*)\(([^\(]+)\)$/)[1..2]
  uri = scheme + '://' + hostname
  case path_query
  when @no_pth
    path = ''
    query = ''
  else
    path, query = path_query.gsub(@pth_sep,"/").split("?")
  end
  uri += "/" + path unless path.nil? or path.empty?
  uri += "?" + query unless query.nil? or query.empty?
  arr = []
  arr << dirname << basename << extension << scheme << hostname << path << query << uri
end

#pathname_to_uri(pathname) ⇒ Object

converts pathname to an URI. UriPathname::PTH_SEP appearances will be substituted by “/” (the standard path separator). returns the URI String if pathname was convertible or nil in other case.



87
88
89
90
# File 'lib/uri_pathname.rb', line 87

def pathname_to_uri(pathname)
  pn = self.parse(pathname)
  pn ? pn[7] : nil
end

#uri_to_pathname(uri, basedirpath = nil, extension = "") ⇒ Object

Converts an uri to a valid pathname, that is:

[_DIR_/]_hostname_HS_path_?_query_(_scheme_)[._extension_]

where

DIR is +basedirpath+ if given else _@base_dir_
HS is UriPathname::HOST_SEP
all '/' within _path_ and _query_ will be substituted by

UriPathname::PTH_SEP value.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/uri_pathname.rb', line 59

def uri_to_pathname(uri,basedirpath=nil,extension="")
  arr = URI.split uri
  # raise(InvalidURI, "#{uri} lacks scheme and/or host") unless (arr[0] && arr[2])
  return nil unless (arr[0] && arr[2])
  scheme = "(" << arr[0] << ")"
  host = arr[2]
  path = arr[5]
  if path.size <= 1 # "" or "/"
    path = @no_pth
  else
    path=path[1..-1].gsub("/",@pth_sep) # first '/' isn't necessary
  end
  query = arr[7]
  query = if query
    "?" << query.gsub("/",@pth_sep)
  else
    ""
  end
  extension='' unless extension
  extension.insert(0,'.') unless extension.empty? or extension.start_with? '.'
  pathname = host << @host_sep << path << query << scheme << extension
  basedirpath = @base_dir unless basedirpath
  basedirpath.size > 0 ? File.expand_path(pathname, basedirpath) : pathname
end