class Syncano::Resources::DataObject

Data object resource - corresponds to Syncano data resource

Public Class Methods

batch_copy(batch_client, client, scope_parameters = {}, data_ids = []) click to toggle source

Batch version of “move” method @param [Jimson::BatchClient] batch_client @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Array] data_ids @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 109
def self.batch_copy(batch_client, client, scope_parameters = {}, data_ids = [])
  perform_copy(client, batch_client, scope_parameters, data_ids)
end
batch_move(batch_client, client, scope_parameters = {}, data_ids = [], conditions = {}, new_folder = nil, new_state = nil) click to toggle source

Batch version of “move” method @param [Jimson::BatchClient] batch_client @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Array] data_ids @param [Hash] conditions @param [String] new_folder @param [String] new_state @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 71
def self.batch_move(batch_client, client, scope_parameters = {}, data_ids = [], conditions = {}, new_folder = nil, new_state = nil)
  perform_move(client, batch_client, scope_parameters, data_ids, conditions, new_folder, new_state)
end
copy(client, scope_parameters = {}, data_ids = []) click to toggle source

Wrapper for api “copy” method @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Array] data_ids @return [Array] collection of Syncano::Resource::DataObject objects

# File lib/syncano/resources/data_object.rb, line 98
def self.copy(client, scope_parameters = {}, data_ids = [])
  response = perform_copy(client, nil, scope_parameters, data_ids)
  response.data.collect { |attributes| self.new(client, attributes.merge(scope_parameters)) }
end
count(client, scope_parameters = {}, conditions = {}) click to toggle source

Wrapper for api “count” method @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Hash] conditions @return [Integer]

# File lib/syncano/resources/data_object.rb, line 44
def self.count(client, scope_parameters = {}, conditions = {})
  response = perform_count(client, scope_parameters, conditions)
  response.data
end
find_by_key(client, key, scope_parameters = {}, conditions = {}) click to toggle source

Wrapper for api “get_one” method with data_key as a key @param [Syncano::Clients::Base] client @param [String] key @param [Hash] scope_parameters @param [Hash] conditions @return [Syncano::Resources::DataObject]

# File lib/syncano/resources/data_object.rb, line 35
def self.find_by_key(client, key, scope_parameters = {}, conditions = {})
  perform_find(client, :key, key, scope_parameters, conditions)
end
move(client, scope_parameters = {}, data_ids = [], conditions = {}, new_folder = nil, new_state = nil) click to toggle source

Wrapper for api “move” method @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Array] data_ids @param [Hash] conditions @param [String] new_folder @param [String] new_state @return [Array] collection of Syncano::Resource::DataObject objects

# File lib/syncano/resources/data_object.rb, line 57
def self.move(client, scope_parameters = {}, data_ids = [], conditions = {}, new_folder = nil, new_state = nil)
  response = perform_move(client, nil, scope_parameters, data_ids, conditions, new_folder, new_state)
  all(client, scope_parameters, data_ids: data_ids)
end
new(client, attributes = {}) click to toggle source

Overwritten constructor with recursive initializing associated children objects @param [Syncano::Clients::Base] client @param [Hash] attributes

Calls superclass method Syncano::Resources::Base::new
# File lib/syncano/resources/data_object.rb, line 8
def initialize(client, attributes = {})
  super(client, attributes)
  if self.attributes[:children].present?
    self.attributes[:children] = self.attributes[:children].collect do |child|
      if child.is_a?(Hash)
        self.class.new(client, child)
      else
        child
      end
    end
  end

  if self.attributes[:user].is_a?(Hash)
    self.attributes[:user] = ::Syncano::Resources::User.new(client, self.attributes[:user])
  end

  if self.attributes[:additional].is_a?(Hash)
    self.attributes.merge!(self.attributes.delete(:additional))
  end
end

Private Class Methods

attributes_to_sync(attributes) click to toggle source

Prepares hash with attributes used in synchronization with Syncano @return [Hash]

