Class: Roda::RodaPlugins::RestApi::Resource
- Inherits:
-
Object
- Object
- Roda::RodaPlugins::RestApi::Resource
- Defined in:
- lib/roda/plugins/rest_api.rb
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#singleton ⇒ Object
readonly
Returns the value of attribute singleton.
Instance Method Summary collapse
- #delete(&block) ⇒ Object
-
#initialize(request, options = {}) ⇒ Resource
constructor
A new instance of Resource.
- #list(&block) ⇒ Object
- #one(&block) ⇒ Object
- #perform(method, id = nil) ⇒ Object
- #routes(*routes) ⇒ Object
- #routes! ⇒ Object
- #save(&block) ⇒ Object
- #serialize(&block) ⇒ Object
Constructor Details
#initialize(request, options = {}) ⇒ Resource
Returns a new instance of Resource.
20 21 22 23 24 25 |
# File 'lib/roda/plugins/rest_api.rb', line 20 def initialize(request, ={}) @request = request @singleton = .delete(:singleton) || false @primary_key = .delete(:primary_key) || "id" @content_type = .delete(:content_type) || APPLICATION_JSON end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
18 19 20 |
# File 'lib/roda/plugins/rest_api.rb', line 18 def content_type @content_type end |
#singleton ⇒ Object (readonly)
Returns the value of attribute singleton.
18 19 20 |
# File 'lib/roda/plugins/rest_api.rb', line 18 def singleton @singleton end |
Instance Method Details
#delete(&block) ⇒ Object
42 43 44 45 |
# File 'lib/roda/plugins/rest_api.rb', line 42 def delete(&block) @delete = block if block @delete || ->(_){raise NotImplementedError, "delete"} end |
#list(&block) ⇒ Object
27 28 29 30 |
# File 'lib/roda/plugins/rest_api.rb', line 27 def list(&block) @list = block if block @list || ->(_){raise NotImplementedError, "list"} end |
#one(&block) ⇒ Object
32 33 34 35 |
# File 'lib/roda/plugins/rest_api.rb', line 32 def one(&block) @one = block if block @one || ->(_){raise NotImplementedError, "one"} end |
#perform(method, id = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/roda/plugins/rest_api.rb', line 64 def perform(method, id = nil) begin args = method === :save ? JSON.parse(@request.body) : @request.GET args.merge!(@primary_key => id) if id self.send(method).call(args) rescue StandardError => e @request.response.status = method === :save ? 422 : 404 end end |
#routes(*routes) ⇒ Object
52 53 54 |
# File 'lib/roda/plugins/rest_api.rb', line 52 def routes(*routes) @routes = routes end |
#routes! ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/roda/plugins/rest_api.rb', line 56 def routes! unless @routes @routes = SINGLETON_ROUTES.dup @routes << :index unless @singleton end @routes.each { |route| @request.send(route) } end |
#save(&block) ⇒ Object
37 38 39 40 |
# File 'lib/roda/plugins/rest_api.rb', line 37 def save(&block) @save = block if block @save || ->(_){raise NotImplementedError, "save"} end |
#serialize(&block) ⇒ Object
47 48 49 50 |
# File 'lib/roda/plugins/rest_api.rb', line 47 def serialize(&block) @serialize = block if block @serialize || ->(obj){obj.is_a?(String) ? obj : obj.send(:to_json)} end |