Class: SafeNet::NFS

Inherits:
Object
  • Object
show all
Defined in:
lib/safenet.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_obj) ⇒ NFS

Returns a new instance of NFS.



247
248
249
# File 'lib/safenet.rb', line 247

def initialize(client_obj)
  @client = client_obj
end

Instance Method Details

#create_directory(dir_path, options = {}) ⇒ Object

Create a directory using the NFS API. Only authorised requests can create a directory.

Usage: SafeNet.create_directory(“/photos”) Adv.Usage: SafeNet.create_directory(“/photos”, is_private: true, metadata: “some meta”, is_path_shared: false, is_versioned: false) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true

Reference: maidsafe.readme.io/docs/nfs-create-directory



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/safenet.rb', line 333

def create_directory(dir_path, options = {})
  # entry point
  url = "#{@client.app_info[:launcher_server]}nfs/directory"

  # default values
  options[:is_private]     = true  if ! options.has_key?(:is_private)
  options[:is_versioned]   = false if ! options.has_key?(:is_versioned)
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # payload
  payload = {
    dirPath: dir_path,
    isPrivate: options[:is_private],
    isVersioned: options[:is_versioned],
    isPathShared: options[:is_path_shared]
  }

  # optional
  payload["metadata"] = Base64.strict_encode64(options[:metadata]) if options.has_key?(:metadata)

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  req.body = @client.key_helper.encrypt(payload.to_json)
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(@client.key_helper.decrypt(res.body))
end

#delete_directory(dir_path, options = {}) ⇒ Object

ex.: delete_directory(“/photos”)



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/safenet.rb', line 367

def delete_directory(dir_path, options = {})
  # default values
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@client.app_info[:launcher_server]}nfs/directory/#{CGI.escape(dir_path)}/#{options[:is_path_shared]}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Delete.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(@client.key_helper.decrypt(res.body))
end

#delete_file(file_path, options = {}) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/safenet.rb', line 429

def delete_file(file_path, options = {})
  # default values
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@client.app_info[:launcher_server]}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Delete.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(@client.key_helper.decrypt(res.body))
end

#file(file_path, options = {}) ⇒ Object

Create a File using the NFS API. Only authorised requests can invoke the API.

Usage: SafeNet.file(“/photos/cat.jpg”) Adv.Usage: SafeNet.file(“/photos/cat.jpg”, is_private: true, metadata: “some meta”, is_path_shared: false, is_versioned: false) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true

Reference: maidsafe.readme.io/docs/nfsfile



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/safenet.rb', line 290

def file(file_path, options = {})
  url = "#{@client.app_info[:launcher_server]}nfs/file"

  # default values
  options[:is_private]     = true  if ! options.has_key?(:is_private)
  options[:is_versioned]   = false if ! options.has_key?(:is_versioned)
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # payload
  payload = {
    filePath: file_path,
    isPrivate: options[:is_private],
    isVersioned: options[:is_versioned],
    isPathShared: options[:is_path_shared]
  }

  # optional
  payload["metadata"] = Base64.strict_encode64(options[:metadata]) if options.has_key?(:metadata)

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  req.body = @client.key_helper.encrypt(payload.to_json)
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(@client.key_helper.decrypt(res.body))
end

#get_directory(dir_path, options = {}) ⇒ Object

Create a directory using the NFS API. Only authorised requests can create a directory.

Usage: SafeNet.get_directory(“/photos”, is_path_shared: false) Fail: “description”=>“FfiError::PathNotFound” Success: {“name”=> “Ruby Demo App-Root-Dir”, …, …}

Reference: maidsafe.readme.io/docs/nfs-get-directory



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/safenet.rb', line 261

def get_directory(dir_path, options = {})
  # default values
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@client.app_info[:launcher_server]}nfs/directory/#{CGI.escape(dir_path)}/#{options[:is_path_shared]}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  JSON.parse(@client.key_helper.decrypt(res.body))
end

#get_file(file_path, options = {}) ⇒ Object

options: offset, length, is_path_shared



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/safenet.rb', line 385

def get_file(file_path, options = {})
  # default values
  options[:offset]         = 0     if ! options.has_key?(:offset)
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@client.app_info[:launcher_server]}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}?"

  # query params are encrypted
  query = []
  query << "offset=#{options[:offset]}"
  query << "length=#{options[:length]}" if options.has_key?(:length) # length is optional
  url = "#{url}#{@client.key_helper.encrypt(query.join('&'))}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  res.code == "200" ? @client.key_helper.decrypt(res.body) : JSON.parse(@client.key_helper.decrypt(res.body))
end

#update_file_content(file_path, contents, options = {}) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/safenet.rb', line 409

def update_file_content(file_path, contents, options = {})
  # default values
  options[:offset]         = 0     if ! options.has_key?(:offset)
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@client.app_info[:launcher_server]}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}?offset=#{options[:offset]}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Put.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  req.body = @client.key_helper.encrypt(contents)
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(@client.key_helper.decrypt(res.body))
end