Skip to content

Commit

Permalink
Merge 34ba325 into c8938c9
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec committed Dec 5, 2018
2 parents c8938c9 + 34ba325 commit 9ecda43
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
57 changes: 57 additions & 0 deletions lib/bugsnag/breadcrumbs/breadcrumb.rb
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
90 changes: 90 additions & 0 deletions spec/breadcrumbs/breadcrumb_spec.rb
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

0 comments on commit 9ecda43

Please sign in to comment.