Class: Worochi::Agent::Box

Inherits:
Worochi::Agent show all
Defined in:
lib/worochi/agent/box.rb

Overview

The Worochi::Agent for Box API.

Instance Attribute Summary

Attributes inherited from Worochi::Agent

#options

Instance Method Summary collapse

Methods inherited from Worochi::Agent

#files, #files_and_folders, #folders, #initialize, #name, new, #push, #push_items, #remove, #set_dir, #set_options, #type

Constructor Details

This class inherits a constructor from Worochi::Agent

Instance Method Details

#delete(path) ⇒ Boolean

Deletes the file at ‘path` from Box.

Parameters:

  • path (String)

    path relative to current directory

Returns:

  • (Boolean)

    ‘true` if a file was actually deleted



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/worochi/agent/box.rb', line 58

def delete(path)
  abs_path = full_path(path)
  begin
    file = @client.file(abs_path)
    return false if file.nil?
    file.delete
  rescue RubyBox::AuthError
    box_error
  end
  true
end

#init_clientRubyBox::Client

Initializes ruby-box client.

Returns:

  • (RubyBox::Client)


10
11
12
13
14
15
# File 'lib/worochi/agent/box.rb', line 10

def init_client
  session = RubyBox::Session.new(
    client_id: 'dummy_id',
    access_token: options[:token])
  @client = RubyBox::Client.new(session)
end

#list(path = nil) ⇒ Array<Hash>

Returns a list of files and subdirectories at the remote path specified by ‘options`.

Parameters:

  • path (String) (defaults to: nil)

    path to list instead of the current directory

Returns:

  • (Array<Hash>)

    list of files and subdirectories



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/worochi/agent/box.rb', line 22

def list(path=nil)
  remote_path = list_path(path)
  begin
    folder = @client.folder(remote_path)
    raise Error if folder.nil?
    folder.items.map do |elem|
      {
        name: elem.name,
        path: "#{remote_path}/#{elem.name}",
        type: elem.type
      }
    end
  rescue RubyBox::AuthError
    box_error
  end
end

#push_item(item) ⇒ Object

Push a single Item to Box.

Parameters:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/worochi/agent/box.rb', line 42

def push_item(item)
  abs_path = full_path(item.path)
  begin
    folder = @client.create_folder(File.dirname(abs_path))
    raise Error if folder.nil?
    folder.upload_file(item.filename, item.content, true)
  rescue RubyBox::AuthError
    box_error
  end
  nil
end