Class: Bright::Connection

Inherits:
Object show all
Defined in:
lib/bright/connection.rb

Constant Summary collapse

OPEN_TIMEOUT =
60
READ_TIMEOUT =
60
VERIFY_PEER =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ Connection

Returns a new instance of Connection.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

  if Bright.devmode && !@logger
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::INFO
  end
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



12
13
14
# File 'lib/bright/connection.rb', line 12

def endpoint
  @endpoint
end

#ignore_http_statusObject

Returns the value of attribute ignore_http_status.



21
22
23
# File 'lib/bright/connection.rb', line 21

def ignore_http_status
  @ignore_http_status
end

#loggerObject

Returns the value of attribute logger.



19
20
21
# File 'lib/bright/connection.rb', line 19

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



13
14
15
# File 'lib/bright/connection.rb', line 13

def open_timeout
  @open_timeout
end

#pemObject

Returns the value of attribute pem.



17
18
19
# File 'lib/bright/connection.rb', line 17

def pem
  @pem
end

#pem_passwordObject

Returns the value of attribute pem_password.



18
19
20
# File 'lib/bright/connection.rb', line 18

def pem_password
  @pem_password
end

#proxy_addressObject

Returns the value of attribute proxy_address.



22
23
24
# File 'lib/bright/connection.rb', line 22

def proxy_address
  @proxy_address
end

#proxy_portObject

Returns the value of attribute proxy_port.



23
24
25
# File 'lib/bright/connection.rb', line 23

def proxy_port
  @proxy_port
end

#read_timeoutObject

Returns the value of attribute read_timeout.



14
15
16
# File 'lib/bright/connection.rb', line 14

def read_timeout
  @read_timeout
end

#ssl_versionObject

Returns the value of attribute ssl_version.



16
17
18
# File 'lib/bright/connection.rb', line 16

def ssl_version
  @ssl_version
end

#tagObject

Returns the value of attribute tag.



20
21
22
# File 'lib/bright/connection.rb', line 20

def tag
  @tag
end

#verify_peerObject

Returns the value of attribute verify_peer.



15
16
17
# File 'lib/bright/connection.rb', line 15

def verify_peer
  @verify_peer
end

Instance Method Details

#request(method, body, headers = {}) ⇒ Object



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
# File 'lib/bright/connection.rb', line 41

def request(method, body, headers = {})
  request_start = Time.now.to_f
  info "connection_http_method=#{method.to_s.upcase} connection_uri=#{endpoint} headers=#{headers.inspect}", tag

  result = nil

  if !Bright.devmode
    HTTPI.log = false
  end

  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) if Bright.devmode
      HTTPI.post(request)
    when :put
      debug(body) if Bright.devmode
      HTTPI.put(request)
    when :patch
      debug(body) if Bright.devmode
      HTTPI.patch(request)
    when :delete
      HTTPI.delete(request)
    else
      raise ArgumentError, "Unsupported request method #{method.to_s.upcase}"
    end
  end

  if Bright.devmode
    info("--> %d (%d %.4fs)" % [result.code, result.body ? result.body.length : 0, realtime], tag)
    debug(result.body)
  end

  handle_response(result)
ensure
  info "connection_request_total_time=%.4fs" % [Time.now.to_f - request_start], tag
end