class CritiMonLib::CritiMon

Public Class Methods

api_key() click to toggle source
# File lib/critimonlib-ruby.rb, line 60
def self.api_key
  @@api_key
end
api_key=(value) click to toggle source
# File lib/critimonlib-ruby.rb, line 64
def self.api_key=(value)
  @@api_key = value
end
app_id() click to toggle source
# File lib/critimonlib-ruby.rb, line 68
def self.app_id
  @@app_id
end
app_id=(value) click to toggle source
# File lib/critimonlib-ruby.rb, line 72
def self.app_id=(value)
  @@app_id = value
end
app_version() click to toggle source
# File lib/critimonlib-ruby.rb, line 76
def self.app_version
  @@app_version
end
app_version=(value) click to toggle source
# File lib/critimonlib-ruby.rb, line 80
def self.app_version=(value)
  @@app_version = value
end
critimon_initialised() click to toggle source
# File lib/critimonlib-ruby.rb, line 28
def self.critimon_initialised
  @@critimon_initialised
end
critimon_initialised=(value) click to toggle source
# File lib/critimonlib-ruby.rb, line 32
def self.critimon_initialised=(value)
  @@critimon_initialised = value
end
critimon_url() click to toggle source
# File lib/critimonlib-ruby.rb, line 36
def self.critimon_url
  @@critimon_url
end
critimon_url=(value) click to toggle source
# File lib/critimonlib-ruby.rb, line 40
def self.critimon_url=(value)
  @@critimon_url = value
end
device_id() click to toggle source
# File lib/critimonlib-ruby.rb, line 84
def self.device_id
  @@device_id
end
device_id=(value) click to toggle source
# File lib/critimonlib-ruby.rb, line 88
def self.device_id=(value)
  @@device_id = value
end
do_lb() click to toggle source
# File lib/critimonlib-ruby.rb, line 52
def self.do_lb
  @@do_lb
end
do_lb=(value) click to toggle source
# File lib/critimonlib-ruby.rb, line 56
def self.do_lb=(value)
  @@do_lb = value
end
session_id() click to toggle source
# File lib/critimonlib-ruby.rb, line 44
def self.session_id
  @@session_id
end
session_id=(value) click to toggle source
# File lib/critimonlib-ruby.rb, line 48
def self.session_id=(value)
  @@session_id = value
end

Public Instance Methods

SendCrash(e, severity, customProperties = {}) click to toggle source
# File lib/critimonlib-ruby.rb, line 169
def SendCrash(e, severity, customProperties = {})
  exceptionDetails = processStacktrace(e.backtrace)

  postData = returnPostData(e, severity, exceptionDetails, customProperties)

  sendCrashRequest(postData)
end
initialise(api_key, app_id, app_version) click to toggle source
# File lib/critimonlib-ruby.rb, line 92
def initialise(api_key, app_id, app_version)

  self.class.api_key = api_key
  self.class.app_id = app_id
  self.class.app_version = app_version

  if File.file?('critimon.tmp')
    file = File.open('critimon.tmp')
    @@device_id = file.read
    file.close
  else
    file = File.open('critimon.tmp', 'w')
    o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
    @@device_id = (0...10).map { o[rand(o.length)] }.join
    file.write(@@device_id)
    file.close
  end

  post_url = self.class.critimon_url + 'initialise'

  uri = URI.parse(post_url)
  data = {
    ApplicationID: app_id,
    DeviceID: @@device_id,
    AppVersion: app_version
  }

  headers = {
    "authorisation-token": api_key
  }

  http = Net::HTTP.new(uri.host, uri.port)
  if uri.port == 443
    http.use_ssl = true
  end

  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.set_form_data(data)

  response = http.request(request)

  if response.code == '200'

    json = JSON.parse(response.body)

    if json['result'] == 0
      self.class.critimon_initialised = true
      cookies = response.header['set-cookie'].split(',')

      cookies.each do |item|

        cookie = item.split('=')

        if cookie[0].strip == 'SESSIONID'
          session_data = cookie[1].strip.split(';')
          self.class.session_id = session_data[0].strip
        elsif cookie[0].strip == 'DO-LB'
          dolb_data = cookie[1].strip.split(';')
          self.class.do_lb = dolb_data[0].strip
        end
      end

      # Process anything in the queue
      @@pending_crashes.each do |d|
        sendCrashRequest(d)
      end
      @@pending_crashes.clear
    elsif json['result'] == 5
      sleep(1)
      self.initialise(self.class.api_key, self.class.app_id, self.class.app_version)
    end
  else
    puts('Received HTTP Status Code: ' + response.code)
    puts(response.body)
  end
