class KintoBox::KintoObject

Attributes

client[R]
id[R]
parent[R]

Public Class Methods

child_class(value = nil) click to toggle source

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
new(client: nil, id: nil, parent: nil, info: nil) click to toggle source
# 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
path_name() click to toggle source

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_permission(principal, permission) click to toggle source

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
child(child_id) click to toggle source

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
child_path() click to toggle source

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_children(filters = nil) click to toggle source

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
count_children_request(filters = nil) click to toggle source

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_child(data) click to toggle source

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
create_child_request(data) click to toggle source

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() click to toggle source

Delete this object @return [Hash] Response

# File lib/kinto_box/kinto_object.rb, line 69
def delete
  @info = delete_request.execute
end
delete_children(filters = nil) click to toggle source

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
delete_children_request(filters = nil) click to toggle source

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
delete_request() click to toggle source

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
exists?() click to toggle source

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
info() click to toggle source

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
info_request() click to toggle source

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
list_children(filters = nil, sort = nil) click to toggle source

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
list_children_request(filters = nil, sort = nil) click to toggle source

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
permissions() click to toggle source

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
reload() click to toggle source

Delete the cached info for this object

# File lib/kinto_box/kinto_object.rb, line 63
def reload
  @info = nil
end
replace(data) click to toggle source

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_permission(principal, permission) click to toggle source

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
replace_request(data) click to toggle source

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(data) click to toggle source

Update this object @return [Hash] Response

# File lib/kinto_box/kinto_object.rb, line 75
def update(data)
  @info = update_request(data).execute
end
update_request(data) click to toggle source

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
url_path() click to toggle source

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

child_class() click to toggle source

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
child_class?() click to toggle source

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
principal_name(principal) click to toggle source

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
url_w_qsp(filters = nil, sort = nil, add_child = true) click to toggle source

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