class Miniphonic::ApiObject

Attributes

uuid[RW]

Public Class Methods

all() click to toggle source
# 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
new(uuid = nil) click to toggle source
# File lib/miniphonic/api_object.rb, line 32
def initialize(uuid = nil)
  @uuid = uuid
end

Public Instance Methods

attributes_to_payload() click to toggle source
# 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() click to toggle source
# File lib/miniphonic/api_object.rb, line 74
def collection_endpoint
  endpoint + "s"
end
collection_url() click to toggle source

URLs

# File lib/miniphonic/api_object.rb, line 80
def collection_url
  "/api/#{ collection_endpoint }.json"
end
command(command, payload = nil) click to toggle source
# 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) click to toggle source
# 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() click to toggle source

Create an API object on server, will conveniently send already set attributes

# 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() click to toggle source

Delete API object from server

# File lib/miniphonic/api_object.rb, line 65
def delete
  delete_from_server single_url
end
endpoint() click to toggle source
# File lib/miniphonic/api_object.rb, line 20
def endpoint
  raise NotImplementedError, "#endpoint has to be overridden in #{ self.class.name }"
end
get_attributes() click to toggle source

Update the local API object with the data from the server (Mostly to be used after ApiObject.new(uuid))

# 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) click to toggle source
# 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() click to toggle source
# File lib/miniphonic/api_object.rb, line 84
def single_url
  raise UuidError unless self.uuid
  "/api/#{ endpoint }/#{ self.uuid }.json"
end
update() click to toggle source

Reset the API object on server and replaces with current attributes (We can do this brute-force thing because we have local state.)

# 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) click to toggle source
# File lib/miniphonic/api_object.rb, line 60
def upload_cover(path)
  command :upload, path_to_payload(path, :image)
end