Class: HttpRouter::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/http_router/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, parts, extension) ⇒ Path

Returns a new instance of Path.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/http_router/path.rb', line 6

def initialize(path, parts, extension)
  @path, @parts, @extension = path, parts, extension
  if duplicate_variable_names = variable_names.dup.uniq!
    raise AmbiguousVariableException.new("You have duplicate variable name present: #{duplicate_variable_names.join(', ')}")
  end

  eval_path = path.gsub(/[:\*]([a-zA-Z0-9_]+)/) {"\#{args.shift || (options && options.delete(:#{$1})) || raise(MissingParameterException.new(\"missing parameter #{$1}\"))}" }
  instance_eval "
  def raw_url(args,options)
    \"#{eval_path}\"
  end
  "
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



4
5
6
# File 'lib/http_router/path.rb', line 4

def extension
  @extension
end

#partsObject (readonly)

Returns the value of attribute parts.



4
5
6
# File 'lib/http_router/path.rb', line 4

def parts
  @parts
end

#routeObject

Returns the value of attribute route.



5
6
7
# File 'lib/http_router/path.rb', line 5

def route
  @route
end

Instance Method Details

#===(other_path) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/http_router/path.rb', line 20

def ===(other_path)
  return false if @parts.size != other_path.parts.size
  @parts.each_with_index {|p,i| 
    return unless compare_parts(p, other_path.parts[i])
  }
  compare_parts(@extension, other_path.extension)
end

#compare_parts(p1, p2) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/http_router/path.rb', line 28

def compare_parts(p1, p2)
  case p1
  when Glob then p2.is_a?(Glob)
  when Variable then p2.is_a?(Variable)
  else
    p1 == p2
  end
end

#generate_querystring(uri, params) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/http_router/path.rb', line 45

def generate_querystring(uri, params)
  if params && !params.empty?
    uri_size = uri.size
    params.each do |k,v|
      case v
      when Array
        v.each { |v_part| uri << '&' << CGI.escape(k.to_s) << '%5B%5D=' << CGI.escape(v_part.to_s) }
      else
        uri << '&' << CGI.escape(k.to_s) << '=' << CGI.escape(v.to_s)
      end
    end
    uri[uri_size] = ??
  end
end

#matches_extension?(extension) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/http_router/path.rb', line 72

def matches_extension?(extension)
  @extension.nil? || @extension === (extension)
end

#url(args, options) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/http_router/path.rb', line 37

def url(args, options)
  path = raw_url(args, options)
  raise TooManyParametersException.new unless args.empty?
  Rack::Utils.uri_escape!(path)
  generate_querystring(path, options)
  path
end

#variable_namesObject



68
69
70
# File 'lib/http_router/path.rb', line 68

def variable_names
  @variable_names ||= variables.map{|v| v.name}
end

#variablesObject



60
61
62
63
64
65
66
# File 'lib/http_router/path.rb', line 60

def variables
  unless @variables
    @variables = @parts.select{|p| p.is_a?(Variable)}
    @variables << @extension if @extension.is_a?(Variable)
  end
  @variables
end