Class: Livestatus::PatronHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/livestatus/handler/patron.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, config) ⇒ PatronHandler

Returns a new instance of PatronHandler.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/livestatus/handler/patron.rb', line 10

def initialize(connection, config)
  @connection = connection
  @session = Patron::Session.new
  @session.timeout = 10
  @session.headers["User-Agent"] = "livestatus/#{VERSION} ruby/#{RUBY_VERSION}"
  @session.insecure = config[:insecure]
  @session.auth_type = config.fetch(:auth_type, :basic).to_sym
  @session.username = config[:username]
  @session.password = config[:password]
  @uri = config[:uri]
end

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



8
9
10
# File 'lib/livestatus/handler/patron.rb', line 8

def session
  @session
end

Instance Method Details

#command(cmd, time = nil) ⇒ Object



28
29
30
31
32
# File 'lib/livestatus/handler/patron.rb', line 28

def command(cmd, time = nil)
  time = Time.now.to_i unless time
  query(:command, "[#{time}] #{cmd}")
  nil
end

#get(model, options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/livestatus/handler/patron.rb', line 22

def get(model, options = {})
  query(:get, model.table_name, options).map do |data|
    model.new(data, @connection)
  end
end

#query(method, query, headers = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/livestatus/handler/patron.rb', line 34

def query(method, query, headers = {})
  headers = Hash[headers.merge({
    :query => "#{method.to_s.upcase} #{query}"
  }).map do |k, v|
    v = Yajl::Encoder.encode(v) if v.is_a?(Array)
    ["X-Livestatus-#{k.to_s.dasherize}", v]
  end]

  result = session.get(@uri, headers)

  unless result.status == 200
    raise HandlerException, "livestatus query failed with status #{result.status}"
  end

  Yajl::Parser.parse(result.body)
end