class RestfulModel

Attributes

_id[RW]
created_at[RW]

Public Class Methods

collection_name() click to toggle source
# File lib/restful_model.rb, line 6
def self.collection_name
  "#{self.to_s.downcase}s"
end
new(parent) click to toggle source
# File lib/restful_model.rb, line 11
def initialize(parent)
  if parent.is_a?(Populr)
    @_api = parent
  else
    @_parent = parent
    @_api = parent.instance_variable_get :@_api
  end
end

Public Instance Methods

==(comparison_object) click to toggle source
# File lib/restful_model.rb, line 20
def ==(comparison_object)
  comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && comparison_object._id == _id)
end
as_json(options = {}) click to toggle source
# File lib/restful_model.rb, line 40
def as_json(options = {})
  hash = {}
  setters = methods.grep(/^\w+=$/)
  setters.each do |setter|
    getter = setter.to_s.sub('=', '')
    unless options[:except] && options[:except].include?(getter)
      value = send(getter)
      unless value.is_a?(RestfulModelCollection)
        value = value.as_json(options) if value.respond_to?(:as_json)
        hash[getter] = value
      end
    end
  end
  hash
end
destroy() click to toggle source
# File lib/restful_model.rb, line 69
def destroy
  update('DELETE', '')
end
inflate(json) click to toggle source
# File lib/restful_model.rb, line 24
def inflate(json)
  setters = methods.grep(/^\w+=$/)
  setters.each do |setter|
    property_name = setter.to_s.sub('=', '')
    send(setter, json[property_name]) if json.has_key?(property_name)
  end
end
path(action = "") click to toggle source
# File lib/restful_model.rb, line 73
def path(action = "")
  action = "/#{action}" unless action.empty?
  prefix = @_parent ? @_parent.path : ''
  "#{prefix}#{_id}#{action}"
end
save!() click to toggle source
# File lib/restful_model.rb, line 32
def save!
  if _id
    update('PUT', '', as_json(:api_representation => true))
  else
    update('POST', '', as_json(:api_representation => true))
  end
end
update(http_method, action, data = {}) click to toggle source
# File lib/restful_model.rb, line 56
def update(http_method, action, data = {})
  http_method = http_method.downcase
  action_url = @_api.url_for_path(path(action))

  RestClient.send(http_method, action_url, data) do |response, request, result|
    unless http_method == 'delete'
      json = Populr.interpret_response(result, response, :expected_class => Object)
      inflate(json)
    end
  end
  self
end