Module: Sinatra::API::Resources
- Defined in:
- lib/sinatra/api/resources.rb
Overview
API for defining parameters an endpoint requires or accepts, their types, and optional validators.
Instance Method Summary collapse
-
#api_locate_resource(r, container = nil) ⇒ Object
private
Attempt to locate a resource based on an ID supplied in a request parameter.
Instance Method Details
#api_locate_resource(r, container = nil) ⇒ Object (private)
Attempt to locate a resource based on an ID supplied in a request parameter.
If the param map contains a resource id (ie, :folder_id), we attempt to locate and expose it to the route.
A 404 is raised if:
- the scope is missing (@space for folder, @space or @folder for page)
- the resource couldn't be identified in its scope (@space or @folder)
If the resources were located, they're accessible using @folder or @page.
The route can be halted using the :requires => [] condition when it expects a resource.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sinatra/api/resources.rb', line 49 def api_locate_resource(r, container = nil) resource_id = params[r + '_id'].to_i rklass = r.camelize collection = case when container.nil?; eval "#{ResourcePrefix}#{rklass}" else; container.send("#{r.to_plural}") end puts "locating resource #{r} with id #{resource_id} from #{collection} [#{container}]" resource = collection.get(resource_id) if !resource m = "No such resource: #{rklass}##{resource_id}" if container m << " in #{container.class.name.to_s}##{container.id}" end halt 404, m end if respond_to?(:can?) unless can? :access, resource halt 403, "You do not have access to this #{rklass} resource." end end instance_variable_set('@'+r, resource) Sinatra::API.trigger :resource_located, resource, r resource end |