class Ragios::Client

Attributes

address[R]
password[R]
port[R]
username[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/ragios-client.rb, line 15
def initialize(args = {})
  @address = args.fetch(:address, 'http://127.0.0.1')
  @port = args.fetch(:port, '5041')
  @username = args.fetch(:username, nil)
  @password = args.fetch(:password, nil)
end

Public Instance Methods

all(limit = nil) click to toggle source
# File lib/ragios-client.rb, line 34
def all(limit = nil)
  params = limit ? "?limit=#{limit}" : ""
  api_request { RestClient.get "#{address_port}/monitors#{params}", auth_cookie }
end
all_events(limit = nil) click to toggle source
# File lib/ragios-client.rb, line 56
def all_events(limit = nil)
  params = limit ? "?limit=#{limit}" : ""
  api_request { RestClient.get "#{address_port}/events#{params}", auth_cookie }
end
create(monitor) click to toggle source
# File lib/ragios-client.rb, line 28
def create(monitor)
  api_request { RestClient.post "#{address_port}/monitors/", generate_json(monitor), http_request_options }
end
delete(monitor_id) click to toggle source
# File lib/ragios-client.rb, line 44
def delete(monitor_id)
  api_request { RestClient.delete "#{address_port}/monitors/#{monitor_id}", auth_cookie }
end
delete_event(event_id) click to toggle source
# File lib/ragios-client.rb, line 60
def delete_event(event_id)
  api_request { RestClient.delete "#{address_port}/events/#{event_id}", auth_cookie }
end
events(monitor_id, startdate, enddate, limit=nil) click to toggle source
# File lib/ragios-client.rb, line 63
def events(monitor_id, startdate, enddate, limit=nil)
  api_request { RestClient.get "#{address_port}/monitors/#{monitor_id}/events", options(startdate, enddate, limit) }
end
events_by_state(monitor_id, state, startdate, enddate, limit=nil) click to toggle source
# File lib/ragios-client.rb, line 69
def events_by_state(monitor_id, state, startdate, enddate, limit=nil)
  api_request { RestClient.get "#{address_port}/monitors/#{monitor_id}/events_by_state/#{state}", options(startdate, enddate, limit) }
end
events_by_type(monitor_id, type, startdate, enddate, limit=nil) click to toggle source
# File lib/ragios-client.rb, line 66
def events_by_type(monitor_id, type, startdate, enddate, limit=nil)
  api_request { RestClient.get "#{address_port}/monitors/#{monitor_id}/events_by_type/#{type}", options(startdate, enddate, limit) }
end
find(monitor_id) click to toggle source
# File lib/ragios-client.rb, line 31
def find(monitor_id)
  api_request { RestClient.get "#{address_port}/monitors/#{monitor_id}/", auth_cookie }
end
find_event(event_id) click to toggle source
# File lib/ragios-client.rb, line 53
def find_event(event_id)
  api_request { RestClient.get "#{address_port}/events/#{event_id}/", auth_cookie }
end
login(username,password) click to toggle source
# File lib/ragios-client.rb, line 22
def login(username,password)
  @username = username
  @password = password
  auth_session
end
maestro_test(url, source_code) click to toggle source
# File lib/ragios-client.rb, line 76
def maestro_test(url, source_code)
  api_request { RestClient.post "#{address_port}/maestro/test", {url: url, source_code: source_code}, http_request_options }
end
start(monitor_id) click to toggle source
# File lib/ragios-client.rb, line 41
def start(monitor_id)
  api_request { RestClient.put "#{address_port}/monitors/#{monitor_id}",{:status => "active"},http_request_options }
end
stop(monitor_id) click to toggle source
# File lib/ragios-client.rb, line 38
def stop(monitor_id)
  api_request { RestClient.put "#{address_port}/monitors/#{monitor_id}",{:status => "stopped"}, http_request_options }
end
test(monitor_id) click to toggle source
# File lib/ragios-client.rb, line 72
def test(monitor_id)
  api_request { RestClient.post "#{address_port}/tests", {:id => monitor_id}, http_request_options }
end
update(monitor_id, options) click to toggle source
# File lib/ragios-client.rb, line 50
def update(monitor_id, options)
  api_request { RestClient.put "#{address_port}/monitors/#{monitor_id}",generate_json(options), http_request_options }
end
where(options) click to toggle source
# File lib/ragios-client.rb, line 47
def where(options)
  api_request { RestClient.get "#{address_port}/monitors/attributes?#{URI.encode_www_form(options)}", auth_cookie }
end

Private Instance Methods

address_port() click to toggle source
# File lib/ragios-client.rb, line 111
def address_port
  "#{@address}:#{@port}"
end
api_request() { || ... } click to toggle source
# File lib/ragios-client.rb, line 90
def api_request
  response = yield
  parse_json(response)
rescue => e
  raise_error(e)
end
auth_session() click to toggle source
# File lib/ragios-client.rb, line 123
def auth_session
  return "" if [@username, @password].any? { |e| e.nil? }
  auth = RestClient.post "#{address_port}/session", { :username=> @username, :password => @password}
  hash = parse_json(auth)
  hash[:RagiosAuthSession]
rescue => e
  raise_error(e)
end
generate_json(str) click to toggle source
# File lib/ragios-client.rb, line 115
def generate_json(str)
  MultiJson.dump(str)
end
http_request_options() click to toggle source
# File lib/ragios-client.rb, line 105
def http_request_options
  {:content_type => :json,
   :cookies => {:RagiosAuthSession => auth_session}
  }
end
options(startdate, enddate, limit = nil) click to toggle source
# File lib/ragios-client.rb, line 81
def options(startdate, enddate, limit = nil)
  params = {}
  params[:start_date] = startdate
  params[:end_date] = enddate
  params[:limit] = limit if limit
  query_options = auth_cookie
  query_options[:params] = params
  query_options
end
parse_json(str) click to toggle source
# File lib/ragios-client.rb, line 119
def parse_json(str)
  MultiJson.load(str, symbolize_names: true)
end
raise_error(e) click to toggle source
# File lib/ragios-client.rb, line 97
def raise_error(e)
  e.respond_to?('response') ? raise(ClientException, e.response) : raise(e)
end