Module: GmailExtractor

Defined in:
lib/gmail_extractor.rb,
lib/gmail_extractor/email.rb,
lib/gmail_extractor/version.rb,
lib/gmail_extractor/email_extractor.rb,
lib/gmail_extractor/email_progressbar.rb,
lib/gmail_extractor/printer/file_printer.rb,
lib/gmail_extractor/printer/console_printer.rb,
lib/gmail_extractor/email_formatter/email_csv_formatter.rb,
lib/gmail_extractor/email_formatter/email_xml_formatter.rb,
lib/gmail_extractor/email_formatter/email_html_formatter.rb

Defined Under Namespace

Classes: ConsolePrinter, Email, EmailCsvFormatter, EmailExtractor, EmailHtmlFormatter, EmailProgressbar, EmailXmlFormatter, FilePrinter

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.executeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gmail_extractor.rb', line 21

def self.execute
  # Reading from command line
  require "optparse"
  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: get_mails.rb [options]"
    opts.on("-u","--username USERNAME","Set gmail USERNAME") do |username|
      options[:username] = username
    end
    opts.on("-p","--password PASSWORD","Set gmail PASSWORD") do |password|
      options[:password] = password
    end
    opts.on("-n","--name NAME","Set own NAME; defaults to Self if not given") do |name|
      options[:name] = name || "Self"
    end
    opts.on("-m","--mailbox MAILBOX","Set MAILBOX from where the mails are fetched. Label names can be used too!") do |mailbox|
      options[:mailbox] = mailbox
    end
    opts.on("-l","--limit LIMIT","Set limit LIMIT to how many emails are fetched; defaults to all if not given") do |limit|
      options[:limit] = limit.to_i
    end
    opts.on("-o","--output TYPE","Set output TYPE to xml, csv or html. defaults to csv") do |output|
      options[:output] = output || "csv"
    end
    opts.on("-f","--file FILE","Set output FILE. if not present, only output to STDOUT") do |file|
      options[:file] = file
    end
    opts.on("-pb","--progressbar","Show progressbar on STDOUT") do
      options[:progressbar] = true
    end
    opts.on("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end.parse!

  # set printer
  file = options[:file]
  if file
    printer = ConsolePrinter.new(FilePrinter.new(file))
  else
    printer = ConsolePrinter.new
  end

  # set correct formatter
  if options[:output]
    output = options[:output].downcase
  else
    output = "csv"
  end

  if output == "xml"
    formatter = EmailXmlFormatter.new(printer)
  elsif output == "csv"
    formatter = EmailCsvFormatter.new(printer)
  elsif output == "html"
    formatter = EmailHtmlFormatter.new(printer)
  else
    raise "Internal error - no output option given"
  end

  # handle progressbar
  if options[:progressbar]
    formatter = EmailProgressbar.new(formatter)
  end

  # Asking missing values
  require "highline/import"
  user = options[:username] || ask("Enter user: ")
  password = options[:password] || ask("Enter password: ") { |q| q.echo = false }
  name = options[:name]
  extractor = EmailExtractor.new(user,password,name,formatter)

  # Ask missing values and extract
  label = options[:mailbox] || ask("Enter mailbox/label: ")
  limit = options[:limit]
  extractor.extract(label, limit)
end