Class: WebFacter::App

Inherits:
Object
  • Object
show all
Defined in:
lib/web-facter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filters = []) ⇒ App

Returns a new instance of App.



10
11
12
# File 'lib/web-facter.rb', line 10

def initialize(filters=[])
  @filters = filters
end

Class Method Details

.run!(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/web-facter.rb', line 36

def self.run!(options)

  conf = options[:config] ? ParseConfig.new(options[:config]) : false

  if conf && conf.get_value('filters')
    application = self.new(conf.get_value('filters').split(','))
  else
    application = self.new
  end

  daemonize = options[:daemonize]
  port = options[:port]

  if conf
    application = application.add_auth(conf) if conf.get_value('password')
    daemonize = conf.get_value('daemonize') ? conf.get_value('daemonize') == "true" : daemonize
    port = conf.get_value('port') ? conf.get_value('port') : port
  end

  Rack::Server.new(:app => application, :Port => port, :daemonize => daemonize).start

end

Instance Method Details

#add_auth(conf) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/web-facter.rb', line 25

def add_auth(conf)
  application = Rack::Auth::Basic.new(self) do |username, password|
    stored_username = conf.get_value('username')
    username_check = stored_username ? stored_username == username : true
    password_check = conf.get_value('password') == password
    username_check && password_check
  end
  application.realm = 'Web Facter'
  application
end

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/web-facter.rb', line 14

def call(env)
  response = Rack::Response.new
  response.header['Content-Type'] = 'application/json'
  facts = Facter.to_hash.dup
  @filters.each do |filter|
    facts.delete(filter.strip)
  end
  response.write JSON.pretty_generate(facts)
  response.finish
end