Class: Tunnlr::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/tunnlr/configurator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigurator

Returns a new instance of Configurator.



7
8
9
10
# File 'lib/tunnlr/configurator.rb', line 7

def initialize
  @ui = HighLine.new
  @not_configured=true
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



6
7
8
# File 'lib/tunnlr/configurator.rb', line 6

def email
  @email
end

#not_configuredObject

Returns the value of attribute not_configured.



6
7
8
# File 'lib/tunnlr/configurator.rb', line 6

def not_configured
  @not_configured
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/tunnlr/configurator.rb', line 6

def password
  @password
end

#subdomainObject

Returns the value of attribute subdomain.



6
7
8
# File 'lib/tunnlr/configurator.rb', line 6

def subdomain
  @subdomain
end

Instance Method Details

#add_passwordObject



22
23
24
# File 'lib/tunnlr/configurator.rb', line 22

def add_password
  @configuration.gsub!(/PASSWD/,password)
end

#configure(path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tunnlr/configurator.rb', line 40

def configure(path)
  while not_configured
    begin
      get_credentials
      fetch(subdomain, email,password)
      add_password
      write_config(path)
      puts "Created configuration in #{path}"
      self.not_configured=false
    rescue Net::HTTPNotAcceptable=>e
      puts "Login failed, please try again"
    end
  end
end

#fetch(subdomain, username, password) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/tunnlr/configurator.rb', line 12

def fetch(subdomain,username,password)
  url=URI.parse("https://#{subdomain}.tunnlr.com/configuration.yml")
  req = Net::HTTP::Get.new(url.path)
  req.basic_auth(username,password)
  res = Net::HTTP.start(url.host, url.port, :verify_mode => OpenSSL::SSL::VERIFY_NONE, :use_ssl => url.scheme == 'https') {|http|
        http.request(req)
      }
  @configuration = res.body
end

#get_credentialsObject



32
33
34
35
36
37
38
# File 'lib/tunnlr/configurator.rb', line 32

def get_credentials
  puts "Enter the email address and password you used to sign up for Tunnlr."
  self.subdomain = @ui.ask("Subdomain (i.e. elevatedrails for elevatedrails.tunnlr.com): ") 
  self.email = @ui.ask("Email: ") 
  self.password = @ui.ask("Password: ") { |q| q.echo = false }
  fetch(subdomain,email,password)
end

#write_config(path) ⇒ Object



26
27
28
29
30
# File 'lib/tunnlr/configurator.rb', line 26

def write_config(path)
  open(path,"w+") do |f|
    f.puts(@configuration)
  end
end