class SimpleSegment::Request
Constants
- DEFAULT_HEADERS
Attributes
error_handler[R]
host[R]
http_options[R]
logger[R]
stub[R]
write_key[R]
Public Class Methods
new(client)
click to toggle source
# File lib/simple_segment/request.rb, line 12 def initialize(client) @write_key = client.config.write_key @error_handler = client.config.on_error @stub = client.config.stub @logger = client.config.logger @http_options = client.config.http_options @host = client.config.host end
Public Instance Methods
post(path, payload, headers: DEFAULT_HEADERS)
click to toggle source
# File lib/simple_segment/request.rb, line 21 def post(path, payload, headers: DEFAULT_HEADERS) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength response = nil status_code = nil response_body = nil uri = URI("https://#{host}#{path}") payload = JSON.generate(payload) if stub logger.debug "stubbed request to \ #{uri.path}: write key = #{write_key}, \ payload = #{payload}" { status: 200, error: nil } else Net::HTTP.start(uri.host, uri.port, :ENV, http_options) do |http| request = Net::HTTP::Post.new(uri.path, headers) request.basic_auth write_key, nil http.request(request, payload).tap do |res| status_code = res.code response_body = res.body response = res response.value end end end rescue StandardError => e error_handler.call(status_code, response_body, e, response) end