Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Coerce context and event attributes (#45)
Browse files Browse the repository at this point in the history
* Coerce context and event attributes

* Coerce System.pid

* More tests
  • Loading branch information
binarylogic committed Feb 23, 2017
1 parent a4445cd commit 873e350
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/timber/contexts/custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(attributes)
end

def as_json(_options = {})
{type => data}
{Timber::Object.try(type, :to_sym) => data}
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/timber/contexts/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(attributes)
end

def as_json(_options = {})
{id: id, name: name}
{id: Timber::Object.try(id, :to_s), name: name}
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/timber/contexts/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(attributes)
end

def as_json(_options = {})
{pid: pid}
{pid: Timber::Object.try(pid, :to_s)}
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/timber/contexts/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(attributes)
end

def as_json(_options = {})
{id: id, name: name}
{id: Timber::Object.try(id, :to_s), name: name}
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/timber/events/custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(attributes)
end

def to_hash
{type => data}
{Timber::Object.try(type, :to_sym) => data}
end
alias to_h to_hash

Expand Down
1 change: 1 addition & 0 deletions lib/timber/util.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "timber/util/active_support_log_subscriber"
require "timber/util/hash"
require "timber/util/object"
require "timber/util/struct"

module Timber
Expand Down
13 changes: 13 additions & 0 deletions lib/timber/util/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Timber
# @private
module Object
# @private
def self.try(object, method)
if object == nil
nil
else
object.send(method) rescue object
end
end
end
end
11 changes: 11 additions & 0 deletions spec/timber/contexts/custom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"

describe Timber::Contexts::Custom, :rails_23 => true do
describe ".as_json" do
it "should coerce type into an atom" do
custom_context = described_class.new(:type => "my type", :data => {:key => "value"})
json = custom_context.as_json()
expect(json.keys.first).to eq(:"my type")
end
end
end
11 changes: 11 additions & 0 deletions spec/timber/contexts/organization_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"

describe Timber::Contexts::Organization, :rails_23 => true do
describe ".as_json" do
it "should coerce id into a string" do
user_context = described_class.new(:id => 1)
json = user_context.as_json()
expect(json[:id]).to eq("1")
end
end
end
11 changes: 11 additions & 0 deletions spec/timber/contexts/system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"

describe Timber::Contexts::System, :rails_23 => true do
describe ".as_json" do
it "should coerce pid into an string" do
custom_context = described_class.new(:pid => 1)
json = custom_context.as_json()
expect(json[:pid]).to eq("1")
end
end
end
11 changes: 11 additions & 0 deletions spec/timber/contexts/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"

describe Timber::Contexts::User, :rails_23 => true do
describe ".as_json" do
it "should coerce id into a string" do
user_context = described_class.new(:id => 1)
json = user_context.as_json()
expect(json[:id]).to eq("1")
end
end
end
11 changes: 11 additions & 0 deletions spec/timber/events/custom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"

describe Timber::Events::Custom, :rails_23 => true do
describe ".to_hash" do
it "should coerce type into an atom" do
custom_context = described_class.new(:type => "my type", :message => "hello", :data => {:key => "value"})
hash = custom_context.to_hash()
expect(hash.keys.first).to eq(:"my type")
end
end
end

0 comments on commit 873e350

Please sign in to comment.