Sha256: 05434271eac62f2c6228edbb9b35f0cc910129dfb53561df1f0ac0507ffd348a

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 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 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, what)
  raise InvalidRoute, "what must be a hash" unless what.is_a? Hash
  what[:url] = File.expand_path File.join(route_prefix, what[:url])
  Nephos::Router.check!(what)
  Nephos::Router.add_params!(what)
  Nephos::Router.add(what, verb)
end

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

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

# @param what [Hash] see {#add_route}
def put what
  add_route "PUT", 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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nephos-server-0.6.4 lib/nephos-server/router/helpers.rb
nephos-server-0.6.3 lib/nephos-server/router/helpers.rb
nephos-server-0.6.2 lib/nephos-server/router/helpers.rb
nephos-server-0.6.1 lib/nephos-server/router/helpers.rb
nephos-server-0.5.4 lib/nephos-server/router/helpers.rb
nephos-server-0.5.2 lib/nephos-server/router/helpers.rb