Class: Manifestly::Entity::Endpoint

Inherits:
Base
  • Object
show all
Defined in:
lib/manifestly/entity/endpoint.rb

Direct Known Subclasses

ChecklistRun, ChecklistStep, User, Workflow

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attr_accessor, attr_reader, #attributes, attributes, #attributes=, #initialize, invalid_class_method, invalid_method, #to_h

Constructor Details

This class inherits a constructor from Manifestly::Entity::Base

Class Method Details

.clientObject



21
22
23
# File 'lib/manifestly/entity/endpoint.rb', line 21

def self.client
  @client ||= Manifestly::Client.new
end

.get(id) ⇒ Object



38
39
40
41
42
# File 'lib/manifestly/entity/endpoint.rb', line 38

def self.get(id)
  response = client.get("#{path}/#{id}")
  json_entity = JSON.parse(response[:body], symbolize_names: true)[path.chomp('s').to_sym]
  new(json_entity)
end

.list(**params) ⇒ Object



32
33
34
35
36
# File 'lib/manifestly/entity/endpoint.rb', line 32

def self.list(**params)
  response = client.get(path, params: params)
  json_entities = JSON.parse(response[:body], symbolize_names: true)[path.to_sym]
  json_entities.map { |it| new(it) }
end

.pathObject



13
14
15
# File 'lib/manifestly/entity/endpoint.rb', line 13

def self.path
  raise 'Must override method'
end

Instance Method Details

#clientObject



17
18
19
# File 'lib/manifestly/entity/endpoint.rb', line 17

def client
  self.class.client
end

#createObject



25
26
27
28
29
30
# File 'lib/manifestly/entity/endpoint.rb', line 25

def create
  response = client.post(path, params: to_h)
  json_entity = JSON.parse(response[:body], symbolize_names: true)[path.chomp('s').to_sym]
  self.attributes = json_entity
  self
end

#deleteObject



57
58
59
60
# File 'lib/manifestly/entity/endpoint.rb', line 57

def delete
  client.delete(path, params: {external_id: external_id})
  nil
end

#pathObject



9
10
11
# File 'lib/manifestly/entity/endpoint.rb', line 9

def path
  self.class.path
end

#saveObject



49
50
51
52
53
54
55
# File 'lib/manifestly/entity/endpoint.rb', line 49

def save
  if id
    update
  else
    create
  end
end

#updateObject



44
45
46
47
# File 'lib/manifestly/entity/endpoint.rb', line 44

def update
  client.post("#{path}/#{id}", params: to_h)
  self
end