module Sinatra::RoleBuilder

Public Class Methods

empty_handlers() click to toggle source
# File lib/belphanior/servant/role_builder.rb, line 78
def self.empty_handlers
  {
    "roles" => [
    ]
  }
end
registered(app) click to toggle source
# File lib/belphanior/servant/role_builder.rb, line 85
def self.registered(app)
  app.set :implementation, Sinatra::RoleBuilder.empty_handlers
  app.get '/protocol' do
    BelphaniorServantHelper.text_out_as_json(JSON.dump(app.implementation))
  end

end

Public Instance Methods

add_handler(command_name, argument_names, http_method, path, data, role_index=0, &blk) click to toggle source

Adds a handler at the specified URL

params are

  • command_name: The identifier for the command this handler implements.

  • argument_names: The identifiers for the arguments. The command block will receive the arguments in the same order.

  • http_method: HTTP access method, one of “GET”, “POST”, etc.

  • path: The path for the HTTP request (including arguments specified in $(argument name) format).

  • data: If a POST method, the data that should be sent (including arguments specified in $(argument name) format).

  • role_index: Which role this handler should be mapped into. Assumes the role already has a description.

# File lib/belphanior/servant/role_builder.rb, line 107
def add_handler(command_name, argument_names, http_method, path, data, role_index=0, &blk)
  # validate name, args, and method
  if not RoleBuilderUtils::is_valid_identifier? command_name
    raise BadParameterException, (command_name + " is not a valid command name.")
  end
  argument_names.each { |i|
    if not RoleBuilderUtils::is_valid_identifier? i
      raise BadParameterException, (i + " is not a valid argument name.")
    end
  }
  if not ["GET", "POST", "PUT", "DELETE"].include? http_method
    raise BadParameterException, (http_method + " is not a valid HTTP method (is it capitalized?)")
  end

  new_handler = {
    "name" => RoleBuilderUtils::normalize_identifier(command_name),
    "method" => http_method,
    "path" => path,
    "data" => data
  }

  implementation["roles"][role_index]["handlers"] << new_handler

  # Add the method that will execute for this handler
  sinatra_path = RoleBuilderUtils::arguments_and_path_to_sinatra_path(argument_names, path)
  if http_method == "GET"
    get(sinatra_path, &blk)
  elsif http_method == "POST"
    post(sinatra_path, &blk)
  else
    raise BadParameterException, ("Unknown HTTP method '" + http_method + "'.")
  end
  def clear_handlers
    # Resets the handler list.
    set :implementation, Sinatra::RoleBuilder.empty_handlers
  end
end
clear_handlers() click to toggle source
# File lib/belphanior/servant/role_builder.rb, line 139
def clear_handlers
  # Resets the handler list.
  set :implementation, Sinatra::RoleBuilder.empty_handlers
end
set_role_url(url, role_index=0) click to toggle source

Sets the implementation’s URL

# File lib/belphanior/servant/role_builder.rb, line 94
def set_role_url(url, role_index=0)
    implementation["roles"][role_index]["role_url"]=url
end