Class: Sciosano::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/sciosano/report.rb

Overview

Report represents an error or message to be sent to sciosano

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Report

Returns a new instance of Report.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sciosano/report.rb', line 9

def initialize(attrs = {})
  @id = attrs[:id] || SecureRandom.uuid
  @report_type = attrs[:report_type] || "error"
  @source = attrs[:source] || "backend"
  @error = attrs[:error]
  @user_description = attrs[:user_description]
  @context = attrs[:context] || {}
  @environment = attrs[:environment] || {}
  @breadcrumbs = attrs[:breadcrumbs] || []
  @tags = attrs[:tags] || {}
  @timestamp = attrs[:timestamp] || Time.now.utc.iso8601(3)
end

Instance Attribute Details

Returns the value of attribute breadcrumbs.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def breadcrumbs
  @breadcrumbs
end

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def context
  @context
end

#environmentObject (readonly)

Returns the value of attribute environment.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def environment
  @environment
end

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def id
  @id
end

#report_typeObject (readonly)

Returns the value of attribute report_type.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def report_type
  @report_type
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def source
  @source
end

#tagsObject (readonly)

Returns the value of attribute tags.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def tags
  @tags
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def timestamp
  @timestamp
end

#user_descriptionObject (readonly)

Returns the value of attribute user_description.



6
7
8
# File 'lib/sciosano/report.rb', line 6

def user_description
  @user_description
end

Class Method Details

.build_from_exception(exception, context:, breadcrumbs:, configuration:) ⇒ Report

Build report from an exception

Parameters:

Returns:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sciosano/report.rb', line 29

def self.build_from_exception(exception, context:, breadcrumbs:, configuration:)
  new(
    report_type: "error",
    source: "backend",
    error: build_error_hash(exception),
    context: build_context_hash(context, configuration),
    environment: build_environment_hash(configuration),
    breadcrumbs: breadcrumbs.map(&:to_h),
    tags: context.tags
  )
end

.build_from_message(message, level:, context:, breadcrumbs:, configuration:) ⇒ Report

Build report from a message

Parameters:

Returns:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sciosano/report.rb', line 49

def self.build_from_message(message, level:, context:, breadcrumbs:, configuration:)
  new(
    report_type: "feedback",
    source: "backend",
    user_description: message,
    context: build_context_hash(context, configuration),
    environment: build_environment_hash(configuration),
    breadcrumbs: breadcrumbs.map(&:to_h),
    tags: context.tags.merge("level" => level.to_s)
  )
end

Instance Method Details

#to_hHash

Convert to hash

Returns:

  • (Hash)


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sciosano/report.rb', line 71

def to_h
  {
    reportType: report_type,
    source: source,
    error: error,
    userDescription: user_description,
    context: context,
    environment: environment,
    breadcrumbs: breadcrumbs,
    tags: tags,
    timestamp: timestamp
  }.compact
end

#to_json(*_args) ⇒ String

Convert to JSON for API

Returns:

  • (String)


64
65
66
# File 'lib/sciosano/report.rb', line 64

def to_json(*_args)
  JSON.generate(to_h)
end