Module: GoogleDriveCompanion::Session
Constant Summary collapse
- PROTECTED_METHODS =
Public: Call methods, check against whitelist
Returns [something]
%w[push pull mv del]
Instance Method Summary collapse
-
#configs ⇒ Object
Set authentication creditials in ~/.google_drive/conf.yaml.
-
#del(file, permanently = false) ⇒ Object
Public: Deletes remote folder.
-
#get_file(ary) ⇒ Object
Public: Get a file from Google Drive.
- #handle_msg(cmd) ⇒ Object
-
#mv(src, dest, force = true) ⇒ Object
Public: Move file from one remote location to another.
-
#pull(remote_file, local_file = nil) ⇒ Object
Public: Downloads remote file.
-
#push(local_file, remote_file = nil, force = true) ⇒ Object
Public: Uploads local file to remote local.
-
#root ⇒ Object
Public: Return the top level folder of the accounts google drive.
- #send_protected(ary) ⇒ Object
- #session ⇒ Object
-
#split_file(name = "", sep = File::SEPARATOR) ⇒ Object
Public: Split filename into array by separator.
-
#traverse(collections, force = false) ⇒ Object
Public: Takes an array of names of gdrive folders and traverss them.
Instance Method Details
#configs ⇒ Object
Set authentication creditials in ~/.google_drive/conf.yaml
~/.google_drive/conf.yaml:
username: [email protected] password: hunter2
OR at the env level:
[email protected] password=hunter2 [cmd] [arg1, … ]
17 18 19 20 21 22 |
# File 'lib/google_drive_companion/session.rb', line 17 def configs @configs ||= begin fn = File.join(Dir.home, ".google_drive", "conf.yaml") configs = File.exists?(fn) ? YAML::load(File.open(fn)) : ENV end end |
#del(file, permanently = false) ⇒ Object
Public: Deletes remote folder
file - Remote file path, File::SEPARATOR delimited permanent - Delete permantantly? (or just put in trash) (def. false)
Returns boolean
189 190 191 192 |
# File 'lib/google_drive_companion/session.rb', line 189 def del(file, permanently = false) get_file(split_file(file)).delete(permanently) permanently end |
#get_file(ary) ⇒ Object
Public: Get a file from Google Drive
ary - Array of collections, last element is title of file
Example
get_file(["foo", "bar", "faz.rb"])
# => <GoogleDrive::File faz.rb>
Returns a GoogleDrive::File
101 102 103 104 105 |
# File 'lib/google_drive_companion/session.rb', line 101 def get_file(ary) title = ary.pop traverse(ary). files({ "title" => title, "title-exact" => "true" })[0] end |
#handle_msg(cmd) ⇒ Object
194 195 196 |
# File 'lib/google_drive_companion/session.rb', line 194 def handle_msg(cmd) send_protected(cmd) || "bad command!" end |
#mv(src, dest, force = true) ⇒ Object
Public: Move file from one remote location to another
src - Source remote path to file dest - Destination remote path for file force - Make destination folder path if not already (def. true) Examples
mv("/old/path/to/file.txt", "/new/path/")
# => mil;
Raise RuntimeError is remote file doesn’t exist Returns nil
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/google_drive_companion/session.rb', line 119 def mv(src, dest, force = true) src_ary = split_file(src) src_node = traverse(src_ary[0...-1]) file = get_file(src_ary) if file traverse(split_file(dest), force).add(file) src_node.remove(file) else raise "Remote file: #{file} doesn't exist yo!" end nil end |
#pull(remote_file, local_file = nil) ⇒ Object
Public: Downloads remote file
remote_file - Remote file path, File::SEPARATOR delimited local_file - Local file path, File::SEPARATOR delimited
Examples
pull("/remote/path/to/text.txt", "/tmp/text.txt")
# => [ file /tmp/text.txt ]
Returns an integer
173 174 175 176 177 178 179 180 181 |
# File 'lib/google_drive_companion/session.rb', line 173 def pull(remote_file, local_file = nil) remote_ary = split_file(remote_file) local_file ||= remote_ary.last get_file(remote_ary).download_to_file(local_file) rescue raise "problem locating remote file: #{remote_file}" end |
#push(local_file, remote_file = nil, force = true) ⇒ Object
Public: Uploads local file to remote local
remote_file - Remote file path, File::SEPARATOR delimited local_file - Local file path, File::SEPARATOR delimited
Examples
push("/tmp/text.txt")
# => [ remote file /tmp/text.txt ]
push("/tmp/text.txt", "/remote/path/to/text.txt")
# => [ remote file /remote/path/to/text.txt ]
Returns GoogleDrive::File
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/google_drive_companion/session.rb', line 148 def push(local_file, remote_file = nil, force = true) remote_file ||= local_file ary = split_file(remote_file) remote_fn = ary.pop file = session.upload_from_file(local_file, remote_fn) unless ary.empty? traverse(ary, force).add(file) root.remove(file) end file end |
#root ⇒ Object
Public: Return the top level folder of the accounts google drive
Returns GoogleDrive::Collection
45 46 47 |
# File 'lib/google_drive_companion/session.rb', line 45 def root session.root_collection end |
#send_protected(ary) ⇒ Object
54 55 56 57 58 |
# File 'lib/google_drive_companion/session.rb', line 54 def send_protected(ary) if PROTECTED_METHODS.include?(ary.first) send(ary.shift.to_sym, *ary) end end |
#session ⇒ Object
198 199 200 |
# File 'lib/google_drive_companion/session.rb', line 198 def session @session = GoogleDrive.login(configs["username"], configs["password"]) end |
#split_file(name = "", sep = File::SEPARATOR) ⇒ Object
Public: Split filename into array by separator
name - Name of file sep - Seperator (def. system separator)
Examples
split_file("/path/to/file.txt")
# => ["path", "to", "file.txt")
split_file(“-path-to-file.txt—-”, “-”) res.must_equal(exp)
Returns array
38 39 40 |
# File 'lib/google_drive_companion/session.rb', line 38 def split_file(name = "", sep = File::SEPARATOR) name.gsub(/^#{sep}+/, '').split(sep) end |
#traverse(collections, force = false) ⇒ Object
Public: Takes an array of names of gdrive folders and traverss them. If name is erroneous, it stops at last leaf; if forced, creates the subcollection
Examples
traverse(["foo", "bar"])
# => <collection "bar">
traverse(["foo", "bar", "baz"]) NOTE: bar doesn't exist
# => <collection "foo">
traverse(["foo", "bar", "baz"], :force) NOTE: bar doesn't exist
# => <collection "baz">
Returns a GoogleDrive::Collection
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/google_drive_companion/session.rb', line 74 def traverse(collections, force = false) node = root collections.each do |subcollection| new_node = node.subcollection_by_title(subcollection) if new_node node = new_node elsif force node = node.create_subcollection(subcollection) else break end end node end |