Sha256: b10c6a11cb567894c7ff74e9322298c1874b23d0c16290f6d3f7dde15154cdd3

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

def route_prefix
  @route_prefix ||= []
  File.join(["/"] + @route_prefix)
end

# @param verb [String] has to be a valid http verb, so a string uppercase
# @param url [String] if an url is provided, then it will be put in the hash
#   to have the same behavior as if it was specified in the what hash
#   \\{url: URL}
# @param what [Hash] has to contain the following keys:
#   - :url
#   - :controller
#   - :method
#
# The method create a valid route, set in Nephos::Router::ROUTES
# it will call the method from the controller, based on the parameters
# if the client request match with the verb and the url provided.
#
# Checkout the documentation about the parameters and API for more informations
def add_route(verb, url=nil, what)
  raise InvalidRoute, "what must be a hash" unless what.is_a? Hash
  what[:url] ||= url
  Array(what[:url]).each do |url|
    route = what.dup
    route[:url] = url
    route[:url] = File.expand_path File.join(route_prefix, route[:url])
    Nephos::Router.check!(route)
    Nephos::Router.add_params!(route)
    Nephos::Router.add(route, verb)
  end
end

# @param url [String] see {#add_route}
# @param what [Hash] see {#add_route}
def get url=nil, what
  add_route "GET", url, what
end

# @param url [String] see {#add_route}
# @param what [Hash] see {#add_route}
def post url=nil, what
  add_route "POST", url, what
end

# @param url [String] see {#add_route}
# @param what [Hash] see {#add_route}
def put url=nil, what
  add_route "PUT", url, what
end

# @param name [String]
# @param block [Bloc]
#
# Create a resource named based on the parameter name
# Every call of {#add_route} {#get} {#post} {#put} in the bloc
# will have a modified url, working with the following schema:
#   "/name/" + url
def resource(name, &block)
  @route_prefix ||= []
  @route_prefix << name
  block.call
  @route_prefix.pop
end

# An alias is an url which have the same proprieties than the previous route
def alias_route
  raise "Not implemented yet"
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nephos-server-0.6.9 lib/nephos-server/router/helpers.rb
nephos-server-0.6.8 lib/nephos-server/router/helpers.rb
nephos-server-0.6.7 lib/nephos-server/router/helpers.rb
nephos-server-0.6.5 lib/nephos-server/router/helpers.rb