Module: Office::Login

Defined in:
lib/office-login.rb,
lib/office-login/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.get_configurationObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/office-login.rb', line 30

def self.get_configuration
  file = File.expand_path("~") + '/.office-login'
  if File.exists?(file)
    configuration = YAML.load_file file
  else
    configuration = {
      :log_in => 'https://host/login.html',
      :log_out => 'https://host/logout.html',
      :username => 'username',
      :password => 'password'
    }
  end

  configuration
end

.get_options(args) ⇒ Object



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
# File 'lib/office-login.rb', line 51

def self.get_options(args)
  options = { }
  OptionParser.new do |opts|
    opts.banner = "Usage: office-login [options]"

    opts.on("-a", "--log-in [STRING]", "URL for LOGIN-action") do |li|
      options[:log_in] = li
    end

    opts.on("-b", "--log-out [STRING]", "URL for LOGOUT-action") do |lo|
      options[:log_out] = lo
    end

    opts.on("-u", "--username [STRING]", "The username to be used") do |u|
      options[:username] = u
    end

    opts.on("-p", "--password [STRING]", "The password to be used") do |p|
      options[:password] = p
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end.parse!(args)

  options
end

.login(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/office-login.rb', line 9

def self.(args)

  options = Login::get_options(args)
  configuration = Login::get_configuration
  configuration.merge!(options)
  Login::store_configuration(configuration)

  Headless.ly do |headless|
    browser = Watir::Browser.new
    browser.goto configuration[:log_out]
    browser.goto configuration[:log_in]
    browser.text_field(:name => 'username').set(configuration[:username])
    browser.text_field(:name => 'password').set(configuration[:password])
    browser.button(:name => 'Submit').click
    browser.close
  end
rescue => error
  puts "Something did not work: #{error.inspect}"
  exit 1
end

.store_configuration(configuration) ⇒ Object



46
47
48
49
# File 'lib/office-login.rb', line 46

def self.store_configuration configuration
  file_name = File.expand_path("~") + '/.office-login'
  File.open(file_name, 'w') { |file| file.puts(configuration.to_yaml) }
end