Module: Evvnt::ClassTemplateMethods
- Defined in:
- lib/evvnt/class_template_methods.rb
Overview
Internal: Template methods to provide default behaviour for API actions.
These are defined on Evvnt::Base subclasses where required to map the Evvnt API actions.
Constant Summary collapse
- PARAM_REGEX =
Regular expression for params in URL strings
%r{\:[^\/$]+}
Instance Method Summary collapse
-
#create(**params) ⇒ Object
Template method for creating a new record on the API.
-
#index(**params) ⇒ Object
Template method for fetching an index of record from the API.
-
#mine(**params) ⇒ Object
Template method for fetching mine records from the API.
-
#ours(record_id = nil, **params) ⇒ Object
Template method for fetching mine records from the API.
-
#show(record_id = nil, **params) ⇒ Object
Template method for creating a given record.
-
#update(record_id, **params) ⇒ Object
Template method for updating a given record.
Instance Method Details
#create(**params) ⇒ Object
Template method for creating a new record on the API.
params - A Hash of params to send to the API.
Returns Base subclass
18 19 20 21 |
# File 'lib/evvnt/class_template_methods.rb', line 18 def create(**params) path = nest_path_within_parent(plural_resource_path, params) api_request(:post, path, params: params) end |
#index(**params) ⇒ Object
Template method for fetching an index of record from the API.
params - A Hash of params to send to the API.
Returns Array
28 29 30 31 32 |
# File 'lib/evvnt/class_template_methods.rb', line 28 def index(**params) params.stringify_keys! path = nest_path_within_parent(plural_resource_path, params) api_request(:get, path, params: params) end |
#mine(**params) ⇒ Object
Template method for fetching mine records from the API.
params - A Hash of params to send to the API.
Returns Array
81 82 83 84 |
# File 'lib/evvnt/class_template_methods.rb', line 81 def mine(**params) path = File.join(plural_resource_path, "mine").to_s api_request(:get, path, params: params) end |
#ours(record_id = nil, **params) ⇒ Object
Template method for fetching mine records from the API.
record_id - An Integer or String representing the record ID on the API (optional). params - A Hash of params to send to the API.
Returns Array Returns Base
69 70 71 72 73 74 |
# File 'lib/evvnt/class_template_methods.rb', line 69 def ours(record_id = nil, **params) id_segment = record_id.to_s segments = [plural_resource_path, "ours", id_segment].select(&:present?) path = File.join(*segments).to_s api_request(:get, path, params: params) end |
#show(record_id = nil, **params) ⇒ Object
Template method for creating a given record
record_id - An Integer or String representing the record ID on the API. params - A Hash of params to send to the API.
Returns Base subclass
40 41 42 43 44 45 46 |
# File 'lib/evvnt/class_template_methods.rb', line 40 def show(record_id = nil, **params) if record_id.nil? && !singular_resource? raise ArgumentError, "record_id cannot be nil" end path = nest_path_within_parent(singular_path_for_record(record_id, params), params) api_request(:get, path, params: params) end |
#update(record_id, **params) ⇒ Object
Template method for updating a given record
record_id - An Integer or String representing the record ID on the API. params - A Hash of params to send to the API.
Returns Base subclass
54 55 56 57 58 59 60 |
# File 'lib/evvnt/class_template_methods.rb', line 54 def update(record_id, **params) if record_id.nil? && !singular_resource? raise ArgumentError, "record_id cannot be nil" end path = nest_path_within_parent(singular_path_for_record(record_id, params), params) api_request(:put, path, params: params) end |