class KintoBox::KintoObject
Attributes
Public Class Methods
Assign or retrieve the child class
# File lib/kinto_box/kinto_object.rb, line 10 def child_class(value = nil) return @child_class if value.nil? @child_class = value end
# File lib/kinto_box/kinto_object.rb, line 18 def initialize(client: nil, id: nil, parent: nil, info: nil) @client = client || parent.client @parent = parent @id = id || (info ? info['data']['id'] : nil) @info = info self end
Get the name of this class suitable for use in url path
# File lib/kinto_box/kinto_object.rb, line 5 def path_name name.sub('KintoBox::Kinto', '').downcase + 's' end
Public Instance Methods
Add a permission to this object @param [String] Principal aka user or group name @param [String] Permission name i.e. read, write, etc @return [KintoObject] The current instance; for chaining
# File lib/kinto_box/kinto_object.rb, line 127 def add_permission(principal, permission) @info = client.patch(url_path, 'permissions' => { permission => [principal_name(principal)] }) self end
Return an object for this child @param [String] Child object ID @return [KintoObject] Child for ID
# File lib/kinto_box/kinto_object.rb, line 88 def child(child_id) child_class.new(id: child_id, parent: self) end
Get the path to this object in the Kinto API Use the name of url_path
and whatever the child class has set for @parent.url_path is to create a partial url path. @return [String] URL fragment
# File lib/kinto_box/kinto_object.rb, line 40 def child_path return unless child_class? "#{url_path}/#{child_class.path_name}".gsub(%r{/+}, '/') end
Count all children @param [String,Array] Filters @return [Integer] Count
# File lib/kinto_box/kinto_object.rb, line 119 def count_children(filters = nil) count_children_request(filters).execute['Total-Records'].to_i end
Get a kinto request object for making a count children request @return [KintoRequest] Object representing this request
# File lib/kinto_box/kinto_object.rb, line 193 def count_children_request(filters = nil) client.create_request('HEAD', url_w_qsp(filters)) end
Create a child object @param [String] Child object ID @return [KintoObject] New child
# File lib/kinto_box/kinto_object.rb, line 95 def create_child(data) resp = create_child_request(data).execute child_class.new(info: resp, parent: self) end
Get a kinto request object for making a create child request @return [KintoRequest] Object representing this request
# File lib/kinto_box/kinto_object.rb, line 175 def create_child_request(data) client.create_request('POST', url_w_qsp, 'data' => data) end
Delete this object @return [Hash] Response
# File lib/kinto_box/kinto_object.rb, line 69 def delete @info = delete_request.execute end
Get all children @param [String,Array] Filters @param [String] Sort by @return [Hash] All children
# File lib/kinto_box/kinto_object.rb, line 112 def delete_children(filters = nil) delete_children_request(filters).execute end
Get a kinto request object for making a delete children request @return [KintoRequest] Object representing this request
# File lib/kinto_box/kinto_object.rb, line 187 def delete_children_request(filters = nil) client.create_request('DELETE', url_w_qsp(filters)) end
Get a kinto request object for making a delete request @return [KintoRequest] Object representing this request
# File lib/kinto_box/kinto_object.rb, line 169 def delete_request client.create_request('DELETE', url_path) end
Check to see if this Kinto object exists @return [Boolean]
# File lib/kinto_box/kinto_object.rb, line 47 def exists? begin return false if info && info['data'] && info['data']['deleted'] == true rescue return false end true end
Get the data related to this object @return [Hash] Object data
# File lib/kinto_box/kinto_object.rb, line 58 def info @info ||= info_request.execute end
Get a kinto request object for making an info request @return [KintoRequest] Object representing this request
# File lib/kinto_box/kinto_object.rb, line 149 def info_request client.create_request('GET', url_path) end
Get all children @param [String,Array] Filters @param [String] Sort by @return [Hash] All children
# File lib/kinto_box/kinto_object.rb, line 104 def list_children(filters = nil, sort = nil) list_children_request(filters, sort).execute end
Get a kinto request object for making a list children request @return [KintoRequest] Object representing this request
# File lib/kinto_box/kinto_object.rb, line 181 def list_children_request(filters = nil, sort = nil) client.create_request('GET', url_w_qsp(filters, sort)) end
Get the permissions for the current object @return [Hash] Hash of permissions and assigned principals.
# File lib/kinto_box/kinto_object.rb, line 143 def permissions info['permissions'] end
Delete the cached info for this object
# File lib/kinto_box/kinto_object.rb, line 63 def reload @info = nil end
Replace all data in the object @return [Hash] Response
# File lib/kinto_box/kinto_object.rb, line 81 def replace(data) @info = replace_request(data).execute end
Replace all permissions for this object @param [String] Principal aka user or group name @param [String] Permission name i.e. read, write, etc @return [KintoObject] The current instance; for chaining
# File lib/kinto_box/kinto_object.rb, line 136 def replace_permission(principal, permission) @info = client.put(url_path, 'permissions' => { permission => [principal_name(principal)] }) self end
Get a kinto request object for making a delete request @param [Hash] Data @return [KintoRequest] Object representing this request
# File lib/kinto_box/kinto_object.rb, line 163 def replace_request(data) client.create_request('PUT', url_path, 'data' => data) end
Update this object @return [Hash] Response
# File lib/kinto_box/kinto_object.rb, line 75 def update(data) @info = update_request(data).execute end
Get a kinto request object for making an update request @param [Hash] Data @return [KintoRequest] Object representing this request
# File lib/kinto_box/kinto_object.rb, line 156 def update_request(data) client.create_request('PATCH', url_path, 'data' => data) end
Get the path to this object in the Kinto API. This method uses the name of this class and whatever @parent.url_path is to create a partial url path. @return [String] URL fragment
# File lib/kinto_box/kinto_object.rb, line 30 def url_path path = "/#{self.class.path_name}/#{id}" path = parent.url_path + path unless parent.nil? path.gsub(%r{/+}, '/') end
Private Instance Methods
Get the class for this object's child @return [KintoObject] Child class
# File lib/kinto_box/kinto_object.rb, line 201 def child_class self.class.child_class end
Does this class have a child class? @return [Boolean]
# File lib/kinto_box/kinto_object.rb, line 207 def child_class? !(child_class.nil? || child_class.is_a?(KintoObject)) end
Convert the principal name to something Kinto will like @return [String] Valid Kinto principal name
# File lib/kinto_box/kinto_object.rb, line 213 def principal_name(principal) case principal.downcase when 'authenticated' 'system.Authenticated' when 'anonymous' 'system.Everyone' when 'everyone' 'system.Everyone' else principal end end
Build the path including querystring
# File lib/kinto_box/kinto_object.rb, line 227 def url_w_qsp(filters = nil, sort = nil, add_child = true) url = child_path.nil? || !add_child ? url_path : child_path query_string = '' query_string = filters unless filters.nil? query_string = "#{query_string}&" unless filters.nil? || sort.nil? query_string = "#{query_string}_sort=#{sort}" unless sort.nil? query_string == '' ? url : "#{url}?#{query_string}" end