Class: Miniphonic::ApiObject
- Inherits:
-
Object
- Object
- Miniphonic::ApiObject
show all
- Extended by:
- Helpers
- Includes:
- Helpers
- Defined in:
- lib/miniphonic/api_object.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helpers
delete_from_server, from_server, handle_response, path_to_payload, server_error, to_server
Constructor Details
#initialize(uuid = nil) ⇒ ApiObject
Returns a new instance of ApiObject.
32
33
34
|
# File 'lib/miniphonic/api_object.rb', line 32
def initialize(uuid = nil)
@uuid = uuid
end
|
Instance Attribute Details
#uuid ⇒ Object
Returns the value of attribute uuid.
18
19
20
|
# File 'lib/miniphonic/api_object.rb', line 18
def uuid
@uuid
end
|
Class Method Details
.all ⇒ Object
8
9
10
11
12
13
14
|
# File 'lib/miniphonic/api_object.rb', line 8
def all
from_server self.new.collection_url do |response|
return response.data.each_with_object([]) do |hash, objects|
objects << self.new(hash["uuid"])
end
end
end
|
Instance Method Details
#attributes_to_payload ⇒ Object
24
25
26
|
# File 'lib/miniphonic/api_object.rb', line 24
def attributes_to_payload
raise NotImplementedError, "#create has to be overridden in #{ self.class.name }"
end
|
#collection_endpoint ⇒ Object
74
75
76
|
# File 'lib/miniphonic/api_object.rb', line 74
def collection_endpoint
endpoint + "s"
end
|
#collection_url ⇒ Object
80
81
82
|
# File 'lib/miniphonic/api_object.rb', line 80
def collection_url
"/api/#{ collection_endpoint }.json"
end
|
#command(command, payload = nil) ⇒ Object
69
70
71
72
|
# File 'lib/miniphonic/api_object.rb', line 69
def command(command, payload = nil)
url = command_url(command)
to_server url, payload
end
|
#command_url(command) ⇒ Object
89
90
91
92
|
# File 'lib/miniphonic/api_object.rb', line 89
def command_url(command)
raise UuidError unless self.uuid
"/api/#{ endpoint }/#{ self.uuid }/#{ command }.json"
end
|
#create ⇒ Object
Create an API object on server, will conveniently send already set attributes
37
38
39
40
41
|
# File 'lib/miniphonic/api_object.rb', line 37
def create
to_server(collection_url, attributes_to_payload) do |response|
self.uuid = response.data["uuid"]
end
end
|
#delete ⇒ Object
Delete API object from server
65
66
67
|
# File 'lib/miniphonic/api_object.rb', line 65
def delete
delete_from_server single_url
end
|
#endpoint ⇒ Object
20
21
22
|
# File 'lib/miniphonic/api_object.rb', line 20
def endpoint
raise NotImplementedError, "#endpoint has to be overridden in #{ self.class.name }"
end
|
#get_attributes ⇒ Object
Update the local API object with the data from the server (Mostly to be used after ApiObject.new(uuid))
54
55
56
57
58
|
# File 'lib/miniphonic/api_object.rb', line 54
def get_attributes
from_server single_url do |response|
payload_to_attributes(response.data)
end
end
|
#payload_to_attributes(payload) ⇒ Object
28
29
30
|
# File 'lib/miniphonic/api_object.rb', line 28
def payload_to_attributes(payload)
raise NotImplementedError, "#attributes_to_payload has to be overridden in #{ self.class.name }"
end
|
#single_url ⇒ Object
84
85
86
87
|
# File 'lib/miniphonic/api_object.rb', line 84
def single_url
raise UuidError unless self.uuid
"/api/#{ endpoint }/#{ self.uuid }.json"
end
|
#update ⇒ Object
Reset the API object on server and replaces with current attributes (We can do this brute-force thing because we have local state.)
45
46
47
48
49
50
|
# File 'lib/miniphonic/api_object.rb', line 45
def update
url = single_url
payload = attributes_to_payload
payload[:reset_data] = true
to_server url, payload
end
|
#upload_cover(path) ⇒ Object
60
61
62
|
# File 'lib/miniphonic/api_object.rb', line 60
def upload_cover(path)
command :upload, path_to_payload(path, :image)
end
|