Class: Airbed::Resources::Resty

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

Overview

Acts as a dispatcher to map between HTTP verbs and actions

Instance Method Summary collapse

Instance Method Details

#delete(id) ⇒ Object

Map from DELETE requests to restful actions.

delete + id loads instance_context and calls destroy



92
93
94
# File 'lib/airbed.rb', line 92

def delete(id)
  destroy(@model || instance_context(id))
end

#get(id = nil, action = nil) ⇒ Object

Maps from GET requests to restful actions

  • If id and action are provided, call the method action (if implemented).

  • If only id is provided, load the show_context and call show.

  • Otherwise, load the new context and call list



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/airbed.rb', line 38

def get(id=nil,action=nil)

  # we have id and action ... call arbitrary action
  if !id.blank? and !action.blank?
    model = instance_context(id)
    
    # special case
    action = (action == 'new' ? 'new_action' : action)
    send(action,model)

  elsif !id.blank?
    model = show_context(id)
    show(model)

  else
    list = list_context
    list(list)
  end
end

#post(id = nil) ⇒ Object

Maps from POST requests to restful actions. Also supports “exotic” methods PUT and DESTROY (via the _verb parameter).

  1. POST with an id is undefined

  2. POST with no id loads new_context and calls create.

  3. _verb == ‘put’ + id delegates to put the handler

  4. _verb == ‘destroy’ + id deletgates to the destroy handler



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/airbed.rb', line 65

def post(id=nil)
  unless id.blank?
    @model = instance_context(id)
    
    case input['_verb']
    when 'put'
      put(id)
    when 'delete'
      delete(id)
    else
      raise "eh? unknown verb #{input['_verb']}"
    end
  else
    create(new_context)
  end
end

#put(id) ⇒ Object

Map from PUT requests to restful actions.

put + id loads instance_context and calls update



85
86
87
# File 'lib/airbed.rb', line 85

def put(id)
  update(@model || instance_context(id))
end