Module: Logcamp
- Defined in:
- lib/logcamp.rb,
lib/logcamp/event.rb,
lib/logcamp/version.rb,
lib/logcamp/api_resource.rb,
lib/logcamp/logcamp_object.rb,
lib/logcamp/errors/api_error.rb,
lib/logcamp/errors/logcamp_error.rb,
lib/logcamp/api_operations/create.rb,
lib/logcamp/errors/api_connection_error.rb,
lib/logcamp/errors/authentication_error.rb,
lib/logcamp/errors/invalid_request_error.rb,
lib/logcamp/errors/invalid_metadata_error.rb
Defined Under Namespace
Modules: APIOperations Classes: APIConnectionError, APIError, APIResource, AuthenticationError, Event, InvalidMetadataError, InvalidRequestError, LogcampError, LogcampObject
Constant Summary collapse
- VERSION =
"0.0.2"
Class Attribute Summary collapse
-
.api_base ⇒ Object
Returns the value of attribute api_base.
-
.api_version ⇒ Object
Returns the value of attribute api_version.
-
.token ⇒ Object
Returns the value of attribute token.
-
.verify_ssl_certs ⇒ Object
Returns the value of attribute verify_ssl_certs.
Class Method Summary collapse
- .api_error(error, rcode, rbody, error_obj) ⇒ Object
- .api_url(url = "") ⇒ Object
- .authentication_error(error, rcode, rbody, error_obj) ⇒ Object
- .general_api_error(rcode, rbody) ⇒ Object
- .handle_api_error(rcode, rbody) ⇒ Object
- .handle_connection_error(e) ⇒ Object
- .invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
- .request(method, url, token, params = {}, headers = {}) ⇒ Object
Class Attribute Details
.api_base ⇒ Object
Returns the value of attribute api_base.
32 33 34 |
# File 'lib/logcamp.rb', line 32 def api_base @api_base end |
.api_version ⇒ Object
Returns the value of attribute api_version.
32 33 34 |
# File 'lib/logcamp.rb', line 32 def api_version @api_version end |
.token ⇒ Object
Returns the value of attribute token.
32 33 34 |
# File 'lib/logcamp.rb', line 32 def token @token end |
.verify_ssl_certs ⇒ Object
Returns the value of attribute verify_ssl_certs.
32 33 34 |
# File 'lib/logcamp.rb', line 32 def verify_ssl_certs @verify_ssl_certs end |
Class Method Details
.api_error(error, rcode, rbody, error_obj) ⇒ Object
137 138 139 |
# 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 = "") ⇒ Object
35 36 37 |
# File 'lib/logcamp.rb', line 35 def self.api_url(url="") @api_base + url end |
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
133 134 135 |
# 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) ⇒ Object
141 142 143 144 |
# 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) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# 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) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/logcamp.rb', line 92 def self.handle_connection_error(e) case e when SocketError = "Unexpected error when trying to connect to Logcamp." when NoMethodError = "Unexpected HTTP response code" when InvalidMetadataError = "Invalid metadata. Only key value pair is supported" else = "Unexpected error communicating with Logcamp." end raise APIConnectionError.new( + "\n\n(Network error: #{e.})") end |
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
128 129 130 131 |
# 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 = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# 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 |