Class: Inbox::RestfulModel
- Inherits:
-
Object
- Object
- Inbox::RestfulModel
show all
- Defined in:
- lib/restful_model.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(api, namespace_id = nil) ⇒ RestfulModel
Returns a new instance of RestfulModel.
12
13
14
15
16
|
# File 'lib/restful_model.rb', line 12
def initialize(api, namespace_id = nil)
raise StandardError.new unless api.class <= Inbox::API
@namespace_id = namespace_id
@_api = api
end
|
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
6
7
8
|
# File 'lib/restful_model.rb', line 6
def created_at
@created_at
end
|
#id ⇒ Object
Returns the value of attribute id.
4
5
6
|
# File 'lib/restful_model.rb', line 4
def id
@id
end
|
#namespace_id ⇒ Object
Returns the value of attribute namespace_id.
5
6
7
|
# File 'lib/restful_model.rb', line 5
def namespace_id
@namespace_id
end
|
Class Method Details
.collection_name ⇒ Object
8
9
10
|
# File 'lib/restful_model.rb', line 8
def self.collection_name
"#{self.to_s.downcase}s".split('::').last
end
|
Instance Method Details
#==(comparison_object) ⇒ Object
18
19
20
|
# File 'lib/restful_model.rb', line 18
def ==(comparison_object)
comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && comparison_object.id == id)
end
|
#as_json(options = {}) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/restful_model.rb', line 43
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 ⇒ Object
71
72
73
|
# File 'lib/restful_model.rb', line 71
def destroy
update('DELETE', '')
end
|
#inflate(json) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/restful_model.rb', line 22
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
|
#save! ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/restful_model.rb', line 30
def save!
if id
update('PUT', '', as_json())
else
update('POST', '', as_json())
end
end
|
#update(http_method, action, data = {}) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/restful_model.rb', line 59
def update(http_method, action, data = {})
http_method = http_method.downcase
::RestClient.send(http_method, self.url(action), data.to_json, :content_type => :json) do |response, request, result|
unless http_method == 'delete'
json = Inbox.interpret_response(result, response, :expected_class => Object)
inflate(json)
end
end
self
end
|
#url(action = "") ⇒ Object
38
39
40
41
|
# File 'lib/restful_model.rb', line 38
def url(action = "")
action = "/#{action}" unless action.empty?
@_api.url_for_path("/n/#{@namespace_id}/#{self.class.collection_name}/#{id}#{action}")
end
|