class Bright::Connection
Constants
- OPEN_TIMEOUT
- READ_TIMEOUT
- VERIFY_PEER
Attributes
endpoint[RW]
ignore_http_status[RW]
logger[RW]
open_timeout[RW]
pem[RW]
pem_password[RW]
proxy_address[RW]
proxy_port[RW]
read_timeout[RW]
ssl_version[RW]
tag[RW]
verify_peer[RW]
Public Class Methods
new(endpoint)
click to toggle source
# File lib/bright/connection.rb, line 25 def initialize(endpoint) @endpoint = endpoint.is_a?(URI) ? endpoint : URI.parse(endpoint) @open_timeout = OPEN_TIMEOUT @read_timeout = READ_TIMEOUT @verify_peer = VERIFY_PEER @ignore_http_status = false @ssl_version = nil @proxy_address = nil @proxy_port = nil end
Public Instance Methods
request(method, body, headers = {})
click to toggle source
# File lib/bright/connection.rb, line 36 def request(method, body, headers = {}) request_start = Time.now.to_f begin info "connection_http_method=#{method.to_s.upcase} connection_uri=#{endpoint}", tag result = nil realtime = Benchmark.realtime do request = HTTPI::Request.new(endpoint.to_s) request.headers = headers request.body = body if body request.auth.ssl.verify_mode = :none if !@verify_peer configure_proxy(request) configure_timeouts(request) result = case method when :get raise ArgumentError, "GET requests do not support a request body" if body HTTPI.get(request) when :post debug body HTTPI.post(request) when :put debug body HTTPI.put(request) when :patch debug body HTTPI.patch(request) when :delete HTTPI.delete(request) else raise ArgumentError, "Unsupported request method #{method.to_s.upcase}" end end info "--> %d (%d %.4fs)" % [result.code, result.body ? result.body.length : 0, realtime], tag debug result.body result end ensure info "connection_request_total_time=%.4fs" % [Time.now.to_f - request_start], tag end
Private Instance Methods
configure_proxy(http)
click to toggle source
# File lib/bright/connection.rb, line 81 def configure_proxy(http) http.proxy = "#{proxy_address}:#{proxy_port}" if proxy_address end
configure_timeouts(http)
click to toggle source
# File lib/bright/connection.rb, line 85 def configure_timeouts(http) http.open_timeout = open_timeout http.read_timeout = read_timeout end
debug(message, tag = nil)
click to toggle source
# File lib/bright/connection.rb, line 103 def debug(message, tag = nil) log(:debug, message, tag) end
error(message, tag = nil)
click to toggle source
# File lib/bright/connection.rb, line 111 def error(message, tag = nil) log(:error, message, tag) end
handle_response(response)
click to toggle source
# File lib/bright/connection.rb, line 90 def handle_response(response) if @ignore_http_status then return response.body else case response.code.to_i when 200...300 response.body else raise ResponseError.new(response) end end end
info(message, tag = nil)
click to toggle source
# File lib/bright/connection.rb, line 107 def info(message, tag = nil) log(:info, message, tag) end
log(level, message, tag)
click to toggle source
# File lib/bright/connection.rb, line 115 def log(level, message, tag) message = "[#{tag}] #{message}" if tag logger.send(level, message) if logger end