end

Private Instance Methods

processStacktrace(backtrace) click to toggle source
# File lib/critimonlib-ruby.rb, line 263
def processStacktrace(backtrace)
  # The first line contains the crash point
  line = backtrace[0]
  lineDetails = line.split(':')

  sectionCount = 0
  file = ''
  if lineDetails.count > 1
    # We're most likely on Windows so there are extra colons (:) so make sure the full path is still included
    file = lineDetails[0]
    file = file + ':' + lineDetails[1]
    sectionCount += 2
  end
  lineNo = lineDetails[sectionCount]
  sectionCount += 1

  # Create the full stack trace as a string
  stacktrace = ''
  backtrace.each do |line|
    stacktrace += line + "\n"
  end

  {
    file: file,
    lineNo: lineNo,
    stacktrace: stacktrace
  }
end
returnOSName() click to toggle source
# File lib/critimonlib-ruby.rb, line 249
def returnOSName
  if OS.windows?
    'Windows'
  elsif OS.linux?
    'Linux'
  elsif OS.mac?
    'Mac'
  else
    'Unknown'
  end
end
returnPostData(e, severity, exceptionDetails, customProperties = {}) click to toggle source
# File lib/critimonlib-ruby.rb, line 221
def returnPostData(e, severity, exceptionDetails, customProperties = {})
  postData = {}
  postData['ExceptionType'] = e.class.to_s
  postData['DeviceID'] = @@device_id
  postData['Stacktrace'] = exceptionDetails[:stacktrace]
  postData['Severity'] = severity
  postData['CrashType'] = 'Handled'
  postData['DeviceType'] = 'Ruby'
  postData['AppID'] = self.class.app_id
  postData['VersionName'] = @@app_version
  postData['ExceptionMessage'] = e.message
  postData['File'] = exceptionDetails[:file]
  postData['LineNo'] = exceptionDetails[:lineNo]
  postData['OSName'] = returnOSName
  postData['Architecture'] = 'x' + OS.bits.to_s
  postData['RubyVersion'] = RUBY_VERSION
  if customProperties != {}
    postData['CustomProperty'] = customProperties.to_json
  else
    postData['CustomProperty'] = ''
  end


  postData
end
sendCrashRequest(postData) click to toggle source
# File lib/critimonlib-ruby.rb, line 179
def sendCrashRequest(postData)
  headers = {
      "authorisation-token": self.class.api_key,
      "cookie": 'SESSIONID=' + self.class.session_id
  }

  if self.class.do_lb != ''
    headers['cookie'] = headers[:cookie] + '; DO-LB=' + self.class.do_lb
  end

  post_url = self.class.critimon_url + 'crash'

  uri = URI.parse(post_url)

  http = Net::HTTP.new(uri.host, uri.port)
  if uri.port == 443
    http.use_ssl = true
  end
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.set_form_data(postData)

  response = http.request(request)



  if response.code != '200'
    @@pending_crashes.push(postData)
    if @@critimon_initialised
      self.initialise(@@api_key, @@app_id, @@app_version)
    end
  else
    if response.body != ''
      json = JSON.parse(response.body)
      if json['result'] == 5
        self.initialise(@@api_key, @@app_id, @@app_version)
      end
    end
  end
end