Class: Octarine::Path

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/octarine/path.rb

Overview

Octarine::Path represents the path and query string portion of a url.

You are unlikely to need to create a Path instance yourself, insted you will usually obtain one from Request#path.

If you set a handler like so:

get "/users/:user_id/messages/:message_id" {|request| ... }

and a request is made like:

GET /users/1234/messages/4567?history=true

then ‘request.path` will behave as below:

path.user_id        #=> "1234"
path[:user_id]      #=> "1234"
path.message_id     #=> "5678"
path[:message_id]   #=> "5678"
path["history"]     #=> "true"
path.to_s           #=> "/users/1234/messages/4567?history=true"
path.path           #=> "/users/1234/messages/4567"
path.to_hash        #=> {:user_id=>"1234", :message_id=>"5678", "history"=>"true"}

The following methods are available and behave as if the path was a hash: assoc, [], each, each_key, each_pair, each_value, empty?, fetch, has_key?, has_value?, key, key?, keys, rassoc, to_a, to_hash, value?, values, values_at and all Enumerable methods

The following methods are available and behave as if path was a string:

~, bytesize, length, size

Instance Method Summary collapse

Constructor Details

#initialize(template, path) ⇒ Path

:call-seq: Path.new(template, path_string) -> path

Create a new Path instance from a path template and a string of the path.



50
51
52
53
54
55
56
57
58
# File 'lib/octarine/path.rb', line 50

def initialize(template, path)
  @template = Octarine::PathTemplate.new(template)
  @params = @template.recognize(path)
  
  @params.each do |key, value|
    next unless Symbol === key
    self.class.class_eval {define_method(key) {@params[key]}}
  end
end

Instance Method Details

#+(part) ⇒ Object

:call-seq: path + string -> new_path

Returns a new path with string appended.

path = Path.new("/users/:id", "/users/1")
new_path = (path + "favourites/:favourite_id").merge(:favourite_id => 2)
new_path.to_s   #=> "/users/1/favourites/2"


97
98
99
# File 'lib/octarine/path.rb', line 97

def +(part)
  dup.tap {|cp| cp.template = @template + part}
end

#==(other) ⇒ Object

:call-seq: path == other -> bool

Returns true if other is equal to path, false otherwise.



114
115
116
# File 'lib/octarine/path.rb', line 114

def ==(other)
  self.class === other && to_s == other.to_s
end

#===(other) ⇒ Object

:call-seq: path === other -> bool

Returns true if other as a string is equal to path as a string, false otherwise.



123
124
125
# File 'lib/octarine/path.rb', line 123

def ===(other)
  to_s === other.to_s
end

#initialize_copy(source) ⇒ Object

:nodoc:



127
128
129
130
131
# File 'lib/octarine/path.rb', line 127

def initialize_copy(source) # :nodoc:
  super
  @template = @template.dup
  @params = @params.dup
end

#merge(other) ⇒ Object

:call-seq: path.merge(hash) -> new_path

Returns a new path with contents of hash merged in to the path parameters.

path = Path.new("/users/:id/favourites", "/users/1/favourites?limit=10")
new_path = path.merge(:id => 2, "offset" => 20)
new_path.to_s   #=> "users/2/favourites?limit=10&offset=20"


85
86
87
# File 'lib/octarine/path.rb', line 85

def merge(other)
  dup.tap {|cp| cp.params.merge!(other)}
end

#to_sObject Also known as: to_str

:call-seq: path.to_s -> string

Returns the path as a string.



105
106
107
# File 'lib/octarine/path.rb', line 105

def to_s
  @template.apply(@params)
end

#without(part) ⇒ Object

:call-seq: path.without(part) -> new_path

Return a new path without part.

path = Path.new("/users/:id", "/users/1")
path.without("users").to_s   #=> "/1"

path = Path.new("/users/:id", "/users/1")
path.without(":id").to_s     #=> "/users"


70
71
72
73
74
75
# File 'lib/octarine/path.rb', line 70

def without(part)
  dup.tap do |cp|
    cp.template = @template.without(part)
    cp.params.delete(part)
  end
end