Class: Layer::Resource
- Inherits:
-
Object
show all
- Defined in:
- lib/layer/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes) ⇒ Resource
Returns a new instance of Resource.
5
6
7
|
# File 'lib/layer/resource.rb', line 5
def initialize(attributes)
@attributes = attributes
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
25
26
27
28
|
# File 'lib/layer/resource.rb', line 25
def method_missing(method, *args, &block)
method_key = method.to_s
attributes.has_key?(method_key) ? attributes[method_key] : super
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/layer/resource.rb', line 3
def attributes
@attributes
end
|
#client ⇒ Object
Returns the value of attribute client.
3
4
5
|
# File 'lib/layer/resource.rb', line 3
def client
@client
end
|
Class Method Details
.class_name ⇒ Object
40
41
42
|
# File 'lib/layer/resource.rb', line 40
def class_name
name.split("::").last
end
|
.client ⇒ Object
52
53
54
55
56
57
|
# File 'lib/layer/resource.rb', line 52
def client
Layer::HttpClient.new(
ENV['LAYER_APP_ID'],
ENV['LAYER_API_TOKEN']
)
end
|
.create(url, params = {}) ⇒ Object
59
60
61
62
|
# File 'lib/layer/resource.rb', line 59
def create(url, params = {})
response = client.post(url, body: params.to_json)
new(response)
end
|
.find(url, id) ⇒ Object
64
65
66
67
|
# File 'lib/layer/resource.rb', line 64
def find(url, id)
response = client.get("#{url}/#{id}")
new(response)
end
|
.list(url, params = {}) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/layer/resource.rb', line 69
def list(url, params = {})
collection = client.get(url, body: params.to_json)
if collection.any?
collection.map{ |resource| new(resource) }
else
[]
end
end
|
.pluralized_name ⇒ Object
44
45
46
|
# File 'lib/layer/resource.rb', line 44
def pluralized_name
"#{class_name}s"
end
|
.url ⇒ Object
48
49
50
|
# File 'lib/layer/resource.rb', line 48
def url
pluralized_name.downcase
end
|
Instance Method Details
#destroy ⇒ Object
17
18
19
|
# File 'lib/layer/resource.rb', line 17
def destroy
client.delete(url)
end
|
#respond_to_missing?(method, include_private = false) ⇒ Boolean
30
31
32
33
|
# File 'lib/layer/resource.rb', line 30
def respond_to_missing?(method, include_private = false)
method_key = method.to_s
attributes.has_key?(method_key) ? true : false
end
|
#update(params) ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/layer/resource.rb', line 9
def update(params)
client.patch(
url,
body: params.to_json,
headers: client.
)
end
|
#uuid ⇒ Object
35
36
37
|
# File 'lib/layer/resource.rb', line 35
def uuid
attributes["id"].split("/").last if attributes["id"]
end
|