Class: Layer::Resource

Inherits:
Object
  • 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, client) ⇒ Resource

Returns a new instance of Resource.



5
6
7
8
# File 'lib/layer/resource.rb', line 5

def initialize(attributes, client)
  @attributes = attributes
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



22
23
24
25
# File 'lib/layer/resource.rb', line 22

def method_missing(method, *args, &block)
  method_key = method.to_s
  attributes.has_key?(method_key) ? attributes[method_key] : super
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/layer/resource.rb', line 3

def attributes
  @attributes
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/layer/resource.rb', line 3

def client
  @client
end

Class Method Details

.class_nameObject



43
44
45
# File 'lib/layer/resource.rb', line 43

def class_name
  name.split("::").last
end

.create(client, url, params = {}) ⇒ Object



55
56
57
58
# File 'lib/layer/resource.rb', line 55

def create(client, url, params = {})
  response = client.post(url, body: params.to_json)
  new(response, client)
end

.find(client, url, id) ⇒ Object



60
61
62
63
# File 'lib/layer/resource.rb', line 60

def find(client, url, id)
  response = client.get("#{url}/#{id}")
  new(response, client)
end

.list(client, url, params = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/layer/resource.rb', line 65

def list(client, url, params = {})
  collection = client.get(url, body: params.to_json)

  if collection.any?
    collection.map{ |resource| new(resource, client) }
  else
    []
  end
end

.pluralized_nameObject



47
48
49
# File 'lib/layer/resource.rb', line 47

def pluralized_name
  "#{class_name}s"
end

.urlObject



51
52
53
# File 'lib/layer/resource.rb', line 51

def url
  pluralized_name.downcase
end

Instance Method Details

#destroyObject



18
19
20
# File 'lib/layer/resource.rb', line 18

def destroy
  client.delete(url)
end

#inspectObject Also known as: to_s



36
37
38
# File 'lib/layer/resource.rb', line 36

def inspect
  "#<#{self.class} attributes=#{@attributes}>"
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/layer/resource.rb', line 27

def respond_to_missing?(method, include_private = false)
  method_key = method.to_s
  attributes.has_key?(method_key) ? true : false
end

#update(params) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/layer/resource.rb', line 10

def update(params)
  client.patch(
    url,
    body: params.to_json,
    headers: client.layer_patch_header
  )
end

#uuidObject



32
33
34
# File 'lib/layer/resource.rb', line 32

def uuid
  attributes["id"].split("/").last if attributes["id"]
end