Class: KintoBox::KintoObject

Inherits:
Object
  • Object
show all
Defined in:
lib/kinto_box/kinto_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#clientObject (readonly)

Returns the value of attribute client.



16
17
18
# File 'lib/kinto_box/kinto_object.rb', line 16

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/kinto_box/kinto_object.rb', line 16

def id
  @id
end

#parentObject (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_nameObject

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

Parameters:

  • Principal (String)

    aka user or group name

  • Permission (String)

    name i.e. read, write, etc

Returns:



127
128
129
130
# 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

#child(child_id) ⇒ KintoObject

Return an object for this child

Parameters:

  • Child (String)

    object ID

Returns:



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_pathString

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.

Returns:

  • (String)

    URL fragment



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

Parameters:

  • Filters (String, Array)

Returns:

  • (Integer)

    Count



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

Returns:



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

Parameters:

  • Child (String)

    object ID

Returns:



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

Returns:



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

#deleteHash

Delete this object

Returns:

  • (Hash)

    Response



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

Parameters:

  • Filters (String, Array)
  • Sort (String)

    by

Returns:

  • (Hash)

    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

Returns:



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_requestKintoRequest

Get a kinto request object for making a delete request

Returns:



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

Returns:

  • (Boolean)


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

#infoHash

Get the data related to this object

Returns:

  • (Hash)

    Object data



58
59
60
# File 'lib/kinto_box/kinto_object.rb', line 58

def info
  @info ||= info_request.execute
end

#info_requestKintoRequest

Get a kinto request object for making an info request

Returns:



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

Parameters:

  • Filters (String, Array)
  • Sort (String)

    by

Returns:

  • (Hash)

    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

Returns:



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

#permissionsHash

Get the permissions for the current object

Returns:

  • (Hash)

    Hash of permissions and assigned principals.



143
144
145
# File 'lib/kinto_box/kinto_object.rb', line 143

def permissions
  info['permissions']
end

#reloadObject

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

Returns:

  • (Hash)

    Response



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

Parameters:

  • Principal (String)

    aka user or group name

  • Permission (String)

    name i.e. read, write, etc

Returns:



136
137
138
139
# 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

#replace_request(data) ⇒ KintoRequest

Get a kinto request object for making a delete request

Parameters:

  • Data (Hash)

Returns:



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

Returns:

  • (Hash)

    Response



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

Parameters:

  • Data (Hash)

Returns:



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_pathString

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.

Returns:

  • (String)

    URL fragment



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