-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add before_event hook for intercepting events (#567)
- Loading branch information
1 parent
57090ef
commit 2f86728
Showing
6 changed files
with
156 additions
and
23 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
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
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
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,56 @@ | ||
require 'forwardable' | ||
|
||
module Honeybadger | ||
class Event | ||
extend Forwardable | ||
|
||
# The timestamp of the event | ||
attr_reader :ts | ||
|
||
# The event_type of the event | ||
attr_reader :event_type | ||
|
||
# The payload data of the event | ||
attr_reader :payload | ||
|
||
def_delegator :payload, :[] | ||
|
||
# @api private | ||
def initialize(event_type_or_payload, payload={}) | ||
if event_type_or_payload.is_a?(String) | ||
@event_type = event_type_or_payload | ||
@payload = payload | ||
elsif event_type_or_payload.is_a?(Hash) | ||
@event_type = event_type_or_payload[:event_type] || event_type_or_payload["event_type"] | ||
@payload = event_type_or_payload | ||
end | ||
|
||
@ts = payload[:ts] || Time.now.utc.strftime("%FT%T.%LZ") | ||
@halted = false | ||
end | ||
|
||
# Halts the event and the before_event callback chain. | ||
# | ||
# Returns nothing. | ||
def halt! | ||
@halted ||= true | ||
end | ||
|
||
# @api private | ||
# Determines if this event will be discarded. | ||
def halted? | ||
!!@halted | ||
end | ||
|
||
# @api private | ||
# Template used to create JSON payload. | ||
# | ||
# @return [Hash] JSON representation of the event. | ||
def as_json(*args) | ||
payload.tap do |p| | ||
p[:ts] = ts | ||
p[:event_type] = event_type if event_type | ||
end | ||
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
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,56 @@ | ||
require 'honeybadger/event' | ||
require 'timecop' | ||
|
||
describe Honeybadger::Event do | ||
let(:event_type) { "event_type" } | ||
let(:payload) { {} } | ||
|
||
subject { described_class.new(event_type, payload) } | ||
|
||
describe "initialize" do | ||
context "event_type is passed as string" do | ||
let(:event_type) { "action" } | ||
let(:payload) { {} } | ||
|
||
its(:event_type) { should eq "action" } | ||
its(:payload) { should eq payload } | ||
end | ||
|
||
context "event_type is passed as part of payload" do | ||
subject { described_class.new(event_type) } | ||
|
||
let(:event_type) { { event_type: "action" } } | ||
|
||
its(:event_type) { should eq "action" } | ||
its(:payload) { should eq({ event_type: "action" }) } | ||
end | ||
end | ||
|
||
describe "ts" do | ||
before { Timecop.freeze } | ||
after { Timecop.unfreeze } | ||
|
||
its(:ts) { should eq Time.now.utc.strftime("%FT%T.%LZ") } | ||
end | ||
|
||
describe "halted" do | ||
context "halt! is not called" do | ||
its(:halted?) { should be false } | ||
end | ||
|
||
context "halt! is called" do | ||
before { subject.halt! } | ||
its(:halted?) { should be true } | ||
end | ||
end | ||
|
||
describe "as_json" do | ||
let(:event_type) { "action" } | ||
let(:payload) { { data1: 1 } } | ||
|
||
before { Timecop.freeze } | ||
after { Timecop.unfreeze } | ||
|
||
its(:as_json) { should eq({ event_type: "action", ts: Time.now.utc.strftime("%FT%T.%LZ"), data1: 1 }) } | ||
end | ||
end |