Class: Manifestly::Entity::Endpoint
- Inherits:
-
Base
- Object
- Base
- Manifestly::Entity::Endpoint
show all
- Defined in:
- lib/manifestly/entity/endpoint.rb
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
Class Method Details
.client ⇒ Object
37
38
39
|
# File 'lib/manifestly/entity/endpoint.rb', line 37
def self.client
@client ||= Manifestly::Client.new
end
|
.endpoint_target ⇒ Object
9
10
11
|
# File 'lib/manifestly/entity/endpoint.rb', line 9
def self.endpoint_target
raise 'Must specify endpoint_target'
end
|
.get(id, path: location) ⇒ Object
54
55
56
57
58
|
# File 'lib/manifestly/entity/endpoint.rb', line 54
def self.get(id, path: location)
response = client.get("#{path}/#{id}")
json_entity = JSON.parse(response[:body], symbolize_names: true)[singular_endpoint_target.to_sym]
new(json_entity)
end
|
.list(path: location, **params) ⇒ Object
48
49
50
51
52
|
# File 'lib/manifestly/entity/endpoint.rb', line 48
def self.list(path: location, **params)
response = client.get(path, params: params)
json_entities = JSON.parse(response[:body], symbolize_names: true)[endpoint_target]
json_entities.map { |it| new(it) }
end
|
.location ⇒ Object
29
30
31
|
# File 'lib/manifestly/entity/endpoint.rb', line 29
def self.location
endpoint_target
end
|
.singular_endpoint_target ⇒ Object
17
18
19
|
# File 'lib/manifestly/entity/endpoint.rb', line 17
def self.singular_endpoint_target
endpoint_target.to_s.chomp('s').to_sym
end
|
Instance Method Details
#client ⇒ Object
33
34
35
|
# File 'lib/manifestly/entity/endpoint.rb', line 33
def client
self.class.client
end
|
#create(path: location) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/manifestly/entity/endpoint.rb', line 41
def create(path: location)
response = client.post(path, params: to_h)
json_entity = JSON.parse(response[:body], symbolize_names: true)[singular_endpoint_target]
self.attributes = json_entity
self
end
|
#delete(path: location) ⇒ Object
73
74
75
76
|
# File 'lib/manifestly/entity/endpoint.rb', line 73
def delete(path: location)
client.delete(path, params: {external_id: external_id})
nil
end
|
#endpoint_target ⇒ Object
13
14
15
|
# File 'lib/manifestly/entity/endpoint.rb', line 13
def endpoint_target
self.class.endpoint_target
end
|
#location ⇒ Object
25
26
27
|
# File 'lib/manifestly/entity/endpoint.rb', line 25
def location
self.class.location
end
|
#save ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/manifestly/entity/endpoint.rb', line 65
def save
if id
update
else
create
end
end
|
#singular_endpoint_target ⇒ Object
21
22
23
|
# File 'lib/manifestly/entity/endpoint.rb', line 21
def singular_endpoint_target
self.class.singular_endpoint_target
end
|
#update(path: location) ⇒ Object
60
61
62
63
|
# File 'lib/manifestly/entity/endpoint.rb', line 60
def update(path: location)
client.post("#{path}/#{id}", params: to_h)
self
end
|