stringify all symbols in twitter hash

Fixes #1450
This commit is contained in:
Colin Surprenant 2014-06-18 17:16:45 +00:00 committed by Jordan Sissel
parent a536eefad2
commit bf11118709
3 changed files with 47 additions and 1 deletions

View file

@ -2,6 +2,7 @@
require "logstash/inputs/base"
require "logstash/namespace"
require "logstash/timestamp"
require "logstash/util"
# Read events from the twitter streaming api.
class LogStash::Inputs::Twitter < LogStash::Inputs::Base
@ -67,7 +68,7 @@ class LogStash::Inputs::Twitter < LogStash::Inputs::Base
@client.filter(:track => @keywords.join(",")) do |tweet|
@logger.info? && @logger.info("Got tweet", :user => tweet.user.screen_name, :text => tweet.text)
if @full_tweet
event = LogStash::Event.new(tweet.to_hash)
event = LogStash::Event.new(LogStash::Util.stringify_symbols(tweet.to_hash))
event.timestamp = LogStash::Timestamp.new(tweet.created_at)
else
event = LogStash::Event.new(

View file

@ -136,5 +136,17 @@ module LogStash::Util
def self.normalize(o); o; end
end
def self.stringify_symbols(o)
case o
when Hash
o.inject({}){|r, (k, v)| r[k.is_a?(Symbol) ? k.to_s : k] = stringify_symbols(v); r}
when Array
o.map{|i| stringify_symbols(i)}
when Symbol
o.to_s
else
o
end
end
end # module LogStash::Util

33
spec/util_spec.rb Normal file
View file

@ -0,0 +1,33 @@
require "logstash/util"
describe LogStash::Util do
context "stringify_keys" do
it "should convert hash symbol keys to strings" do
expect(LogStash::Util.stringify_symbols({:a => 1, "b" => 2})).to eq({"a" => 1, "b" => 2})
end
it "should keep non symbolic hash keys as is" do
expect(LogStash::Util.stringify_symbols({1 => 1, 2.0 => 2})).to eq({1 => 1, 2.0 => 2})
end
it "should convert inner hash keys to strings" do
expect(LogStash::Util.stringify_symbols({:a => 1, "b" => {:c => 3}})).to eq({"a" => 1, "b" => {"c" => 3}})
expect(LogStash::Util.stringify_symbols([:a, 1, "b", {:c => 3}])).to eq(["a", 1, "b", {"c" => 3}])
end
it "should convert hash symbol values to strings" do
expect(LogStash::Util.stringify_symbols({:a => :a, "b" => :b})).to eq({"a" => "a", "b" => "b"})
end
it "should convert array symbol values to strings" do
expect(LogStash::Util.stringify_symbols([1, :a])).to eq([1, "a"])
end
it "should convert innner array symbol values to strings" do
expect(LogStash::Util.stringify_symbols({:a => [1, :b]})).to eq({"a" => [1, "b"]})
expect(LogStash::Util.stringify_symbols([:a, [1, :b]])).to eq(["a", [1, "b"]])
end
end
end