Class: KintoBox::KintoObject
- Inherits:
-
Object
- Object
- KintoBox::KintoObject
- Defined in:
- lib/kinto_box/kinto_object.rb
Direct Known Subclasses
KintoBucket, KintoCollection, KintoGroup, KintoRecord, KintoServer
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
-
.child_class(value = nil) ⇒ Object
Assign or retrieve the child class.
-
.path_name ⇒ Object
Get the name of this class suitable for use in url path.
Instance Method Summary collapse
-
#add_permission(principal, permission) ⇒ KintoObject
Add a permission to this object.
-
#child(child_id) ⇒ KintoObject
Return an object for this child.
-
#child_path ⇒ String
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.
-
#count_children(filters = nil) ⇒ Integer
Count all children.
-
#count_children_request(filters = nil) ⇒ KintoRequest
Get a kinto request object for making a count children request.
-
#create_child(data) ⇒ KintoObject
Create a child object.
-
#create_child_request(data) ⇒ KintoRequest
Get a kinto request object for making a create child request.
-
#delete ⇒ Hash
Delete this object.
-
#delete_children(filters = nil) ⇒ Hash
Get all children.
-
#delete_children_request(filters = nil) ⇒ KintoRequest
Get a kinto request object for making a delete children request.
-
#delete_request ⇒ KintoRequest
Get a kinto request object for making a delete request.
-
#exists? ⇒ Boolean
Check to see if this Kinto object exists.
-
#info ⇒ Hash
Get the data related to this object.
-
#info_request ⇒ KintoRequest
Get a kinto request object for making an info request.
-
#initialize(client: nil, id: nil, parent: nil, info: nil) ⇒ KintoObject
constructor
A new instance of KintoObject.
-
#list_children(filters = nil, sort = nil) ⇒ Hash
Get all children.
-
#list_children_request(filters = nil, sort = nil) ⇒ KintoRequest
Get a kinto request object for making a list children request.
-
#permissions ⇒ Hash
Get the permissions for the current object.
-
#reload ⇒ Object
Delete the cached info for this object.
-
#replace(data) ⇒ Hash
Replace all data in the object.
-
#replace_permission(principal, permission) ⇒ KintoObject
Replace all permissions for this object.
-
#replace_request(data) ⇒ KintoRequest
Get a kinto request object for making a delete request.
-
#update(data) ⇒ Hash
Update this object.
-
#update_request(data) ⇒ KintoRequest
Get a kinto request object for making an update request.
-
#url_path ⇒ String
Get the path to this object in the Kinto API.
Constructor Details
#initialize(client: nil, id: nil, parent: nil, info: nil) ⇒ KintoObject
Returns a new instance of KintoObject.
18 19 20 21 22 23 24 |
# 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 |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
16 17 18 |
# File 'lib/kinto_box/kinto_object.rb', line 16 def client @client end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
16 17 18 |
# File 'lib/kinto_box/kinto_object.rb', line 16 def id @id end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
16 17 18 |
# File 'lib/kinto_box/kinto_object.rb', line 16 def parent @parent end |
Class Method Details
.child_class(value = nil) ⇒ Object
Assign or retrieve the child class
10 11 12 13 |
# File 'lib/kinto_box/kinto_object.rb', line 10 def child_class(value = nil) return @child_class if value.nil? @child_class = value end |
.path_name ⇒ Object
Get the name of this class suitable for use in url path
5 6 7 |
# File 'lib/kinto_box/kinto_object.rb', line 5 def path_name name.sub('KintoBox::Kinto', '').downcase + 's' end |
Instance Method Details
#add_permission(principal, permission) ⇒ KintoObject
Add a permission to this object
127 128 129 130 |
# File 'lib/kinto_box/kinto_object.rb', line 127 def (principal, ) @info = client.patch(url_path, 'permissions' => { => [principal_name(principal)] }) self end |
#child(child_id) ⇒ KintoObject
Return an object for this child
88 89 90 |
# File 'lib/kinto_box/kinto_object.rb', line 88 def child(child_id) child_class.new(id: child_id, parent: self) end |
#child_path ⇒ String
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.
40 41 42 43 |
# 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_children(filters = nil) ⇒ Integer
Count all children
119 120 121 |
# File 'lib/kinto_box/kinto_object.rb', line 119 def count_children(filters = nil) count_children_request(filters).execute['Total-Records'].to_i end |
#count_children_request(filters = nil) ⇒ KintoRequest
Get a kinto request object for making a count children request
193 194 195 |
# 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_child(data) ⇒ KintoObject
Create a child object
95 96 97 98 |
# 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 |
#create_child_request(data) ⇒ KintoRequest
Get a kinto request object for making a create child request
175 176 177 |
# 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 ⇒ Hash
Delete this object
69 70 71 |
# File 'lib/kinto_box/kinto_object.rb', line 69 def delete @info = delete_request.execute end |
#delete_children(filters = nil) ⇒ Hash
Get all children
112 113 114 |
# File 'lib/kinto_box/kinto_object.rb', line 112 def delete_children(filters = nil) delete_children_request(filters).execute end |
#delete_children_request(filters = nil) ⇒ KintoRequest
Get a kinto request object for making a delete children request
187 188 189 |
# File 'lib/kinto_box/kinto_object.rb', line 187 def delete_children_request(filters = nil) client.create_request('DELETE', url_w_qsp(filters)) end |
#delete_request ⇒ KintoRequest
Get a kinto request object for making a delete request
169 170 171 |
# File 'lib/kinto_box/kinto_object.rb', line 169 def delete_request client.create_request('DELETE', url_path) end |
#exists? ⇒ Boolean
Check to see if this Kinto object exists
47 48 49 50 51 52 53 54 |
# 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 |
#info ⇒ Hash
Get the data related to this object
58 59 60 |
# File 'lib/kinto_box/kinto_object.rb', line 58 def info @info ||= info_request.execute end |
#info_request ⇒ KintoRequest
Get a kinto request object for making an info request
149 150 151 |
# File 'lib/kinto_box/kinto_object.rb', line 149 def info_request client.create_request('GET', url_path) end |
#list_children(filters = nil, sort = nil) ⇒ Hash
Get all children
104 105 106 |
# File 'lib/kinto_box/kinto_object.rb', line 104 def list_children(filters = nil, sort = nil) list_children_request(filters, sort).execute end |
#list_children_request(filters = nil, sort = nil) ⇒ KintoRequest
Get a kinto request object for making a list children request
181 182 183 |
# 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 |
#permissions ⇒ Hash
Get the permissions for the current object
143 144 145 |
# File 'lib/kinto_box/kinto_object.rb', line 143 def info['permissions'] end |
#reload ⇒ Object
Delete the cached info for this object
63 64 65 |
# File 'lib/kinto_box/kinto_object.rb', line 63 def reload @info = nil end |
#replace(data) ⇒ Hash
Replace all data in the object
81 82 83 |
# File 'lib/kinto_box/kinto_object.rb', line 81 def replace(data) @info = replace_request(data).execute end |
#replace_permission(principal, permission) ⇒ KintoObject
Replace all permissions for this object
136 137 138 139 |
# File 'lib/kinto_box/kinto_object.rb', line 136 def (principal, ) @info = client.put(url_path, 'permissions' => { => [principal_name(principal)] }) self end |
#replace_request(data) ⇒ KintoRequest
Get a kinto request object for making a delete request
163 164 165 |
# File 'lib/kinto_box/kinto_object.rb', line 163 def replace_request(data) client.create_request('PUT', url_path, 'data' => data) end |
#update(data) ⇒ Hash
Update this object
75 76 77 |
# File 'lib/kinto_box/kinto_object.rb', line 75 def update(data) @info = update_request(data).execute end |
#update_request(data) ⇒ KintoRequest
Get a kinto request object for making an update request
156 157 158 |
# File 'lib/kinto_box/kinto_object.rb', line 156 def update_request(data) client.create_request('PATCH', url_path, 'data' => data) end |
#url_path ⇒ String
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.
30 31 32 33 34 |
# 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 |