Class: Rack::JSON::Resource

Inherits:
Object
  • Object
show all
Includes:
EndPoint
Defined in:
lib/rackjson/resource.rb

Constant Summary collapse

HTTP_METHODS =
[:get, :post, :put, :delete, :options]

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
# File 'lib/rackjson/resource.rb', line 6

def initialize(app, options)
  @app = app
  @collections = options[:collections]
  @db = options[:db]
  @methods = options[:only] || HTTP_METHODS - (options[:except] || [])
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rackjson/resource.rb', line 13

def call(env)
  request = Rack::JSON::Request.new(env)
  if bypass? request
    @app.call(env)
  elsif method_not_allowed? request
    render "", :status => 405
  else
    @collection = Rack::JSON::Collection.new(@db[request.collection])
    send(request.request_method.downcase, request)
  end
end