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 22 23 24 25 |
# File 'lib/evvnt/class_template_methods.rb', line 18 def create(**params) path = if params_include_parent_resource_id?(params) nest_path_within_parent(plural_resource_path, params) else plural_resource_path end 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/evvnt/class_template_methods.rb', line 32 def index(**params) params.stringify_keys! if plural_resource_path.respond_to?(:call) path = plural_resource_path.call path.match(PARAM_REGEX) do |segment| value = params.delete(segment.to_s[1..-1]) path = path.gsub!(/#{segment}/, value.to_s) end else path = plural_resource_path end if params_include_parent_resource_id?(params) path = nest_path_within_parent(path, params) end 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
96 97 98 99 |
# File 'lib/evvnt/class_template_methods.rb', line 96 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
84 85 86 87 88 89 |
# File 'lib/evvnt/class_template_methods.rb', line 84 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
55 56 57 58 59 60 61 |
# File 'lib/evvnt/class_template_methods.rb', line 55 def show(record_id = nil, **params) if record_id.nil? && !singular_resource? raise ArgumentError, "record_id cannot be nil" end path = singular_path_for_record(record_id, 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
69 70 71 72 73 74 75 |
# File 'lib/evvnt/class_template_methods.rb', line 69 def update(record_id, **params) if record_id.nil? && !singular_resource? raise ArgumentError, "record_id cannot be nil" end path = singular_path_for_record(record_id, params) api_request(:put, path, params: params) end |