Module: Sinatra::RoleBuilder

Defined in:
lib/belphanior/servant/role_builder.rb

Defined Under Namespace

Classes: BadParameterException

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.empty_handlersObject



78
79
80
81
82
83
# File 'lib/belphanior/servant/role_builder.rb', line 78

def self.empty_handlers
  {
    "roles" => [
    ]
  }
end

.registered(app) ⇒ Object



85
86
87
88
89
90
91
# 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

Instance Method Details

#add_handler(command_name, argument_names, http_method, path, data, role_index = 0, &blk) ⇒ Object

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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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_handlersObject



139
140
141
142
# 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) ⇒ Object

Sets the implementation’s URL



94
95
96
# 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