Sha256: ff5e28cd18a83b444395b55033c46e920b3f5eb9d36b1a83690ac2a398f31511

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "json"
require "faraday"

module BrazeRuby
  class HTTP
    DEFAULT_TIMEOUT = 30

    def initialize(api_key, braze_url, options = {})
      @api_key = api_key
      @braze_url = braze_url
      @options = default_options.merge(options)
    end

    def post(path, payload)
      connection.post path do |request|
        request.body = JSON.dump(payload)
      end
    end

    def get(path, query = {})
      connection.get path, query
    end

    def connection
      @connection ||= Faraday.new(url: @braze_url) do |connection|
        connection.headers["Content-Type"] = "application/json"
        connection.headers["Accept"] = "application/json"
        connection.headers["User-Agent"] = "Braze Ruby gem v#{BrazeRuby::VERSION}"
        connection.headers["Authorization"] = "Bearer #{@api_key}"

        connection.response :logger if ENV["BRAZE_RUBY_DEBUG"]

        connection.adapter Faraday.default_adapter

        connection.options[:timeout] = @options[:timeout]
      end
    end

    private

    def default_options
      {timeout: DEFAULT_TIMEOUT}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
braze_ruby-0.5.0 lib/braze_ruby/http.rb