module SalsaLabs::SalsaObject::ClassMethods

Public Instance Methods

all() click to toggle source

@return [Array<Hash>] an array of all the objects

# File lib/salsa_labs/salsa_object.rb, line 32
def all
  SalsaLabs.request('api/getObjects.sjs', {object: object_name}) do |response|
    Hash.from_xml(response).
         try(:[], object_name).
         try(:[], 'item')
  end
end
count() click to toggle source

@return [Integer] the number of total objects

# File lib/salsa_labs/salsa_object.rb, line 21
def count
  SalsaLabs.request('api/getCounts.sjs', {object: object_name}) do |response|
    Hash.from_xml(response).
         try(:[], object_name).
         try(:[], 'count').
         try(:[], 'count').
         try(:to_i)
  end
end
get(key) click to toggle source

@param [String] key of the object being requested @return [Hash] a hash representing the requested object

# File lib/salsa_labs/salsa_object.rb, line 12
def get(key)
  SalsaLabs.request('api/getObject.sjs', {object: object_name, key: key}) do |response|
    Hash.from_xml(response).
         try(:[], object_name).
         try(:[], 'item')
  end
end
save(attributes) click to toggle source

@return [String] the created or modified object’s key @raise [APIResponseError]

if the request to save the object failed
# File lib/salsa_labs/salsa_object.rb, line 43
def save(attributes)
  attributes.merge!({ object: object_name })
  SalsaLabs.request('save', attributes) do |response|
    SalsaLabs::SaveResponse.new(response).key
  end
end

Private Instance Methods

object_name() click to toggle source

@return [String] the name of this class in lowercase and underscored format @example

class MySpecialClass
  include SalsaObject
end

MySpecialClass.object_name #=> 'my_special_class'
# File lib/salsa_labs/salsa_object.rb, line 59
def object_name
  self.to_s.split('::').last.gsub(/(?:([A-Za-z])|^)(?=[^a-z])/) { "#{$1}#{$1 && '_'}" }.downcase
end