Class: GELF::Transport::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/gelf/transport/http.rb

Direct Known Subclasses

HTTPS

Instance Method Summary collapse

Constructor Details

#initialize(host, port: 80, path: '', headers: nil) ⇒ HTTP

Returns a new instance of HTTP.



4
5
6
7
8
# File 'lib/gelf/transport/http.rb', line 4

def initialize(host, port: 80, path: '', headers: nil)
  @uri = URI::HTTP.build host: host, port: port
  @headers = headers
  @path = path
end

Instance Method Details

#default_headersObject



29
30
31
32
33
34
# File 'lib/gelf/transport/http.rb', line 29

def default_headers
  {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json'
  }
end

#start_connectionObject



10
11
12
13
14
15
16
# File 'lib/gelf/transport/http.rb', line 10

def start_connection
  @uri.path = "/#{@path.to_s.delete_prefix('/')}"
  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = true if @uri.instance_of? URI::HTTPS
  given_headers = @headers || {}
  @headers = default_headers.merge(given_headers)
end

#transfer(message) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/gelf/transport/http.rb', line 18

def transfer(message)
  start_connection
  request = Net::HTTP::Post.new(@uri.request_uri)
  request.body = json_dump(message)
  @headers.each do |key, value|
    # pass header only if value present -> allows clearing default headers
    request[key] = value if value
  end
  @http.request(request)
end