module Logcamp
Constants
- VERSION
Attributes
api_base[RW]
api_version[RW]
token[RW]
verify_ssl_certs[RW]
Public Class Methods
api_error(error, rcode, rbody, error_obj)
click to toggle source
# File lib/logcamp.rb, line 137 def self.api_error(error, rcode, rbody, error_obj) APIError.new(error[:message], rcode, rbody, error_obj) end
api_url(url="")
click to toggle source
# File lib/logcamp.rb, line 35 def self.api_url(url="") @api_base + url end
authentication_error(error, rcode, rbody, error_obj)
click to toggle source
# File lib/logcamp.rb, line 133 def self.authentication_error(error, rcode, rbody, error_obj) AuthenticationError.new(error[:message], rcode, rbody, error_obj) end
general_api_error(rcode, rbody)
click to toggle source
# File lib/logcamp.rb, line 141 def self.general_api_error(rcode, rbody) APIError.new("Invalid response object from API: #{rbody.inspect} " + "(HTTP response code was #{rcode})", rcode, rbody) end
handle_api_error(rcode, rbody)
click to toggle source
# File lib/logcamp.rb, line 108 def self.handle_api_error(rcode, rbody) begin error_obj = JSON.parse(rbody) rescue JSON::ParserError raise general_api_error(rcode, rbody) end case rcode when 400, 404, 422 raise invalid_request_error error, rcode, rbody, error_obj when 401 raise authentication_error error, rcode, rbody, error_obj when 500 raise api_error error, rcode, rbody, error_obj else # raise api_error error, rcode, rbody, error_obj end end
handle_connection_error(e)
click to toggle source
# File lib/logcamp.rb, line 92 def self.handle_connection_error(e) case e when SocketError message = "Unexpected error when trying to connect to Logcamp." when NoMethodError message = "Unexpected HTTP response code" when InvalidMetadataError message = "Invalid metadata. Only key value pair is supported" else message = "Unexpected error communicating with Logcamp." end raise APIConnectionError.new(message + "\n\n(Network error: #{e.message})") end
invalid_request_error(error, rcode, rbody, error_obj)
click to toggle source
# File lib/logcamp.rb, line 128 def self.invalid_request_error(error, rcode, rbody, error_obj) InvalidRequestError.new(error[:message], error[:param], rcode, rbody, error_obj) end
request(method, url, token, params={}, headers={})
click to toggle source
# File lib/logcamp.rb, line 39 def self.request(method, url, token, params={}, headers={}) unless token ||= @token raise AuthenticationError.new("No token provided") end if !params[:metadata].nil? && params[:metadata].class != Hash raise InvalidMetadataError.new("Invalid metadata. Only key value pair is supported", params) else params[:metadata] = params[:metadata].to_json end url = api_url(url) begin uri = URI(url) request = Net::HTTP::Post.new(uri) if method == :post request["User-Agent"] = "Logcamp-ruby gem" request["Authorization"] = "Token token=\"#{token}\"" request["Content-Type"] = "application/json" request.body = params.to_json http = Net::HTTP.new(uri.hostname, uri.port) # see http://www.rubyinside.com/how-to-cure-nethttps-risky-default-https-behavior-4010.html # for info about ssl verification http.use_ssl = true if uri.scheme == "https" http.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme == "https" response = http.start {|h| h.request(request) } # since http.request doesn't throw such exceptions, check them by status codes handle_api_error(response.code, response.body) rescue SocketError => e handle_connection_error(e) rescue NoMethodError => e handle_connection_error(e) rescue OpenSSL::SSL::SSLError => e handle_connection_error(e) rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e handle_connection_error(e) rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, InvalidMetadataError => e handle_connection_error(e) end [response, token] end