Class: SafeNet::NFS
- Inherits:
-
Object
- Object
- SafeNet::NFS
- Defined in:
- lib/safenet.rb
Instance Method Summary collapse
-
#create_directory(dir_path, options = {}) ⇒ Object
Create a public or private directory either in the application’s root directory or in SAFE Drive.
-
#create_file(file_path, contents, options = {}) ⇒ Object
Create a file.
-
#create_private_directory(dir_path, options = {}) ⇒ Object
Alias.
-
#create_public_directory(dir_path, options = {}) ⇒ Object
Alias.
-
#delete_directory(dir_path, options = {}) ⇒ Object
Delete a directory.
-
#delete_file(file_path, options = {}) ⇒ Object
Delete a file.
-
#get_directory(dir_path, options = {}) ⇒ Object
Fetch a directory.
-
#get_file(file_path, options = {}) ⇒ Object
Read a file.
-
#get_file_meta(file_path, options = {}) ⇒ Object
Fetch the metadata of a file.
-
#initialize(client_obj) ⇒ NFS
constructor
A new instance of NFS.
-
#move_file(src_root_path, src_path, dst_root_path, dst_path, action = 'move') ⇒ Object
Move or copy a file.
-
#rename_directory(dir_path, new_name, options = {}) ⇒ Object
Alias.
-
#rename_file(file_path, new_name, options = {}) ⇒ Object
Alias.
-
#update_directory(dir_path, options = {}) ⇒ Object
Rename a directory and (optionally) update its metadata.
-
#update_file_meta(file_path, options = {}) ⇒ Object
Rename a file and (optionally) update its metadata.
Constructor Details
#initialize(client_obj) ⇒ NFS
Returns a new instance of NFS.
195 196 197 |
# File 'lib/safenet.rb', line 195 def initialize(client_obj) @client = client_obj end |
Instance Method Details
#create_directory(dir_path, options = {}) ⇒ Object
Create a public or private directory either in the application’s root
directory or in SAFE Drive.
Only authorised requests can create a directory.
Usage: my_client.nfs.create_directory(“/photos”) Adv.Usage: my_client.nfs.create_directory(“/photos”, meta: “some meta”, root_path: ‘drive’, is_private: true) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true
Reference: maidsafe.readme.io/docs/nfs-create-directory
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/safenet.rb', line 211 def create_directory(dir_path, = {}) # Default values [:root_path] = 'app' if ! .has_key?(:root_path) [:is_private] = true if ! .has_key?(:is_private) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/directory/#{options[:root_path]}/#{SafeNet.escape(dir_path)}" # Payload payload = { isPrivate: [:is_private], } # Optional payload["metadata"] = Base64.strict_encode64([:meta]) if .has_key?(:meta) # 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' => 'application/json' }) req.body = payload.to_json res = http.request(req) res.code == "200" ? true : JSON.parse(res.body) end |
#create_file(file_path, contents, options = {}) ⇒ Object
Create a file. Only authorised requests can invoke the API.
Usage: my_client.nfs.create_file(“/docs/hello.txt”, “Hello World!”) Adv.Usage: my_client.nfs.create_file(“/docs/hello.txt”, meta: “some meta”, root_path: “app”, content_type: “text/plain”) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true
Reference: maidsafe.readme.io/docs/nfsfile
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/safenet.rb', line 360 def create_file(file_path, contents, = {}) # Default values [:root_path] = 'app' if ! .has_key?(:root_path) contents ||= "" # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/#{options[:root_path]}/#{SafeNet.escape(file_path)}" # API call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) headers = { 'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}", } headers["Metadata"] = Base64.strict_encode64([:meta]) if .has_key?(:meta) headers["Content-Type"] = [:content_type] || 'text/plain' headers["Content-Length"] = contents.size.to_s req = Net::HTTP::Post.new(uri.path, headers) req.body = contents res = http.request(req) res.code == "200" ? true : JSON.parse(res.body) end |
#create_private_directory(dir_path, options = {}) ⇒ Object
Alias
246 247 248 249 |
# File 'lib/safenet.rb', line 246 def create_private_directory(dir_path, = {}) [:is_private] = true self.create_directory(dir_path, ) end |
#create_public_directory(dir_path, options = {}) ⇒ Object
Alias
240 241 242 243 |
# File 'lib/safenet.rb', line 240 def create_public_directory(dir_path, = {}) [:is_private] = false self.create_directory(dir_path, ) end |
#delete_directory(dir_path, options = {}) ⇒ Object
Delete a directory. Only authorised requests can invoke this API.
Rename: my_client.nfs.delete_directory(“/photos”) Change meta: my_client.nfs.delete_directory(“/photos”, root_path: “app”) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true
Reference: maidsafe.readme.io/docs/nfs-delete-directory
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/safenet.rb', line 331 def delete_directory(dir_path, = {}) # Default values [:root_path] = 'app' if ! .has_key?(:root_path) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/directory/#{options[:root_path]}/#{SafeNet.escape(dir_path)}" # 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(res.body) end |
#delete_file(file_path, options = {}) ⇒ Object
Delete a file. Only authorised requests can invoke the API.
Usage: my_client.nfs.delete_file(“/docs/hello.txt”) Adv.Usage: my_client.nfs.delete_file(“/docs/hello.txt”, root_path: “app”) Fail: “description”=>“FfiError::InvalidPath” Success: true
Reference: maidsafe.readme.io/docs/nfs-delete-file
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/safenet.rb', line 537 def delete_file(file_path, = {}) # Default values [:root_path] = 'app' if ! .has_key?(:root_path) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/#{options[:root_path]}/#{SafeNet.escape(file_path)}" # 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(res.body) end |
#get_directory(dir_path, options = {}) ⇒ Object
Fetch a directory. Only authorised requests can invoke this API.
Usage: my_client.nfs.get_directory(“/photos”, root_path: ‘drive’) Fail: “description”=>“FfiError::PathNotFound” Success: {“name”=> “my_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, = {}) # Default values [:root_path] = 'app' if ! .has_key?(:root_path) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/directory/#{options[:root_path]}/#{SafeNet.escape(dir_path)}" # 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(res.body) end |
#get_file(file_path, options = {}) ⇒ Object
Read a file. The file can be streamed in chunks and also fetched as partial content
based on the range header specified in the request.
Only authorised requests can invoke the API.
Usage: my_client.nfs.get_file(“/docs/hello.txt”) Adv.Usage: my_client.nfs.get_file(“/docs/hello.txt”, range: “bytes 0-1000”, root_path: “app”) Fail: “description”=>“FfiError::InvalidPath” Success: “content-range”=>“bytes 0-4/4”, “accept-ranges”=>“bytes”, “content-length”=>“4”, “created-on”=>“2016-08-14T12:51:18.924Z”, “last-modified”=>“2016-08-14T12:51:18.935Z”, “content-type”=>“text/plain”, “date”=>“Sun, 14 Aug 2016 13:30:07 GMT”, “connection”=>“close”, “body”=>“Test”}
Reference: maidsafe.readme.io/docs/nfs-get-file
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/safenet.rb', line 430 def get_file(file_path, = {}) # Default values [:offset] = 0 if ! .has_key?(:offset) [:root_path] = 'app' if ! .has_key?(:root_path) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/#{options[:root_path]}/#{SafeNet.escape(file_path)}?" # Query params query = [] query << "offset=#{options[:offset]}" query << "length=#{options[:length]}" if .has_key?(:length) # length is optional url = "#{url}#{query.join('&')}" # API call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) headers = { 'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}", } headers["Range"] = [:range] if .has_key?(:range) req = Net::HTTP::Get.new(uri.path, headers) res = http.request(req) res_headers = {} res.response.each_header {|k,v| res_headers[k] = v} res.code == "200" ? {"headers" => res_headers, "body" => res.body} : JSON.parse(res.body) end |
#get_file_meta(file_path, options = {}) ⇒ Object
Fetch the metadata of a file. Only authorised requests can invoke the API.
Usage: my_client.nfs.get_file_meta(“/docs/hello.txt”) Adv.Usage: my_client.nfs.get_file_meta(“/docs/hello.txt”, root_path: “app”) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success:
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/safenet.rb', line 395 def (file_path, = {}) # Default values [:root_path] = 'app' if ! .has_key?(:root_path) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/#{options[:root_path]}/#{SafeNet.escape(file_path)}" # API call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) headers = { 'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}", } req = Net::HTTP::Head.new(uri.path, headers) res = http.request(req) res_headers = {} res.response.each_header {|k,v| res_headers[k] = v} res_headers["metadata"] = Base64.strict_decode64(res_headers["metadata"]) if res_headers.has_key?("metadata") res.code == "200" ? {"headers" => res_headers, "body" => res.body} : JSON.parse(res.body) end |
#move_file(src_root_path, src_path, dst_root_path, dst_path, action = 'move') ⇒ Object
Move or copy a file
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/safenet.rb', line 502 def move_file(src_root_path, src_path, dst_root_path, dst_path, action = 'move') # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/movefile" # Payload payload = {} payload["srcRootPath"] = src_root_path # 'app' or 'drive' payload["srcPath"] = src_path payload["destRootPath"] = dst_root_path # 'app' or 'drive' payload["destPath"] = dst_path payload["action"] = action # 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' => 'application/json' }) req.body = payload.to_json res = http.request(req) res.code == "200" ? true : JSON.parse(res.body) end |
#rename_directory(dir_path, new_name, options = {}) ⇒ Object
Alias
315 316 317 318 |
# File 'lib/safenet.rb', line 315 def rename_directory(dir_path, new_name, = {}) [:name] = new_name self.update_directory(dir_path, ) end |
#rename_file(file_path, new_name, options = {}) ⇒ Object
Alias
495 496 497 498 |
# File 'lib/safenet.rb', line 495 def rename_file(file_path, new_name, = {}) [:name] = new_name self.(file_path) end |
#update_directory(dir_path, options = {}) ⇒ Object
Rename a directory and (optionally) update its metadata. Only authorised requests can invoke this API.
Rename: my_client.nfs.update_directory(“/photos”, name: “pics”) Change meta: my_client.nfs.update_directory(“/photos”, meta: “new meta”) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true
Reference: maidsafe.readme.io/docs/nfs-update-directory
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/safenet.rb', line 290 def update_directory(dir_path, = {}) # Default values [:root_path] = 'app' if ! .has_key?(:root_path) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/directory/#{options[:root_path]}/#{SafeNet.escape(dir_path)}" # Optional payload payload = {} payload["name"] = [:name] if .has_key?(:name) payload["metadata"] = Base64.strict_encode64([:meta]) if .has_key?(:meta) # 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' => 'application/json' }) req.body = payload.to_json res = http.request(req) res.code == "200" ? true : JSON.parse(res.body) end |
#update_file_meta(file_path, options = {}) ⇒ Object
Rename a file and (optionally) update its metadata. Only authorised requests can invoke the API.
Usage: my_client.nfs.update_file_metadata(“/docs/hello.txt”) Adv.Usage: my_client.nfs.get_file(“/docs/hello.txt”, range: “bytes 0-1000”, root_path: “app”) Fail: “description”=>“FfiError::InvalidPath” Success: “content-range”=>“bytes 0-4/4”, “accept-ranges”=>“bytes”, “content-length”=>“4”, “created-on”=>“2016-08-14T12:51:18.924Z”, “last-modified”=>“2016-08-14T12:51:18.935Z”, “content-type”=>“text/plain”, “date”=>“Sun, 14 Aug 2016 13:30:07 GMT”, “connection”=>“close”, “body”=>“Test”}
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/safenet.rb', line 470 def (file_path, = {}) # Default values [:root_path] = 'app' if ! .has_key?(:root_path) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/metadata/#{options[:root_path]}/#{SafeNet.escape(file_path)}" # Optional payload payload = {} payload["name"] = [:name] if .has_key?(:name) payload["metadata"] = Base64.strict_encode64([:meta]) if .has_key?(:meta) # 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' => 'application/json' }) req.body = payload.to_json res = http.request(req) res.code == "200" ? true : JSON.parse(res.body) end |