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

Commit

Permalink
Fix deep compact
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Feb 23, 2017
1 parent 7f7e574 commit 80cef25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
5 changes: 1 addition & 4 deletions lib/timber/events/http_server_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ def to_hash
alias to_h to_hash

def as_json(_options = {})
hash = to_hash
hash[:headers] = Util::Hash.compact(hash[:headers])
hash = Util::Hash.compact(hash)
{:server_side_app => {:http_request => hash}}
{:server_side_app => {:http_request => to_hash}}
end

def message
Expand Down
4 changes: 2 additions & 2 deletions lib/timber/log_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def as_json(options = {})
:time_ms => time_ms}

if !event.nil?
hash[:event] = event
hash[:event] = event.as_json
end

if !context_snapshot.nil? && context_snapshot.length > 0
Expand All @@ -60,7 +60,7 @@ def as_json(options = {})
hash
end

Util::Hash.compact(hash)
Util::Hash.deep_compact(hash)
end

def to_json(options = {})
Expand Down
18 changes: 15 additions & 3 deletions lib/timber/util/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ module Util
module Hash
extend self

def compact(hash)
hash.select do |k, v|
v != nil && v != {} && v != []
def deep_compact(hash)
new_hash = {}

hash.each do |k, v|
v = if v.is_a?(::Hash)
deep_compact(v)
else
v
end

if v != nil && v != "" && v != {} && v != []
new_hash[k] = v
end
end

new_hash
end
end
end
Expand Down

0 comments on commit 80cef25

Please sign in to comment.