# File lib/syncano/resources/data_object.rb, line 201
def self.attributes_to_sync(attributes)
  attributes = attributes.dup

  if attributes.keys.map(&:to_sym).include?(:image)
    if attributes[:image].blank?
      attributes[:image] = ''
    elsif attributes[:image].is_a?(String)
      attributes[:image] = Base64.encode64(File.read(attributes[:image]))
    else
      attributes.delete(:image)
    end
  end

  if attributes.keys.map(&:to_sym).include?(:folders) && !attributes.keys.map(&:to_sym).include?(:folder)
    attributes[:folder] = attributes[:folders]
    attributes.delete(:folders)
  end

  attributes.delete(:user)
  attributes.delete(:created_at)
  attributes.delete(:updated_at)
  attributes.delete(:children_count)

  attributes
end
perform_copy(client, batch_client, scope_parameters, data_ids) click to toggle source

Executes proper copy request @param [Syncano::Clients::Base] client @param [Jimson::BatchClient] batch_client @param [Hash] scope_parameters @param [Array] data_ids @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 263
def self.perform_copy(client, batch_client, scope_parameters, data_ids)
  make_request(client, batch_client, :copy, { data_ids: data_ids }.merge(scope_parameters))
end
perform_count(client, scope_parameters, conditions) click to toggle source

Executes proper count request @param [Syncano::Clients::Base] client @param [Hash] scope_parameters @param [Hash] conditions @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 239
def self.perform_count(client, scope_parameters, conditions)
  make_request(client, nil, :count, conditions.merge(scope_parameters))
end
perform_move(client, batch_client, scope_parameters, data_ids, conditions, new_folder, new_state) click to toggle source

Executes proper move request @param [Syncano::Clients::Base] client @param [Jimson::BatchClient] batch_client @param [Hash] scope_parameters @param [Array] data_ids @param [Hash] conditions @param [String] new_folder @param [String] new_state @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 252
def self.perform_move(client, batch_client, scope_parameters, data_ids, conditions, new_folder, new_state)
  move_params = { new_folder: new_folder, new_state: new_state }.delete_if { |k, v| v.nil? }
  make_request(client, batch_client, :save, [conditions, { data_ids: data_ids }, move_params, scope_parameters].inject(&:merge))
end

Public Instance Methods

add_child(child_id, remove_other = false) click to toggle source

Wrapper for api “add_child” method @param [Integer] child_id @param [TrueClass, FalseClass] remove_other @return [Syncano::Resources::DataObject]

# File lib/syncano/resources/data_object.rb, line 164
def add_child(child_id, remove_other = false)
  perform_add_child(nil, child_id, remove_other)
  reload!
end
add_parent(parent_id, remove_other = false) click to toggle source

Wrapper for api “add_parent” method @param [Integer] parent_id @param [TrueClass, FalseClass] remove_other @return [Syncano::Resources::DataObject]

# File lib/syncano/resources/data_object.rb, line 130
def add_parent(parent_id, remove_other = false)
  response = perform_add_parent(nil, parent_id, remove_other)
  reload!
end
batch_add_child(batch_client, child_id, remove_other = false) click to toggle source

Batch version of “add_child” method @param [Jimson::BatchClient] batch_client @param [Integer] child_id @param [TrueClass, FalseClass] remove_other @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 174
def batch_add_child(batch_client, child_id, remove_other = false)
  perform_add_child(batch_client, child_id, remove_other)
end
batch_add_parent(batch_client, parent_id, remove_other = false) click to toggle source

Batch version of “add_parent” method @param [Jimson::BatchClient] batch_client @param [Integer] parent_id @param [TrueClass, FalseClass] remove_other @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 140
def batch_add_parent(batch_client, parent_id, remove_other = false)
  perform_add_parent(batch_client, parent_id, remove_other)
end
batch_copy(batch_client) click to toggle source

Batch version of “copy” method @param [Jimson::BatchClient] batch_client @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 122
def batch_copy(batch_client)
  self.class.batch_copy(batch_client, client, scope_parameters, id.to_s)
end
batch_move(batch_client, new_folder = nil, new_state = nil) click to toggle source

Batch version of “move” method @param [Jimson::BatchClient] batch_client @param [String] new_folder @param [String] new_state @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 89
def batch_move(batch_client, new_folder = nil, new_state = nil)
  perform_move(client, batch_client, scope_parameters, [id], {}, new_folder, new_state)
