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



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

#emailObject

Option shortcuts



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

def email    ; options[: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

#lsObject

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

#passwordObject



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

def password ; options[: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

#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
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

#sessionObject



112
113
114
# File 'lib/megar/shell.rb', line 112

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

#usageObject

prints usage/help information



81
82
83
# File 'lib/megar/shell.rb', line 81

def usage
  self.class.usage
end