Class: Megar::Shell
- Inherits:
-
Object
- Object
- Megar::Shell
- Defined in:
- lib/megar/shell.rb
Overview
class that groks the megar command line options and invokes the required task
Constant Summary collapse
- OPTIONS =
defines the valid command line options
%w(help verbose email=s password=s)
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
holds the remaining command line arguments.
-
#options ⇒ Object
readonly
holds the parsed options.
Class Method Summary collapse
-
.usage ⇒ Object
prints usage/help information.
Instance Method Summary collapse
-
#email ⇒ Object
Option shortcuts.
-
#get(file_id) ⇒ Object
download file with
file_id
. -
#initialize(options, args) ⇒ Shell
constructor
initializes the shell with command line argments:.
-
#ls ⇒ Object
do file listing.
- #password ⇒ Object
-
#put(filenames) ⇒ Object
upload file(s)
filenames
. -
#run ⇒ Object
Command: execute the megar task according to the options provided on initialisation.
- #session ⇒ Object
-
#usage ⇒ Object
prints usage/help information.
Constructor Details
#initialize(options, args) ⇒ Shell
initializes the shell with command line argments:
options
is expected to be the hash structure as provided by GetOptions.new(..)
args
is the remaining command line arguments
18 19 20 21 |
# File 'lib/megar/shell.rb', line 18 def initialize(,args) @options = (||{}).each{|k,v| {k => v} } @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
holds the remaining command line arguments
10 11 12 |
# File 'lib/megar/shell.rb', line 10 def args @args end |
#options ⇒ Object (readonly)
holds the parsed options
7 8 9 |
# File 'lib/megar/shell.rb', line 7 def @options end |
Class Method Details
.usage ⇒ Object
prints usage/help information
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/megar/shell.rb', line 49 def usage $stderr.puts <<-EOS Megar v#{Megar::VERSION} =================================== Usage: megar [options] [commands] Options: -h | --help : shows command help -v | --verbose : run with verbose -e= | --email=value : email address for login -p= | --password=value : password for login Commands: (none) : will perform a basic connection test only ls : returns a full file listing get file_id : downloads the file with id file_id put file_name : uploads the file called "file_name" Examples: megar [email protected] --password=MyPassword ls megar -e [email protected] -p MyPassword ls megar -e [email protected] -p MyPassword get 74ZTXbyR megar -e [email protected] -p MyPassword put ../path/to/my_file.png EOS end |
Instance Method Details
#email ⇒ Object
Option shortcuts
117 |
# File 'lib/megar/shell.rb', line 117 def email ; [:email] ; end |
#get(file_id) ⇒ Object
download file with file_id
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/megar/shell.rb', line 93 def get(file_id) if file = session.files.find_by_id(file_id) $stderr.puts "Downloading #{file_id} to #{file.name}.." File.open(file.name,'wb') do |f| f.write file.body end else $stderr.puts "I couldn't find the file with ID #{file_id}" end end |
#ls ⇒ Object
do file listing
86 87 88 89 90 |
# File 'lib/megar/shell.rb', line 86 def ls session.files.each do |file| puts file end end |
#password ⇒ Object
118 |
# File 'lib/megar/shell.rb', line 118 def password ; [:password] ; end |
#put(filenames) ⇒ Object
upload file(s) filenames
105 106 107 108 109 110 |
# File 'lib/megar/shell.rb', line 105 def put(filenames) Array(filenames).each do |filename| $stderr.puts "Uploading #{filename}.." session.files.create(body: filename) end end |
#run ⇒ Object
Command: execute the megar task according to the options provided on initialisation
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/megar/shell.rb', line 24 def run if email && password $stderr.puts "Connecting to mega as #{email}.." raise "Failed to connect!" unless session.connected? case args.first when /ls/i ls when /get/i get(args[1]) when /put/i put(args.drop(1)) else $stderr.puts "Connected!" end else usage end end |
#session ⇒ Object
112 113 114 |
# File 'lib/megar/shell.rb', line 112 def session @session ||= Megar::Session.new(email: email, password: password) end |
#usage ⇒ Object
prints usage/help information
81 82 83 |
# File 'lib/megar/shell.rb', line 81 def usage self.class.usage end |