Class: Siteleaf::Entity

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

Direct Known Subclasses

Collection, Content, File, Job, Role, Site, SourceFile, User

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

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

Class Method Details

.allObject



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_nameObject



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

.endpointObject



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

#attributesObject



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_endpointObject



62
63
64
# File 'lib/siteleaf/entity.rb', line 62

def create_endpoint
  self.class.endpoint
end

#deleteObject



42
43
44
# File 'lib/siteleaf/entity.rb', line 42

def delete
  Client.delete entity_endpoint
end

#entity_endpointObject



66
67
68
# File 'lib/siteleaf/entity.rb', line 66

def entity_endpoint
  "#{self.class.endpoint}/#{identifier}"
end

#identifierObject



70
71
72
# File 'lib/siteleaf/entity.rb', line 70

def identifier
  id
end

#saveObject



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