lib/consul/client/base.rb in consul-ruby-client-0.0.4 vs lib/consul/client/base.rb in consul-ruby-client-0.0.5

- old
+ new

@@ -48,11 +48,18 @@ opts = {} opts[:params] = params unless params.nil? opts[:accept] = :json if json_only begin - return RestClient.get url, opts + resp = RestClient.get url, opts + success = (resp.code == 200 or resp.code == 201) + if success + logger.debug("Successful GET at endpoint #{url}") + else + logger.warn("Unable to GET from endpoint #{url} returned code: #{resp.code}") + end + return resp rescue Exception => e # Unable to communicate with consul agent. logger.warn(e.message) raise IOError.new "Unable to complete get request: #{e}" end @@ -69,19 +76,25 @@ # IOError: Unable to reach Consul Agent. def _put(url, value, params = nil) # Validation validate_url(url) - p = {} - p[:params] = params unless params.nil? + # If possible, Convert value to json + unless Consul::Utils.valid_json?(value) + value = value.to_json if value.respond_to?(:to_json) + end + + opts = {} + opts[:params] = params unless params.nil? begin - if Consul::Utils.valid_json?(value) - resp = RestClient.put(url, value, :content_type => :json) {|response, req, res| response } + opts[:content_type] = :json if Consul::Utils.valid_json?(value) + resp = RestClient.put(url, value, opts) {|response, req, res| response } + success = (resp.code == 200 or resp.code == 201) + if success + logger.debug("Successful PUT #{value} at endpoint #{url}") else - resp = RestClient.put(url, value) {|response, req, res| response } + logger.warn("Unable to PUT #{value} at endpoint #{url} returned code: #{resp.code}") end - success = (resp.code == 200 or resp.code == 201) - logger.warn("Unable to send #{value} to endpoint #{url} returned code: #{resp.code}") unless success return success, resp.body rescue Exception => e logger.error('RestClient.put Error: Unable to reach consul agent') raise IOError.new "Unable to complete put request: #{e}" end