class Envoy

Main logging handler class Others will be derived from it

Public Class Methods

new(params = {}) click to toggle source

@param params Hash @raise ArgumentError When token is nil

# File lib/util/util.rb, line 56
def initialize(params = {})
  @debug = params.fetch(:debug, false)
  @token = params.fetch(:token, nil)
  raise ArgumentError.new 'Token can not be nil or empty' if @token.nil? || @token.to_s.empty?
  @base_url = params.fetch(:base_url, "https://api.botanalytics.co/v1/")
  @callback = params.fetch(:callback, nil)
end

Protected Instance Methods

fails(err, reason, payload = nil) click to toggle source

@param err Exception @param reason String @param payload Hash

# File lib/util/util.rb, line 74
def fails(err, reason, payload = nil)
    if @callback.nil?
        puts "[Botanalytics Error]: #{reason['error_message'] || reason  unless reason.nil?}, #{err.message unless err.nil?}...\n#{payload unless payload.nil?}"
    else
        send(@callback, err, reason, payload)
    end
end
informs(message) click to toggle source

@param message Object

# File lib/util/util.rb, line 65
def informs(message)
  if @debug
    puts "[Botanalytics Debug]: #{message}"
  end
end
submits(parchment, destination = 'messages/generic/', word = nil) click to toggle source

@param parchment Hash (required) @param destination String @param word String

# File lib/util/util.rb, line 85
def submits(parchment, destination = 'messages/generic/', word = nil)
    # Uri
    uri = URI.parse(@base_url+destination)
    # Header
    header = {'Content-Type':'application/json', 'Authorization': 'Token '+@token}
    begin
        # Send request
        response = Net::HTTP.post(uri, parchment.to_json, header)
        if response.code == "200" or response.code == "201"
            if word.nil?
                informs("Successfully logged message(s)...")
            else
                informs(word)
            end
            return true
        else
            fails(Exception.new('Message(s) can not be logged! StatusCode:'+response.code), JSON.parse(response.body), parchment)
            return false
        end
    rescue Exception => e
        fails(e, 'Error during http request', parchment)
        return false
    end
end