Class: Siteleaf::Entity
- Inherits:
-
Object
show all
- Defined in:
- lib/siteleaf/entity.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Entity
Returns a new instance of Entity.
6
7
8
|
# File 'lib/siteleaf/entity.rb', line 6
def initialize(attributes = {})
self.attributes = attributes
end
|
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
4
5
6
|
# File 'lib/siteleaf/entity.rb', line 4
def error
@error
end
|
#message ⇒ Object
Returns the value of attribute message.
4
5
6
|
# File 'lib/siteleaf/entity.rb', line 4
def message
@message
end
|
Class Method Details
.all ⇒ Object
10
11
12
13
|
# File 'lib/siteleaf/entity.rb', line 10
def self.all
result = Client.get endpoint
result.map { |r| new(r) } if result.is_a? Array
end
|
.class_name ⇒ Object
54
55
56
|
# File 'lib/siteleaf/entity.rb', line 54
def self.class_name
self.name.split('::')[-1]
end
|
.create(attributes = {}) ⇒ Object
20
21
22
|
# File 'lib/siteleaf/entity.rb', line 20
def self.create(attributes = {})
new(attributes).save
end
|
.delete(identifier) ⇒ Object
24
25
26
|
# File 'lib/siteleaf/entity.rb', line 24
def self.delete(identifier)
Client.delete "#{endpoint}/#{identifier}"
end
|
.endpoint ⇒ Object
58
59
60
|
# File 'lib/siteleaf/entity.rb', line 58
def self.endpoint
"#{self.class_name.downcase}s"
end
|
.find(identifier) ⇒ Object
15
16
17
18
|
# File 'lib/siteleaf/entity.rb', line 15
def self.find(identifier)
result = Client.get "#{endpoint}/#{identifier}"
new(result) if result
end
|
Instance Method Details
#attributes ⇒ Object
46
47
48
|
# File 'lib/siteleaf/entity.rb', line 46
def attributes
Hash[self.instance_variables.map { |name| [name[1..-1], self.instance_variable_get(name)] }]
end
|
#attributes=(attributes = {}) ⇒ Object
50
51
52
|
# File 'lib/siteleaf/entity.rb', line 50
def attributes=(attributes = {})
attributes.each_pair { |k, v| self.instance_variable_set("@#{k}", v) }
end
|
#create_endpoint ⇒ Object
62
63
64
|
# File 'lib/siteleaf/entity.rb', line 62
def create_endpoint
self.class.endpoint
end
|
#delete ⇒ Object
42
43
44
|
# File 'lib/siteleaf/entity.rb', line 42
def delete
Client.delete entity_endpoint
end
|
#entity_endpoint ⇒ Object
66
67
68
|
# File 'lib/siteleaf/entity.rb', line 66
def entity_endpoint
"#{self.class.endpoint}/#{identifier}"
end
|
#identifier ⇒ Object
70
71
72
|
# File 'lib/siteleaf/entity.rb', line 70
def identifier
id
end
|
#save ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/siteleaf/entity.rb', line 28
def save
if identifier
result = Client.put entity_endpoint, attributes
else
result = Client.post create_endpoint, attributes
end
if result.is_a?(Hash)
self.attributes = result
return self
else
raise 'Invalid response'
end
end
|