Class: Rester::Service::Resource
- Inherits:
-
Object
- Object
- Rester::Service::Resource
- Defined in:
- lib/rester/service/resource.rb,
lib/rester/service/resource/params.rb
Defined Under Namespace
Classes: Params
Constant Summary collapse
- REQUEST_METHOD_TO_IDENTIFIED_METHOD =
{ 'GET' => :get, 'PUT' => :update, 'DELETE' => :delete }.freeze
- REQUEST_METHOD_TO_UNIDENTIFIED_METHOD =
{ 'GET' => :search, 'POST' => :create }.freeze
- RESOURCE_METHODS =
[:search, :create, :get, :update, :delete].freeze
Class Method Summary collapse
-
.id(name) ⇒ Object
Specify the name of your identifier (Default: ‘id’).
- .id_name ⇒ Object
- .id_param ⇒ Object
- .method_added(method_name) ⇒ Object
- .method_params ⇒ Object
-
.mount(klass) ⇒ Object
Mount another Service Resource.
- .mounts ⇒ Object
- .params(opts = {}, &block) ⇒ Object
Instance Method Summary collapse
- #error!(message = nil) ⇒ Object
-
#id_param ⇒ Object
Class Methods.
- #method_params_for(method_name) ⇒ Object
- #mounts ⇒ Object
-
#process(request_method, id_provided, params = {}) ⇒ Object
Given an HTTP request method, calls the appropriate calls the appropriate instance method.
Class Method Details
.id(name) ⇒ Object
Specify the name of your identifier (Default: ‘id’)
28 29 30 |
# File 'lib/rester/service/resource.rb', line 28 def id(name) @id_name = name.to_sym end |
.id_name ⇒ Object
49 50 51 |
# File 'lib/rester/service/resource.rb', line 49 def id_name @id_name ||= :id end |
.id_param ⇒ Object
53 54 55 |
# File 'lib/rester/service/resource.rb', line 53 def id_param "#{self.name.split('::').last.underscore}_#{id_name}" end |
.method_added(method_name) ⇒ Object
65 66 67 68 69 70 |
# 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).freeze end @_next_params = nil end |
.method_params ⇒ Object
61 62 63 |
# File 'lib/rester/service/resource.rb', line 61 def method_params @_method_params ||= {} end |
.mount(klass) ⇒ Object
Mount another Service Resource
34 35 36 37 38 |
# File 'lib/rester/service/resource.rb', line 34 def mount(klass) raise "Only other Service Resources can be mounted." unless klass < Resource start = self.name.split('::')[0..-2].join('::').length + 2 mounts[klass.name[start..-1].pluralize.underscore] = klass end |
.mounts ⇒ Object
57 58 59 |
# File 'lib/rester/service/resource.rb', line 57 def mounts (@__mounts ||= {}) end |
Instance Method Details
#error!(message = nil) ⇒ Object
99 100 101 |
# File 'lib/rester/service/resource.rb', line 99 def error!(=nil) Errors.throw_error!(Errors::RequestError, ) end |
#id_param ⇒ Object
Class Methods
73 74 75 |
# File 'lib/rester/service/resource.rb', line 73 def id_param self.class.id_param end |
#method_params_for(method_name) ⇒ Object
95 96 97 |
# File 'lib/rester/service/resource.rb', line 95 def method_params_for(method_name) self.class.method_params[method_name.to_sym] end |
#mounts ⇒ Object
91 92 93 |
# File 'lib/rester/service/resource.rb', line 91 def mounts self.class.mounts end |
#process(request_method, id_provided, params = {}) ⇒ Object
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.
84 85 86 87 88 89 |
# File 'lib/rester/service/resource.rb', line 84 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 |