Class: Rod::Rest::API
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Rod::Rest::API
- Extended by:
- Naming
- Defined in:
- lib/rod/rest/api.rb
Class Method Summary collapse
-
.build_api_for(resource, options = {}) ⇒ Object
Build API for a given
resource
. -
.build_metadata_api(metadata, options = {}) ⇒ Object
Build metadata API for the given
metadata
. -
.start_with_database(database, options = {}, web_options = {}) ⇒ Object
Start the API for the
database
.
Methods included from Naming
plural_resource_name, singular_resource_name
Class Method Details
.build_api_for(resource, options = {}) ⇒ Object
Build API for a given resource
. Options:
-
:resource_name
- the name of the resource (resource.name by default) -
:serializer
- the serializer used to serialize the ROD objects (instance of JsonSerializer by default)
15 16 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rod/rest/api.rb', line 15 def build_api_for(resource,={}) serializer = [:serializer] || JsonSerializer.new resource_name = [:resource_name] || plural_resource_name(resource) get "/#{resource_name}" do if params.empty? serializer.serialize({count: resource.count}) elsif params.size == 1 name, value = params.first if resource.respond_to?("find_all_by_#{name}") serializer.serialize(resource.send("find_all_by_#{name}",value)) else status 404 serializer.serialize(nil) end else status 404 serializer.serialize(nil) end end get "/#{resource_name}/:id" do object = resource.find_by_rod_id(params[:id].to_i) if object serializer.serialize(object) else status 404 serializer.serialize(nil) end end resource.plural_associations.each do |property| get "/#{resource_name}/:id/#{property.name}" do object = resource.find_by_rod_id(params[:id].to_i) if object serializer.serialize({count: object.send("#{property.name}_count") }) else status 404 serializer.serialize(nil) end end get "/#{resource_name}/:id/#{property.name}/:index" do object = resource.find_by_rod_id(params[:id].to_i) if object = object.send(property.name)[params[:index].to_i] if serializer.serialize() else status 404 serializer.serialize(nil) end else status 404 serializer.serialize(nil) end end end end |
.build_metadata_api(metadata, options = {}) ⇒ Object
Build metadata API for the given metadata
. Options:
-
:serializer
- the serializer used to serialize the ROD objects
77 78 79 80 81 82 |
# File 'lib/rod/rest/api.rb', line 77 def (,={}) serializer = [:serializer] || JSON get "/metadata" do serializer.dump() end end |
.start_with_database(database, options = {}, web_options = {}) ⇒ Object
Start the API for the database
. Options:
-
resource_serializer
- serializer used for resources -
metadata_serializer
- serializer used for metadata
web_options
are passed to Sinatra run! method.
89 90 91 92 93 94 95 96 |
# File 'lib/rod/rest/api.rb', line 89 def start_with_database(database,={},={}) (database.,serializer: [:metadata_serializer]) database.send(:classes).each do |resource| next if database.special_class?(resource) build_api_for(resource,serializer: [:resource_serializer]) end run!() end |