Created
May 25, 2013 07:31
-
-
Save bash0C7/5648274 to your computer and use it in GitHub Desktop.
Fluent Parser
This file contains hidden or 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
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 |
This file contains hidden or 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
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