Class: Fluent::Plugin::GroongaInput::HTTPInput::ResponseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/in_groonga.rb

Instance Method Summary collapse

Constructor Details

#initialize(handler, input) ⇒ ResponseHandler

Returns a new instance of ResponseHandler.



343
344
345
346
347
# File 'lib/fluent/plugin/in_groonga.rb', line 343

def initialize(handler, input)
  @handler = handler
  @input = input
  @parser = Http::Parser.new(self)
end

Instance Method Details

#<<(chunk) ⇒ Object



349
350
351
# File 'lib/fluent/plugin/in_groonga.rb', line 349

def <<(chunk)
  @parser << chunk
end

#on_body(chunk) ⇒ Object



367
368
369
# File 'lib/fluent/plugin/in_groonga.rb', line 367

def on_body(chunk)
  @body << chunk
end

#on_headers_complete(headers) ⇒ Object



358
359
360
361
362
363
364
365
# File 'lib/fluent/plugin/in_groonga.rb', line 358

def on_headers_complete(headers)
  headers.each do |name, value|
    case name
    when /\AContent-Type\z/i
      @content_type = value
    end
  end
end

#on_message_beginObject



353
354
355
356
# File 'lib/fluent/plugin/in_groonga.rb', line 353

def on_message_begin
  @body = ""
  @content_type = nil
end

#on_message_completeObject



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/fluent/plugin/in_groonga.rb', line 371

def on_message_complete
  return if @parser.status_code == 100

  response = nil
  case @content_type
  when /\Aapplication\/json\z/i
    begin
      response = JSON.parse(@body)
    rescue JSON::ParserError
      @input.log.warn("[input][groonga][response][warn] " +
                      "failed to parse response JSON:",
                      :error => "#{$!.class}: #{$!}",
                      :json => @body)
    end
  when /\Aapplication\/x-msgpack\z/i
    begin
      response = MessagePack.unpack(@body)
    rescue MessagePack::UnpackError, EOFError
      @input.log.warn("[input][groonga][response][warn] " +
                      "failed to parse response MessagePack",
                      :error => "#{$!.class}: #{$!}",
                      :msgpack => @body)
    end
  when /\Atext\/x-groonga-command-list\z/i
    response = @body
  end
  @handler.on_response_complete(response)
end