Class: ActionFramework::ModelHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/actionframework/modelhelper.rb

Class Method Summary collapse

Class Method Details

.error_403(res) ⇒ Object



36
37
38
39
# File 'lib/actionframework/modelhelper.rb', line 36

def self.error_403 res
  res.status = 404 
  res.write({:error => "403 Forbidden"}.to_json)
end

.get(model, res) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/actionframework/modelhelper.rb', line 13

def self.get model,res
  if(model.fetch?)
    response = model.all.to_json
    res.body = [response]
  else
    error_403 res
  end
  puts res.inspect
  res.finish
end

.post(model, req, res) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/actionframework/modelhelper.rb', line 3

def self.post model,req,res
  if(model.create?)
    response = model.create(JSON.parse(req.body.read)).to_json
    res.body =  [response]
  else
    error_403 res
  end
  res.finish
end

.update(model, res) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/actionframework/modelhelper.rb', line 24

def self.update model,res
  if(model.update?)
    doc = JSON.parse(req.body.string)
    modelfind = model.where(doc[:where])
    response = modelfind.update_attributes(doc[:attributes]).to_json            
    res.body = [response]
  else
    error_403 res
  end
  res.finish
end