Class: RestfulModel

Inherits:
Object
  • Object
show all
Defined in:
lib/restful_model.rb

Direct Known Subclasses

Asset, Pop, Template, Tracer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ RestfulModel

Returns a new instance of RestfulModel.



11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#_idObject

Returns the value of attribute _id.



3
4
5
# File 'lib/restful_model.rb', line 3

def _id
  @_id
end

#created_atObject

Returns the value of attribute created_at.



4
5
6
# File 'lib/restful_model.rb', line 4

def created_at
  @created_at
end

Class Method Details

.collection_nameObject



6
7
8
# File 'lib/restful_model.rb', line 6

def self.collection_name
  "#{self.to_s.downcase}s"
end

Instance Method Details

#==(comparison_object) ⇒ Object



20
21
22
# 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 = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/restful_model.rb', line 41

def as_json(options = {})
  hash = {}
  setters = methods.grep(/^\w+=$/)
  setters.each do |setter|
    getter = setter.to_s[0..setter.to_s.index('=')-1]
    unless options[:except] && options[:except].include?(getter)
      value = self.send(getter)
      unless value.is_a? RestfulModelCollection
        hash[getter] = value
        if value.class.method_defined? :as_json
          value = value.as_json(options)
        end
      end
    end
  end
  hash
end

#inflate(json) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/restful_model.rb', line 24

def inflate(json)
  setters = methods.grep(/^\w+=$/)
  setters.each do |setter|
    property_name = setter.to_s[0..setter.to_s.index('=')-1]
    self.send(setter, json[property_name]) if json.has_key?(property_name)
  end
  self.created_at = Time.parse(self.created_at) if self.created_at
end

#path(action = "") ⇒ Object



70
71
72
73
74
# File 'lib/restful_model.rb', line 70

def path(action = "")
  action = "/#{action}" unless action.empty?
  prefix = @_parent ? @_parent.path : ''
  "#{prefix}#{_id}#{action}"
end

#save!Object



33
34
35
36
37
38
39
# File 'lib/restful_model.rb', line 33

def save!
  if _id
    update('PUT', '', self.as_json(:api_representation => true))
  else
    update('POST', '', self.as_json(:api_representation => true))
  end
end

#update(http_method, action, data = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/restful_model.rb', line 59

def update(http_method, action, data = {})
  http_method = http_method.downcase
  action_url = @_api.url_for_path(self.path(action))

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