-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Bugsnag::Breadcrumbs | ||
class Breadcrumb | ||
attr_accessor :message | ||
attr_accessor :type | ||
attr_accessor :meta_data | ||
attr_reader :auto | ||
attr_reader :timestamp | ||
|
||
## | ||
# Creates a breadcrumb. | ||
# | ||
# This will not have been validated, which must occur before this is | ||
# attached to a report. | ||
def initialize(message, type, meta_data, auto) | ||
@should_ignore = false | ||
self.message = message | ||
self.type = type | ||
self.meta_data = meta_data | ||
|
||
# Use the symbol comparison to improve readability of breadcrumb creation | ||
@auto = auto == :auto | ||
|
||
# Store it as a timestamp for now | ||
@timestamp = Time.now | ||
end | ||
|
||
## | ||
# Flags the breadcrumb to be ignored. | ||
# | ||
# Ignored breadcrumbs will not be attached to a report. | ||
def ignore! | ||
@should_ignore = true | ||
end | ||
|
||
## | ||
# Checks if the `ignore!` method has been called. | ||
# | ||
# Ignored breadcrumbs will not be attached to a report. | ||
def ignore? | ||
@should_ignore | ||
end | ||
|
||
## | ||
# Outputs the breadcrumb data in a formatted hash. | ||
# | ||
# These adhere to the breadcrumb format as defined in the Bugsnag error | ||
# reporting API | ||
def to_h | ||
{ | ||
:name => @message, | ||
:type => @type, | ||
:metaData => @meta_data, | ||
:timestamp => @timestamp.utc.strftime('%Y-%m-%dT%H:%M:%SZ') | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
require 'bugsnag/breadcrumbs/breadcrumb' | ||
|
||
RSpec.describe Bugsnag::Breadcrumbs::Breadcrumb do | ||
describe "#message" do | ||
it "is assigned in #initialize" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new("my message", nil, nil, nil) | ||
|
||
expect(breadcrumb.message).to eq("my message") | ||
end | ||
end | ||
|
||
describe "#type" do | ||
it "is assigned in #initialize" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new(nil, "test type", nil, nil) | ||
|
||
expect(breadcrumb.type).to eq("test type") | ||
end | ||
end | ||
|
||
describe "#meta_data" do | ||
it "is assigned in #initialize" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new(nil, nil, {:a => 1, :b => 2}, nil) | ||
|
||
expect(breadcrumb.meta_data).to eq({:a => 1, :b => 2}) | ||
end | ||
end | ||
|
||
describe "#auto" do | ||
it "defaults to false" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new(nil, nil, nil, nil) | ||
|
||
expect(breadcrumb.auto).to eq(false) | ||
end | ||
|
||
it "is true if auto argument == :auto" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new(nil, nil, nil, :auto) | ||
|
||
expect(breadcrumb.auto).to eq(true) | ||
end | ||
|
||
it "if false if auto argument is anything else" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new(nil, nil, nil, :manual) | ||
|
||
expect(breadcrumb.auto).to eq(false) | ||
end | ||
end | ||
|
||
describe "#timestamp" do | ||
it "is stored as a timestamp" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new(nil, nil, nil, nil) | ||
|
||
expect(breadcrumb.timestamp).to be_within(0.5.second).of Time.now | ||
end | ||
end | ||
|
||
describe "#ignore" do | ||
it "is not ignored by default" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new("my message", "test type", {:a => 1, :b => 2}, :manual) | ||
|
||
expect(breadcrumb.ignore?).to eq(false) | ||
end | ||
|
||
it "is able to be ignored" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new("my message", "test type", {:a => 1, :b => 2}, :manual) | ||
breadcrumb.ignore! | ||
|
||
expect(breadcrumb.ignore?).to eq(true) | ||
end | ||
end | ||
|
||
describe "#to_h" do | ||
it "outputs as a hash" do | ||
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new("my message", "test type", {:a => 1, :b => 2}, :manual) | ||
output = breadcrumb.to_h | ||
|
||
expect(output[:name]).to eq("my message") | ||
expect(output[:type]).to eq("test type") | ||
expect(output[:metaData]).to eq({ :a => 1, :b => 2}) | ||
|
||
timestamp_regex = /^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:[\d\.]+Z$/ | ||
|
||
expect(output[:timestamp]).to be_a_kind_of(String) | ||
expect(output[:timestamp]).to match(timestamp_regex) | ||
end | ||
end | ||
end |