Class: Megar::Shell

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options,args)
  @options = (options||{}).each{|k,v| {k => v} }
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

holds the remaining command line arguments



10
11
12
# File 'lib/megar/shell.rb', line 10

def args
  @args
end

#optionsObject (readonly)

holds the parsed options



7
8
9
# File 'lib/megar/shell.rb', line 7

def options
  @options
end

Class Method Details

.usageObject

prints usage/help information



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/megar/shell.rb', line 47

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

Examples:
  megar [email protected] --password=MyPassword ls
  megar -e [email protected] -p MyPassword ls

EOS
end

Instance Method Details

#emailObject

Option shortcuts



84
# File 'lib/megar/shell.rb', line 84

def email    ; options[:email]    ; end

#passwordObject



85
# File 'lib/megar/shell.rb', line 85

def password ; options[:password] ; end

#runObject

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
# 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
      session.files.each do |file|
        puts file
      end
    else
      $stderr.puts "Connected!"
    end
  else
    usage
  end
end

#sessionObject



79
80
81
# File 'lib/megar/shell.rb', line 79

def session
  @session ||= Megar::Session.new(email: email, password: password)
end

#usageObject

prints usage/help information



75
76
77
# File 'lib/megar/shell.rb', line 75

def usage
  self.class.usage
end