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.



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

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

Instance Attribute Details

#emailObject

Returns the value of attribute email.



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

def email
  @email
end

#not_configuredObject

Returns the value of attribute not_configured.



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

def not_configured
  @not_configured
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#subdomainObject

Returns the value of attribute subdomain.



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

def subdomain
  @subdomain
end

Instance Method Details

#add_passwordObject



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

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

#configure(path) ⇒ Object



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

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(username, password) ⇒ Object



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

def fetch(username,password)
  url=URI.parse("http://#{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) {|http|
        http.request(req)
      }
  @configuration = res.body
end

#get_credentialsObject



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

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(email,password)
end

#write_config(path) ⇒ Object



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

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