class PogoPlug::Client
Attributes
token[RW]
Public Instance Methods
create_directory(device_id, service_id, directory_name, parent_id=nil)
click to toggle source
# File lib/pogoplug/client.rb, line 62 def create_directory(device_id, service_id, directory_name, parent_id=nil) create_file(device_id, service_id, File.new(name: directory_name, parent_id: parent_id, type: File::Type::DIRECTORY)) end
create_file(device_id, service, file, io=nil)
click to toggle source
Creates a file handle and optionally attach an io. The provided file argument is expected to contain at minimum a name, type and parent_id. If it has a mimetype that will be assumed to match the mimetype of the io.
# File lib/pogoplug/client.rb, line 70 def create_file(device_id, service, file, io=nil) params = { valtoken: @token, deviceid: device_id, serviceid: service.id, filename: file.name, type: file.type } params[:parentid] = file.parent_id unless file.parent_id.nil? response = self.class.get('/createFile', query: params) file_handle = File.from_json(response.parsed_response['file']) if io send_file(device_id, service, file_handle, io) file_handle.size = io.size end file_handle end
delete(device_id, service_id, file)
click to toggle source
# File lib/pogoplug/client.rb, line 87 def delete(device_id, service_id, file) params = { valtoken: @token, deviceid: device_id, serviceid: service_id, fileid: file.id } response = self.class.get('/removeFile', query: params) true unless response.code.to_s != '200' end
devices()
click to toggle source
Retrieve a list of devices that are registered with the PogoPlug
account
# File lib/pogoplug/client.rb, line 31 def devices validate_token response = self.class.get('/listDevices', query: { valtoken: @token }) devices = [] response.parsed_response['devices'].each do |d| devices << Device.from_json(d) end devices end
download(device_id, service, file)
click to toggle source
# File lib/pogoplug/client.rb, line 82 def download(device_id, service, file) raise "Directories cannot be downloaded" unless file.file? open(URI.escape("#{service.api_url}files/#{@token}/#{device_id}/#{service.id}/#{file.id}/dl/#{file.name}")).read end
files(device_id, service_id, offset=0)
click to toggle source
Retrieve a list of files for a device and service
# File lib/pogoplug/client.rb, line 56 def files(device_id, service_id, offset=0) params = { valtoken: @token, deviceid: device_id, serviceid: service_id, pageoffset: offset } response = self.class.get('/listFiles', query: params) FileListing.from_json(response.parsed_response) end
login(email, password)
click to toggle source
Retrieve an auth token that can be used to make additional calls
-
Raises :
-
AuthenticationError
-> ifPogoPlug
does not like the credentials you provided
-
# File lib/pogoplug/client.rb, line 23 def login(email, password) response = self.class.get('/loginUser', query: { email: email, password: password }) raise_errors(response) @token = response.parsed_response["valtoken"] return self end
services(device_id=nil, shared=false)
click to toggle source
Retrieve a list of services
# File lib/pogoplug/client.rb, line 42 def services(device_id=nil, shared=false) validate_token params = { valtoken: @token, shared: shared } params[:deviceid] = device_id unless device_id.nil? response = self.class.get('/listServices', query: params) services = [] response.parsed_response['services'].each do |s| services << Service.from_json(s) end services end
version()
click to toggle source
Retrieve the current version information of the service
# File lib/pogoplug/client.rb, line 14 def version response = self.class.get('/getVersion') json = JSON.parse(response.body) ApiVersion.new(json['version'], json['builddate']) end
Private Instance Methods
raise_errors(response)
click to toggle source
# File lib/pogoplug/client.rb, line 101 def raise_errors(response) if response.parsed_response['HB-EXCEPTION'] && response.parsed_response['HB-EXCEPTION']['ecode'] == 606 raise AuthenticationError end end
send_file(device_id, service, file_handle, io)
click to toggle source
# File lib/pogoplug/client.rb, line 107 def send_file(device_id, service, file_handle, io) parent = file_handle.id || 0 uri = URI.parse("#{service.api_url}files/#{@token}/#{device_id}/#{service.id}/#{parent}/#{file_handle.name}") req = Net::HTTP::Put.new(uri.path) req['Content-Length'] = io.size req['Content-Type'] = file_handle.mimetype req.body_stream = io put_response = Net::HTTP.new(uri.host, uri.port).request(req) end
validate_token()
click to toggle source
# File lib/pogoplug/client.rb, line 95 def validate_token if @token.nil? raise AuthenticationError('Authentication token is missing. Call login first.') end end