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

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

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ ResponseHandler



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

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

Instance Method Details

#<<(chunk) ⇒ Object



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

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

#on_body(chunk) ⇒ Object



384
385
386
# File 'lib/fluent/plugin/in_groonga.rb', line 384

def on_body(chunk)
  @body << chunk
end

#on_headers_complete(headers) ⇒ Object



375
376
377
378
379
380
381
382
# File 'lib/fluent/plugin/in_groonga.rb', line 375

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



370
371
372
373
# File 'lib/fluent/plugin/in_groonga.rb', line 370

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

#on_message_completeObject



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/fluent/plugin/in_groonga.rb', line 388

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
      $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
      $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