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

- (Path) initialize(route, path, param_names = [])

A new instance of Path

Raises:

  • (AmbiguousVariableException)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/http_router/path.rb', line 6

def initialize(route, path, param_names = [])
  @route, @path, @param_names, @dynamic = route, path, param_names, !param_names.empty?
  duplicate_param_names = param_names.dup.uniq!
  raise AmbiguousVariableException, "You have duplicate variable name present: #{duplicate_param_names.join(', ')}" if duplicate_param_names
  if path.respond_to?(:split)
    regex_parts = path.split(/([:\*][a-zA-Z0-9_]+)/)
    @path_validation_regex, code = '', ''
    regex_parts.each_with_index{ |part, index|
      new_part = case part[0]
      when ?:, ?*
        if index != 0 && regex_parts[index - 1][-1] == ?\\
          @path_validation_regex << Regexp.quote(part)
          code << part
        else
          @path_validation_regex << (route.matches_with[part[1, part.size].to_sym] || '.*?').to_s
          code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || raise(MissingParameterException, \"missing parameter :#{part[1, part.size]}\")}"
        end
      else
        @path_validation_regex << Regexp.quote(part)
        code << part
      end
      new_part
    }
    @path_validation_regex = Regexp.new("^#{@path_validation_regex}$")
    instance_eval <<-EOT, __FILE__, __LINE__ + 1
    def raw_url(args,options)
      \"#{code}\"
    end
    EOT
  end
end

Instance Attribute Details

- (Object) dynamic (readonly) Also known as: dynamic?

Returns the value of attribute dynamic



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

def dynamic
  @dynamic
end

- (Object) param_names (readonly)

Returns the value of attribute param_names



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

def param_names
  @param_names
end

- (Object) route (readonly)

Returns the value of attribute route



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

def route
  @route
end

Instance Method Details

- (Object) hashify_params(params)



38
39
40
# File 'lib/http_router/path.rb', line 38

def hashify_params(params)
  @dynamic && params ? Hash[param_names.zip(params)] : {}
end

- (Object) original_path



49
50
51
# File 'lib/http_router/path.rb', line 49

def original_path
  @path
end

- (Object) url(args, options)



42
43
44
45
46
47
# File 'lib/http_router/path.rb', line 42

def url(args, options)
  path = raw_url(args, options)
  raise InvalidRouteException if path !~ @path_validation_regex
  raise TooManyParametersException unless args.empty?
  [URI.escape(path), options]
end