Skip to content

Instantly share code, notes, and snippets.

@bash0C7
Created May 25, 2013 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bash0C7/5648274 to your computer and use it in GitHub Desktop.
Save bash0C7/5648274 to your computer and use it in GitHub Desktop.
Fluent Parser
module Fluent
class TextParser
class MyJSONParser
include Configurable
config_param :time_key, :string, :default => 'time'
config_param :time_format, :string, :default => nil
def call(text)
record = Yajl.load(text)
binding.pry
if value = record.delete(@time_key)
if @time_format
time = Time.strptime(value, @time_format).to_i
else
time = value.to_i
end
else
time = Engine.now
end
return time, record
rescue Yajl::ParseError
$log.warn "pattern not match: #{text.inspect}: #{$!}"
return nil, nil
end
end
end
end
describe Fluent::TextParser::MyJSONParser do
describe '#call' do
before do
end
let(:text) {'{"time":1362020400,"NAME":"KOSHIBA","AGE":32,"HOGE":"fuga"}'}
let(:result) {described_class.new.call(text)}
context do
subject {result.first}
it {should == 1362020400}
end
context do
let(:hash) {result.last}
it do
hash.size.should == 3
hash['NAME'].should == 'KOSHIBA'
hash['AGE'].should == 32
hash['HOGE'].should == 'fuga'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment