Module: DbAgile::Restful::Middleware::Post

Included in:
OneDatabase
Defined in:
lib/dbagile/restful/middleware/post.rb

Instance Method Summary collapse

Instance Method Details

#post(env) ⇒ Object

Implements POST access of the restful interface



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dbagile/restful/middleware/post.rb', line 7

def post(env)
  request = Rack::Request.new(env)
  decode(env) do |connection, table, format|
    format = :json if format.nil?
    
    # Retrieve heading and keys
    heading = connection.heading(table)
    keys = connection.keys(table)
    
    # Tuple to insert/update
    tuple = params_to_tuple(request.POST, heading)
    inserted = connection.transaction do |t|
      if tuple_has_key?(tuple, keys)
        key_projected = tuple_key(tuple, keys)
        if connection.exists?(table, key_projected)
          t.update(table, tuple, key_projected)
        else
          t.insert(table, tuple)
        end
      else
        t.insert(table, tuple)
      end
    end
    
    [format, to_xxx_enumerable(format, [ inserted ], tuple.keys)]
  end
end