Class: HttpShim

Inherits:
Goliath::API
  • Object
show all
Defined in:
app/http_shim.rb

Instance Method Summary collapse

Instance Method Details

#response(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/http_shim.rb', line 17

def response(env)
  path_params = env[:vayacondios_path]
  klass = ('vayacondios/' + path_params[:type] + '_handler').camelize.constantize

  Log.info("received request #{env['REQUEST_METHOD']} #{env['REQUEST_URI']}")
  Log.info("params: #{env['params']}")

  begin
    case env[:vayacondios_method]
      
    when :show
      record = klass.find(mongo, path_params)
      [200, {}, MultiJson.dump(record.body)]

    when :update
      record = klass.new(mongo).update(env['params'], path_params)
      [200, {}, nil]
      
    when :patch
      record = klass.new(mongo).patch(env['params'], path_params)
      [200, {}, nil]

    when :delete
      record = klass.find(mongo, path_params).destroy(env['params'])
      [200, {}, MultiJson.dump(record.body)]
    end
  rescue Vayacondios::Error::NotFound => ex
    return [404, {}, MultiJson.dump({ error: "Not Found" })]
  rescue Vayacondios::Error::BadRequest => ex
    return [400, {}, MultiJson.dump({ error: "Bad Request" })]
  rescue StandardError => ex
    puts ex
    ex.backtrace.each{ |l| puts l }
  end
end