class RudderAnalyticsSync::Request

Constants

BASE_URL
DEFAULT_HEADERS

Attributes

data_plane_url[R]
error_handler[R]
http_options[R]
logger[R]
stub[R]
write_key[R]

Public Class Methods

new(client) click to toggle source
# File lib/rudder_analytics_sync/request.rb, line 13
def initialize(client)
  @write_key = client.config.write_key
  @data_plane_url = client.config.data_plane_url || BASE_URL
  @error_handler = client.config.on_error
  @stub = client.config.stub
  @logger = client.config.logger
  @http_options = client.config.http_options
end

Public Instance Methods

post(path, payload, headers: DEFAULT_HEADERS) click to toggle source
# File lib/rudder_analytics_sync/request.rb, line 22
def post(path, payload, headers: DEFAULT_HEADERS) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  response = nil
  status_code = nil
  response_body = nil

  uri = URI(data_plane_url)
  payload = JSON.generate(payload)
  if stub
    logger.debug "stubbed request to \
    #{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(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