module CiscoSpark::Model::ClassMethods

Public Instance Methods

attributes(hash=nil) click to toggle source
# File lib/cisco_spark/model.rb, line 18
def attributes(hash=nil)
  return @attributes unless hash

  @attributes = hash
  attr_accessor *@attributes.keys
end
create(attributes) click to toggle source
# File lib/cisco_spark/model.rb, line 44
def create(attributes)
  response = Api.new.post(@resource, attributes)
  parse(response.body)
end
destroy(id) click to toggle source
# File lib/cisco_spark/model.rb, line 55
def destroy(id)
  Api.new.delete("#{@resource}/#{id}")
end
fetch(id, options={}) click to toggle source
# File lib/cisco_spark/model.rb, line 39
def fetch(id, options={})
  response = Api.new.get("#{@resource}/#{id}", options)
  parse(response.body)
end
fetch_all(options={}) click to toggle source
# File lib/cisco_spark/model.rb, line 33
def fetch_all(options={})
  response = fetch_all_raw(options)
  collection = parse_collection(response.body)
  CiscoSpark::Collection.new(self, collection, response)
end
fetch_all_raw(options={}) click to toggle source
# File lib/cisco_spark/model.rb, line 29
def fetch_all_raw(options={})
  Api.new.get(@resource, options)
end
mutable_attributes(*attributes) click to toggle source
# File lib/cisco_spark/model.rb, line 25
def mutable_attributes(*attributes)
  @mutable_attributes = attributes
end
parse(hash) click to toggle source
# File lib/cisco_spark/model.rb, line 65
def parse(hash)
  hash = JSON.parse(hash) if hash.is_a?(String)
  params = attributes.each_with_object({}) do |(attribute, caster), params|
    params[attribute] = caster.call(hash[Utils.camelize(attribute)])
  end
  new(params)
end
parse_collection(object) click to toggle source
# File lib/cisco_spark/model.rb, line 59
def parse_collection(object)
  object = JSON.parse(object) if object.is_a?(String)
  object = object.fetch('items', []) if object.is_a?(Hash)
  object.map{ |hash| parse(hash) }
end
resource(resource=nil) click to toggle source
# File lib/cisco_spark/model.rb, line 12
def resource(resource=nil)
  return @resource unless resource

  @resource = resource
end
update(id, attributes) click to toggle source
# File lib/cisco_spark/model.rb, line 49
def update(id, attributes)
  attributes = attributes.select{ |name, _v| @mutable_attributes.include?(name) }
  response = Api.new.put("#{@resource}/#{id}", attributes)
  parse(response.body)
end