Class: Inbox::RestfulModel
Class Method Summary
collapse
Instance Method Summary
collapse
time_attr_accessor
Methods included from Parameters
included, #parameters
Constructor Details
#initialize(api, namespace_id = nil) ⇒ RestfulModel
Returns a new instance of RestfulModel.
18
19
20
21
22
|
# File 'lib/restful_model.rb', line 18
def initialize(api, namespace_id = nil)
raise StandardError.new unless api.class <= Inbox::API
@namespace_id = namespace_id
@_api = api
end
|
Class Method Details
.collection_name ⇒ Object
14
15
16
|
# File 'lib/restful_model.rb', line 14
def self.collection_name
"#{self.to_s.downcase}s".split('::').last
end
|
Instance Method Details
#==(comparison_object) ⇒ Object
24
25
26
|
# File 'lib/restful_model.rb', line 24
def ==(comparison_object)
comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && comparison_object.id == id)
end
|
#as_json(options = {}) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/restful_model.rb', line 47
def as_json(options = {})
hash = {}
parameters.each do |getter|
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
73
74
75
76
77
|
# File 'lib/restful_model.rb', line 73
def destroy
::RestClient.send('delete', self.url) do |response, request|
Inbox.interpret_http_status(response)
end
end
|
#inflate(json) ⇒ Object
28
29
30
31
32
|
# File 'lib/restful_model.rb', line 28
def inflate(json)
parameters.each do |property_name|
send("#{property_name}=", json[property_name]) if json.has_key?(property_name)
end
end
|
#save! ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/restful_model.rb', line 34
def save!
if id
update('PUT', '', as_json())
else
update('POST', '', as_json())
end
end
|
#update(http_method, action, data = {}) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/restful_model.rb', line 61
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
42
43
44
45
|
# File 'lib/restful_model.rb', line 42
def url(action = "")
action = "/#{action}" unless action.empty?
@_api.url_for_path("/n/#{@namespace_id}/#{self.class.collection_name}/#{id}#{action}")
end
|