Class: NewRelic::Binding::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(context)
  @url = Config.endpoint + Config.uri
  @license_key = context.license_key
end

Instance Attribute Details

#license_keyObject (readonly)

Returns the value of attribute license_key.



9
10
11
# File 'lib/newrelic_platform_binding/connection.rb', line 9

def license_key
  @license_key
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/newrelic_platform_binding/connection.rb', line 9

def url
  @url
end

Instance Method Details

#send_request(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/newrelic_platform_binding/connection.rb', line 16

def send_request(data)
  begin
    PlatformLogger.debug("JSON payload: #{data}")
    uri = URI.parse(url)
    if Config.proxy.nil?
      http = Net::HTTP.new(uri.host, uri.port)
    else
      proxy = Config.proxy
      http = Net::HTTP.new(uri.host, uri.port, proxy['address'], proxy['port'], proxy['user'], proxy['password'])
    end
    if Config.use_ssl?
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE if Config.skip_ssl_host_verification?
    end
    http.open_timeout = 20
    http.read_timeout = 20
    request = Net::HTTP::Post.new(uri.path)
    request['content-type'] = 'application/json'
    request['X-License-Key'] = @license_key
    request.body = data
    response = http.request(request)
    return evaluate_response(response)
  rescue Timeout::Error => err
    PlatformLogger.warn "Connection Timeout Error: #{err.inspect} #{err.message}"
    return false
  rescue => err
    PlatformLogger.warn "HTTP Connection Error: #{err.inspect} #{err.message}"
    return false
  end
end