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 |
# 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) define_index(resource,resource_name,serializer) define_show(resource,resource_name,serializer) resource.plural_associations.each do |property| define_association_index(resource,resource_name,property,serializer) define_association_show(resource,resource_name,property,serializer) 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
31 32 33 34 35 36 |
# File 'lib/rod/rest/api.rb', line 31 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.
43 44 45 46 47 48 49 50 |
# File 'lib/rod/rest/api.rb', line 43 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 |