lib/ragios-client.rb in ragios-client-0.0.3 vs lib/ragios-client.rb in ragios-client-0.0.4
- old
+ new
@@ -1,7 +1,7 @@
require 'rest_client'
-require 'yajl'
+require 'json/pure'
require 'uri'
module Ragios
class ClientException < StandardError; end
@@ -24,70 +24,46 @@
@password = password
auth_session
end
def add(monitors)
- response = RestClient.post "#{address_port}/monitors/", json(monitors), http_request_options
- parse_json(response.body)
- rescue => e
- raise_error(e)
+ api_request { RestClient.post "#{address_port}/monitors/", generate_json(monitors), http_request_options }
end
-
def find(monitor_id)
- response = RestClient.get "#{address_port}/monitors/#{monitor_id}/", auth_cookie
- parse_json(response.body)
- rescue => e
- raise_error(e)
+ api_request { RestClient.get "#{address_port}/monitors/#{monitor_id}/", auth_cookie }
end
-
def all
- response = RestClient.get "#{address_port}/monitors/", auth_cookie
- parse_json(response.body)
+ api_request { RestClient.get "#{address_port}/monitors/", auth_cookie }
end
-
def stop(monitor_id)
- response = RestClient.put "#{address_port}/monitors/#{monitor_id}",{:status => "stopped"}, http_request_options
- parse_json(response)
- rescue => e
- raise_error(e)
+ api_request { RestClient.put "#{address_port}/monitors/#{monitor_id}",{:status => "stopped"}, http_request_options }
end
-
def restart(monitor_id)
- response = RestClient.put "#{address_port}/monitors/#{monitor_id}",{:status => "active"},http_request_options
- parse_json(response)
- rescue => e
- raise_error(e)
+ api_request { RestClient.put "#{address_port}/monitors/#{monitor_id}",{:status => "active"},http_request_options }
end
-
def delete(monitor_id)
- response = RestClient.delete "#{address_port}/monitors/#{monitor_id}", auth_cookie
- parse_json(response)
- rescue => e
- raise_error(e)
+ api_request { RestClient.delete "#{address_port}/monitors/#{monitor_id}", auth_cookie }
end
-
def where(options)
- response = RestClient.get "#{address_port}/monitors?#{URI.encode_www_form(options)}", auth_cookie
- parse_json(response)
+ api_request { RestClient.get "#{address_port}/monitors?#{URI.encode_www_form(options)}", auth_cookie }
end
-
def update(monitor_id, options)
- response = RestClient.put "#{address_port}/monitors/#{monitor_id}",json(options), http_request_options
- parse_json(response)
- rescue => e
- raise_error(e)
+ api_request { RestClient.put "#{address_port}/monitors/#{monitor_id}",generate_json(options), http_request_options }
end
def test(monitor_id)
- response = RestClient.post "#{address_port}/tests", {:id => monitor_id}, http_request_options
+ api_request { RestClient.post "#{address_port}/tests", {:id => monitor_id}, http_request_options }
+ end
+
+private
+ def api_request
+ response = yield
parse_json(response)
rescue => e
raise_error(e)
end
-private
-
def raise_error(e)
e.respond_to?('response') ? raise(ClientException, e.response) : raise(e)
end
def auth_cookie
@@ -102,22 +78,21 @@
def address_port
"#{@address}:#{@port}"
end
- def json(str)
- Yajl::Encoder.encode(str)
+ def generate_json(str)
+ JSON.generate(str)
end
def parse_json(str)
- Yajl::Parser.parse(str, :symbolize_keys => true)
+ JSON.parse(str, symbolize_names: true)
end
def auth_session
- #deal with performance when it becomes a problem not yet a priority
auth = RestClient.post "#{address_port}/session", { :username=> @username, :password => @password}
- hash = Yajl::Parser.parse(auth.to_str)
- hash['AuthSession']
+ hash = parse_json(auth)
+ hash[:AuthSession]
rescue => e
raise_error(e)
end
end
end