class BrazeRuby::HTTP

Constants

DEFAULT_OPEN_TIMEOUT
DEFAULT_TIMEOUT

Public Class Methods

new(api_key, braze_url, options = {}) click to toggle source
# File lib/braze_ruby/http.rb, line 11
def initialize(api_key, braze_url, options = {})
  @api_key = api_key
  @braze_url = braze_url
  @options = default_options.merge(options)
end

Public Instance Methods

connection() click to toggle source
# File lib/braze_ruby/http.rb, line 27
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.request :retry, @options[:retry] if @options[:retry]

    connection.adapter Faraday.default_adapter

    connection.options[:timeout] = @options[:timeout]
    connection.options[:open_timeout] = @options[:open_timeout]
  end
end
get(path, query = {}) click to toggle source
# File lib/braze_ruby/http.rb, line 23
def get(path, query = {})
  connection.get path, query
end
post(path, payload) click to toggle source
# File lib/braze_ruby/http.rb, line 17
def post(path, payload)
  connection.post path do |request|
    request.body = JSON.dump(payload)
  end
end

Private Instance Methods

default_options() click to toggle source
# File lib/braze_ruby/http.rb, line 46
def default_options
  {timeout: DEFAULT_TIMEOUT, open_timeout: DEFAULT_OPEN_TIMEOUT}
end