Module: Rufus::Jig::Path

Defined in:
lib/rufus/jig/path.rb

Overview

Some helper methods for URI paths.

Class Method Summary collapse

Class Method Details

.add_params(path, h) ⇒ Object

Given a path and a hash of options, ...

p Rufus::Jig::Path.add_params('/toto', :q => 'nada')
# => "/toto?q=nada"

p Rufus::Jig::Path.add_params('/toto?rev=2', :q => 'nada')
# => "/toto?rev=2&q=nada"

p Rufus::Jig::Path.add_params('/toto', :q => 'nada', 'x' => 2)
# => "/toto?q=nada&x=2"


44
45
46
47
48
49
50
51
# File 'lib/rufus/jig/path.rb', line 44

def self.add_params(path, h)

  params = h.collect { |k, v| "#{k}=#{v}" }.join('&')

  sep = path.index('?') ? '&' : '?'

  params.length > 0 ? "#{path}#{sep}#{params}" : path
end

.join(*elts) ⇒ Object

Joins paths into a '/' prefixed path.

p Rufus::Jig::Path.join('division', 'customer', :restricted => true)
# => '/division/customer?restricted=true'


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rufus/jig/path.rb', line 58

def self.join(*elts)

  elts, params = if elts.last.is_a?(Hash)
    [ elts[0..-2], elts.last ]
  else
    [ elts, {} ]
  end

  r = elts.collect { |e|

    e = to_path(e)

    if m = e.match(/^(.*)\/$/)
      m[1]
    else
      e
    end
  }.join

  add_params(r, params)
end

.to_name(s) ⇒ Object

Removes any forward slashes at the beginning of the given string.



89
90
91
92
93
94
95
96
# File 'lib/rufus/jig/path.rb', line 89

def self.to_name(s)

  if m = s.match(/^\/+(.+)$/)
    m[1]
  else
    s
  end
end

.to_path(s) ⇒ Object

Makes sure there is a forward slash in the given string.



82
83
84
85
# File 'lib/rufus/jig/path.rb', line 82

def self.to_path(s)

  s.match(/^\//) ? s : "/#{s}"
end