Class: HttpShim

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

Instance Method Summary collapse

Instance Method Details

#documentHash, ...

The document part of the request, e.g. - params that came directly from its body.

Something somewhere in Rack is unhappy when receiving non-Hash-like records via a JSON-formatted request body. So that Vayacondios::Rack::Params takes a non-Hash-like request body and turns it into a Hash with a single key: _document.

This hack does not affect the client-side: clients can still send non-Hash-like JSON documents and they will be interpreted as intended.

Returns:

  • (Hash, Array, String, Fixnum, nil)

    any native JSON datatype



32
33
34
# File 'app/http_shim.rb', line 32

def document
  params['_document'] || params
end

#response(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/http_shim.rb', line 36

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: #{document}")

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

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

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