Module: HttpRouter::RouteHelper

Defined in:
lib/http_router/route_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_default_values(hash) ⇒ Object



26
27
28
29
# File 'lib/http_router/route_helper.rb', line 26

def add_default_values(hash)
  @default_values ||= {}
  @default_values.merge!(hash)
end

#add_match_with(matchers) ⇒ Object



31
32
33
34
# File 'lib/http_router/route_helper.rb', line 31

def add_match_with(matchers)
  @match_with ||= {}
  @match_with.merge!(matchers)
end

#add_other_host(hosts) ⇒ Object



36
37
38
# File 'lib/http_router/route_helper.rb', line 36

def add_other_host(hosts)
  (@other_hosts ||= []).concat(hosts)
end

#add_path(path) ⇒ Object



40
41
42
# File 'lib/http_router/route_helper.rb', line 40

def add_path(path)
  (@paths ||= []) << path
end

#add_request_method(methods) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/http_router/route_helper.rb', line 44

def add_request_method(methods)
  @request_methods ||= Set.new
  methods = [methods] unless methods.is_a?(Array)
  methods.each do |method|
    method = method.to_s.upcase
    unless Route::VALID_HTTP_VERBS.include?(method)
      raise ArgumentError, "Unsupported HTTP request method: #{method}"
    end
    @request_methods << method
  end
end

#getObject



90
91
92
93
94
# File 'lib/http_router/route_helper.rb', line 90

def get
  add_request_method("GET")
  add_request_method("HEAD")
  self
end

#name(name = nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/http_router/route_helper.rb', line 17

def name(name = nil)
  if name
    self.name = name
    self
  else
    @name
  end
end

#pathObject



3
4
5
# File 'lib/http_router/route_helper.rb', line 3

def path
  @route.path_for_generation
end

#path=(path) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/http_router/route_helper.rb', line 7

def path=(path)
  @original_path = path
  if path.respond_to?(:[]) and path[/[^\\]\*$/]
    @match_partially = true
    @path_for_generation = path[0..path.size - 2]
  else
    @path_for_generation = path
  end
end

#process_opts(opts) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/http_router/route_helper.rb', line 56

def process_opts(opts)
  if opts[:conditions]
    opts.merge!(opts[:conditions])
    opts.delete(:conditions)
  end
  opts.each do |k, v|
    if respond_to?(:"#{k}=")
      send(:"#{k}=", v)
    elsif respond_to?(:"add_#{k}")
      send(:"add_#{k}", v)
    else
      add_match_with(k => v)
    end
  end
end

#redirect(path, status = 302) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
101
102
103
104
# File 'lib/http_router/route_helper.rb', line 96

def redirect(path, status = 302)
  raise ArgumentError, "Status has to be an integer between 300 and 399" unless (300..399).include?(status)
  to { |env|
    params = env['router.params']
    response = ::Rack::Response.new
    response.redirect(eval(%|"#{path}"|), status)
    response.finish
  }
end

#static(root) ⇒ Object

Sets the destination of this route to serve static files from either a directory or a single file.



107
108
109
110
111
112
113
114
115
# File 'lib/http_router/route_helper.rb', line 107

def static(root)
  @match_partially = true if File.directory?(root)
  to File.directory?(root) ?
    ::Rack::File.new(root) :
    proc {|env| 
      env['PATH_INFO'] = File.basename(root)
      ::Rack::File.new(File.dirname(root)).call(env)
    }
end

#to(dest = nil, &dest_block) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/http_router/route_helper.rb', line 72

def to(dest = nil, &dest_block)
  @dest = dest || dest_block || raise("you didn't specify a destination")
  if @dest.respond_to?(:url_mount=)
    urlmount = UrlMount.new(@path_for_generation, @default_values || {}) # TODO url mount should accept nil here.
    urlmount.url_mount = @router.url_mount if @router.url_mount
    @dest.url_mount = urlmount
  end
  self
end