class Rester::Service::Resource
Constants
- REQUEST_METHOD_TO_IDENTIFIED_METHOD
- REQUEST_METHOD_TO_UNIDENTIFIED_METHOD
- RESOURCE_METHODS
Public Class Methods
id(name)
click to toggle source
Specify the name of your identifier (Default: 'id')
# File lib/rester/service/resource.rb, line 28 def id(name) @id_name = name.to_sym end
id_name()
click to toggle source
# File lib/rester/service/resource.rb, line 49 def id_name @id_name ||= :id end
id_param()
click to toggle source
# File lib/rester/service/resource.rb, line 53 def id_param "#{self.name.split('::').last.underscore}_#{id_name}" end
method_added(method_name)
click to toggle source
# File lib/rester/service/resource.rb, line 65 def method_added(method_name) if RESOURCE_METHODS.include?(method_name.to_sym) method_params[method_name.to_sym] = (@_next_params || Params.new(strict: false)).freeze end @_next_params = nil end
method_params()
click to toggle source
# File lib/rester/service/resource.rb, line 61 def method_params @_method_params ||= {} end
mount(klass)
click to toggle source
mounts()
click to toggle source
# File lib/rester/service/resource.rb, line 57 def mounts (@__mounts ||= {}) end
params(opts={}, &block)
click to toggle source
# File lib/rester/service/resource.rb, line 40 def params(opts={}, &block) @_next_params = Params.new(opts, &block) end
Public Instance Methods
error!(error, message = nil)
click to toggle source
# File lib/rester/service/resource.rb, line 100 def error!(error, message = nil) Errors.throw_error!(Errors::RequestError.new(error, message)) end
id_param()
click to toggle source
# File lib/rester/service/resource.rb, line 74 def id_param self.class.id_param end
method_params_for(method_name)
click to toggle source
# File lib/rester/service/resource.rb, line 96 def method_params_for(method_name) self.class.method_params[method_name.to_sym] end
mounts()
click to toggle source
# File lib/rester/service/resource.rb, line 92 def mounts self.class.mounts end
process(request_method, id_provided, params = {})
click to toggle source
Given an HTTP request method, calls the appropriate calls the appropriate instance method. `id_provided` specifies whether on not the ID for the object is included in the params hash. This will be used when determining which instance method to call. For example, if the request method is GET: the ID being specified will call the `get` method and if it's not specified then it will call the `search` method.
# File lib/rester/service/resource.rb, line 85 def process(request_method, id_provided, params = {}) meth = (id_provided ? REQUEST_METHOD_TO_IDENTIFIED_METHOD : REQUEST_METHOD_TO_UNIDENTIFIED_METHOD)[request_method] _process(meth, params).to_h end
Private Instance Methods
_process(meth, params)
click to toggle source
Calls the specified method, passing the params if the method accepts an argument. Allows for the arity of the method to be 0, 1 or -1.
# File lib/rester/service/resource.rb, line 109 def _process(meth, params) if meth && respond_to?(meth) params = method_params_for(meth).validate(params) meth = method(meth) case meth.arity.abs when 1 meth.call(params) when 0 meth.call else fail MethodDefinitionError, "#{meth} must take 0 or 1 argument" end else fail Errors::NotFoundError, meth end end