class Papertrail::HttpClient
Constants
- ESCAPE_RE
Public Class Methods
new(ssl)
click to toggle source
# File lib/papertrail/http_client.rb, line 35 def initialize(ssl) @ssl = ssl @headers = {} end
Public Instance Methods
basic_auth(login, pass)
click to toggle source
# File lib/papertrail/http_client.rb, line 53 def basic_auth(login, pass) @headers['Authorization'] = 'Basic ' + ["#{login}:#{pass}"].pack('m').delete("\r\n") end
delete(path)
click to toggle source
# File lib/papertrail/http_client.rb, line 73 def delete(path) request(:delete, path) end
finish()
click to toggle source
# File lib/papertrail/http_client.rb, line 49 def finish https.finish end
get(path, params = {})
click to toggle source
# File lib/papertrail/http_client.rb, line 61 def get(path, params = {}) request(:get, path, params) end
post(path, params)
click to toggle source
# File lib/papertrail/http_client.rb, line 69 def post(path, params) request(:post, path, params) end
put(path, params)
click to toggle source
# File lib/papertrail/http_client.rb, line 65 def put(path, params) request(:put, path, params) end
start() { |self| ... }
click to toggle source
# File lib/papertrail/http_client.rb, line 40 def start if block_given? https.start { yield self } else https.start self end end
token_auth(token)
click to toggle source
# File lib/papertrail/http_client.rb, line 57 def token_auth(token) @headers['X-Papertrail-Token'] = token end
Private Instance Methods
build_nested_query(value, prefix = nil)
click to toggle source
# File lib/papertrail/http_client.rb, line 147 def build_nested_query(value, prefix = nil) case value when Array value.map { |v| build_nested_query(v, "#{prefix}%5B%5D") }.join("&") when Hash value.map { |k, v| build_nested_query(v, prefix ? "#{prefix}%5B#{escape(k)}%5D" : escape(k)) }.join("&") when NilClass prefix else raise ArgumentError, "value must be a Hash" if prefix.nil? "#{prefix}=#{escape(value)}" end end
cli_version()
click to toggle source
# File lib/papertrail/http_client.rb, line 116 def cli_version { :cli_version => Papertrail::VERSION } end
escape(s)
click to toggle source
# File lib/papertrail/http_client.rb, line 163 def escape(s) s.to_s.gsub(ESCAPE_RE) { '%' + $&.unpack('H2' * $&.bytesize).join('%').upcase }.tr(' ', '+') end
https()
click to toggle source
# File lib/papertrail/http_client.rb, line 99 def https @https ||= Net::HTTP.new('papertrailapp.com', 443).tap do |http| http.use_ssl = true http.verify_mode = ssl_verify_mode http.cert_store = ssl_cert_store http.cert = @ssl[:client_cert] if @ssl[:client_cert] http.key = @ssl[:client_key] if @ssl[:client_key] http.ca_file = @ssl[:ca_file] if @ssl[:ca_file] http.ca_path = @ssl[:ca_path] if @ssl[:ca_path] http.verify_depth = @ssl[:verify_depth] if @ssl[:verify_depth] http.ssl_version = @ssl[:version] if @ssl[:version] http.keep_alive_timeout = 10 if http.respond_to?(:keep_alive_timeout=) end end
on_complete(response)
click to toggle source
# File lib/papertrail/http_client.rb, line 138 def on_complete(response) case response when Net::HTTPSuccess Papertrail::HttpResponse.new(response) else response.error! end end
request(http_method, path, params = {})
click to toggle source
# File lib/papertrail/http_client.rb, line 79 def request(http_method, path, params = {}) attempts = 0 uri = "#{request_uri(path)}?cli_version=#{Papertrail::VERSION}&" begin if http_method == :get || http_method == :delete on_complete(https.send(http_method, uri + build_nested_query(params.merge(cli_version)), @headers)) else on_complete(https.send(http_method, uri, build_nested_query(params), @headers)) end rescue IOError, SystemCallError, Net::HTTPFatalError => e attempts += 1 retry if (attempts < 3) raise e end end
request_uri(path)
click to toggle source
# File lib/papertrail/http_client.rb, line 95 def request_uri(path) path.start_with?('/api/v1/') ? path : "/api/v1/#{path}" end
ssl_cert_store()
click to toggle source
# File lib/papertrail/http_client.rb, line 130 def ssl_cert_store return @ssl[:cert_store] if @ssl[:cert_store] # Use the default cert store by default, i.e. system ca certs cert_store = OpenSSL::X509::Store.new cert_store.set_default_paths cert_store end
ssl_verify_mode()
click to toggle source
# File lib/papertrail/http_client.rb, line 120 def ssl_verify_mode @ssl[:verify_mode] || begin if @ssl.fetch(:verify, true) OpenSSL::SSL::VERIFY_PEER else OpenSSL::SSL::VERIFY_NONE end end end