Class: Livestatus::PatronHandler
- Inherits:
-
Object
- Object
- Livestatus::PatronHandler
- Defined in:
- lib/livestatus/handler/patron.rb
Instance Attribute Summary collapse
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
- #command(cmd, time = nil) ⇒ Object
- #get(model, options = {}) ⇒ Object
-
#initialize(connection, config) ⇒ PatronHandler
constructor
A new instance of PatronHandler.
- #query(method, query, headers = {}) ⇒ Object
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
#session ⇒ Object
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, = {}) query(:get, model.table_name, ).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 |