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.



330
331
332
333
334
# File 'lib/fluent/plugin/in_groonga.rb', line 330

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

Instance Method Details

#<<(chunk) ⇒ Object



336
337
338
# File 'lib/fluent/plugin/in_groonga.rb', line 336

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

#on_body(chunk) ⇒ Object



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

def on_body(chunk)
  @body << chunk
end

#on_headers_complete(headers) ⇒ Object



345
346
347
348
349
350
351
352
# File 'lib/fluent/plugin/in_groonga.rb', line 345

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



340
341
342
343
# File 'lib/fluent/plugin/in_groonga.rb', line 340

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

#on_message_completeObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/fluent/plugin/in_groonga.rb', line 358

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