end
batch_remove_child(batch_client, child_id = nil) click to toggle source

Batch version of “remove_child” method @param [Jimson::BatchClient] batch_client @param [Integer] child_id @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 190
def batch_remove_child(batch_client, child_id = nil)
  perform_remove_child(batch_client, child_id)
end
batch_remove_parent(batch_client, parent_id = nil) click to toggle source

Batch version of “remove_parent” method @param [Jimson::BatchClient] batch_client @param [Integer] parent_id @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 156
def batch_remove_parent(batch_client, parent_id = nil)
  perform_remove_parent(batch_client, parent_id)
end
copy() click to toggle source

Wrapper for api “copy” method @return [Syncano::Resource::DataObject]

# File lib/syncano/resources/data_object.rb, line 115
def copy
  self.class.copy(client, scope_parameters, id.to_s).try(:first)
end
move(new_folder = nil, new_state = nil) click to toggle source

Wrapper for api “move” method @param [String] new_folder @param [String] new_state @return [Syncano::Resource::DataObject]

# File lib/syncano/resources/data_object.rb, line 79
def move(new_folder = nil, new_state = nil)
  perform_move(client, nil, scope_parameters, [id], {}, new_folder, new_state)
  reload!
end
remove_child(child_id = nil) click to toggle source

Wrapper for api “remove_child” method @param [Integer] child_id @return [Syncano::Resources::DataObject]

# File lib/syncano/resources/data_object.rb, line 181
def remove_child(child_id = nil)
  perform_remove_child(nil, child_id)
  reload!
end
remove_parent(parent_id = nil) click to toggle source

Wrapper for api “remove_parent” method @param [Integer] parent_id @return [Syncano::Resources::DataObject]

# File lib/syncano/resources/data_object.rb, line 147
def remove_parent(parent_id = nil)
  response = perform_remove_parent(nil, parent_id)
  reload!
end

Private Instance Methods

perform_add_child(batch_client, child_id, remove_other = false) click to toggle source

Executes proper add_child request @param [Jimson::BatchClient] batch_client @param [Integer] child_id @param [TrueClass, FalseClass] remove_other @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 296
def perform_add_child(batch_client, child_id, remove_other = false)
  self.class.make_request(client, batch_client, :add_child, scope_parameters.merge(
      self.class.primary_key_name => primary_key,
      child_id: child_id,
      remove_other: remove_other
  ))
end
perform_add_parent(batch_client, parent_id, remove_other = false) click to toggle source

Executes proper add_parent request @param [Jimson::BatchClient] batch_client @param [Integer] parent_id @param [TrueClass, FalseClass] remove_other @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 272
def perform_add_parent(batch_client, parent_id, remove_other = false)
  self.class.make_request(client, batch_client, :add_parent, scope_parameters.merge(
    self.class.primary_key_name => primary_key,
    parent_id: parent_id,
    remove_other: remove_other
  ))
end
perform_destroy(batch_client) click to toggle source

Overwritten destroy request with non-standard primary key @param [Jimson::BatchClient] batch_client @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 230
def perform_destroy(batch_client)
  self.class.make_request(client, batch_client, :destroy, scope_parameters.merge({ data_ids: [primary_key] }))
end
perform_remove_child(batch_client, child_id) click to toggle source

Executes proper remove_child request @param [Jimson::BatchClient] batch_client @param [Integer] child_id @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 308
def perform_remove_child(batch_client, child_id)
  self.class.make_request(client, batch_client, :remove_child, scope_parameters.merge(
      self.class.primary_key_name => primary_key,
      child_id: child_id
  ))
end
perform_remove_parent(batch_client, parent_id) click to toggle source

Executes proper remove_parent request @param [Jimson::BatchClient] batch_client @param [Integer] parent_id @return [Syncano::Response]

# File lib/syncano/resources/data_object.rb, line 284
def perform_remove_parent(batch_client, parent_id)
  self.class.make_request(client, batch_client, :remove_parent, scope_parameters.merge(
      self.class.primary_key_name => primary_key,
      parent_id: parent_id
  ))
end