Class: Brief::Server::Handlers::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/server/handlers/action.rb

Class Method Summary collapse

Class Method Details

.handle(path_args, briefcase, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/brief/server/handlers/action.rb', line 3

def self.handle(path_args, briefcase, options={})
  parts = path_args.split("/")
  action = parts.shift
  path = parts.join("/")

  document = briefcase.document_at(path)

  headers = {
    "Content-Type" => "application/json"
  }

  if !document
    return [404,headers,{error:"Could not find a document at this path"}]
  end

  model = document.to_model

  if !model.class.defined_actions.include?(action.to_sym)
    [400, headers, {error:"Invalid action: #{ action }"}]
  else
    model.send(action)
    [200, headers, model.as_json]
  end
end