Skip to content

Commit

Permalink
Merge 8e2042e into 1b39175
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec committed Dec 6, 2018
2 parents 1b39175 + 8e2042e commit a18749e
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
76 changes: 76 additions & 0 deletions lib/bugsnag/breadcrumbs/breadcrumb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module Bugsnag::Breadcrumbs
class Breadcrumb
# @return [String] the breadcrumb name
attr_accessor :name

# @return [String] the breadcrumb type
attr_accessor :type

# @return [Hash, nil] metadata hash containing strings, numbers, or booleans, or nil
attr_accessor :meta_data

# @return [Boolean] set to `true` if the breadcrumb was automatically generated
attr_reader :auto

# @return [Time] a Time object referring to breadcrumb creation time
attr_reader :timestamp

##
# Creates a breadcrumb
#
# This will not have been validated, which must occur before this is attached to a report
#
# @api private
#
# @param name [String] the breadcrumb name
# @param type [String] the breadcrumb type from Bugsnag::Breadcrumbs::VALID_BREADCRUMB_TYPES
# @param meta_data [Hash, nil] a hash containing strings, numbers, or booleans, or nil
# @param auto [Symbol] set to `:auto` if the breadcrumb is automatically generated
def initialize(name, type, meta_data, auto)
@should_ignore = false
self.name = name
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.utc
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
#
# @return [True] if `ignore!` has been called
# @return [nil] if `ignore` has not been called
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
#
# @return [Hash] Hash representation of the breadcrumb
def to_h
{
:name => @name,
:type => @type,
:metaData => @meta_data,
:timestamp => @timestamp.iso8601
}
end
end
end
93 changes: 93 additions & 0 deletions spec/breadcrumbs/breadcrumb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# encoding: utf-8

require 'spec_helper'

require 'bugsnag/breadcrumbs/breadcrumb'

RSpec.describe Bugsnag::Breadcrumbs::Breadcrumb do
describe "#name" do
it "is assigned in #initialize" do
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new("my message", nil, nil, nil)

expect(breadcrumb.name).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 "is 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).of Time.now.utc
end
end

describe "#ignore?" do
it "is not true 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 set" 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

timestamp_regex = /^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:[\d\.]+Z$/

expect(output).to match(
:name => "my message",
:type => "test type",
:metaData => {
:a => 1,
:b => 2
},
:timestamp => match(timestamp_regex)
)
end
end
end

0 comments on commit a18749e

Please sign in to comment.