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.



100
101
102
103
104
105
# File 'lib/logbrew.rb', line 100

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.



98
99
100
# File 'lib/logbrew.rb', line 98

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



98
99
100
# File 'lib/logbrew.rb', line 98

def headers
  @headers
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



98
99
100
# File 'lib/logbrew.rb', line 98

def http_client
  @http_client
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



98
99
100
# File 'lib/logbrew.rb', line 98

def timeout
  @timeout
end

Instance Method Details

#send(api_key, body) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/logbrew.rb', line 107

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