Class: LogBrew::HttpTransport

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

Constant Summary collapse

DEFAULT_ENDPOINT =
"https://api.logbrew.com/v1/events"
DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: DEFAULT_ENDPOINT, headers: {}, timeout: DEFAULT_TIMEOUT, http_client: nil) ⇒ HttpTransport

Returns a new instance of HttpTransport.



84
85
86
87
88
89
# File 'lib/logbrew.rb', line 84

def initialize(endpoint: DEFAULT_ENDPOINT, headers: {}, timeout: DEFAULT_TIMEOUT, http_client: nil)
  @endpoint = validate_endpoint(endpoint)
  @headers = copy_headers(headers)
  @timeout = validate_timeout(timeout)
  @http_client = http_client
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



82
83
84
# File 'lib/logbrew.rb', line 82

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



82
83
84
# File 'lib/logbrew.rb', line 82

def headers
  @headers
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



82
83
84
# File 'lib/logbrew.rb', line 82

def http_client
  @http_client
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



82
83
84
# File 'lib/logbrew.rb', line 82

def timeout
  @timeout
end

Instance Method Details

#send(api_key, body) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/logbrew.rb', line 91

def send(api_key, body)
  Validation.require_non_empty("api_key", api_key)
  raise SdkError.new("validation_error", "body must be non-empty") if body.nil?

  request = Net::HTTP::Post.new(request_path)
  request["authorization"] = "Bearer #{api_key}"
  request["content-type"] = "application/json"
  @headers.each { |name, value| request[name] = value }
  request.body = body

  response = @http_client ? @http_client.request(request) : request_with_default_client(request)
  TransportResponse.new(response.code.to_i, 1)
rescue TransportError
  raise
rescue IOError, SystemCallError, SocketError, Timeout::Error, EOFError, Net::OpenTimeout, Net::ReadTimeout => error
  raise TransportError.network("http transport failed: #{error.message}")
end