Skip to content

Instantly share code, notes, and snippets.

@mostlyobvious
Created October 19, 2010 22:14
Show Gist options
  • Save mostlyobvious/635249 to your computer and use it in GitHub Desktop.
Save mostlyobvious/635249 to your computer and use it in GitHub Desktop.
DM adapter for most basic query in MKLiveStatus broker
require 'socket'
require 'dm-core'
require 'fastercsv'
WRITE_SHUTDOWN = 1
module DataMapper
module Adapters
class LqlAdapter < AbstractAdapter
def read(query)
lql_query = "GET #{query.model.storage_name}"
response = socket do |s|
s.puts(lql_query)
s.shutdown(WRITE_SHUTDOWN)
s.read
end
resources = []
FasterCSV.parse(response, :col_sep => ';',:headers => true, :converters => [:numeric]) do |row|
resources << Hash[row.headers.zip(row.fields)]
end
return resources
end
private
def initialize(name, options = {})
super
@socket_path = options[:path]
end
def socket(&block)
socket = UNIXSocket.new(@socket_path)
response = yield socket
socket.close
return response
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment