Class: Groonga::Client::Response::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/client/response/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, header, body) ⇒ Base

Returns a new instance of Base.



265
266
267
268
269
270
271
# File 'lib/groonga/client/response/base.rb', line 265

def initialize(command, header, body)
  self.command = command
  self.header = header
  self.body = body
  self.trace_logs = nil
  self.raw = nil
end

Instance Attribute Details

#body::Hash

Returns The body of response. Its content is depends on command.

Returns:

  • (::Hash)

    The body of response. Its content is depends on command.

See Also:



256
257
258
# File 'lib/groonga/client/response/base.rb', line 256

def body
  @body
end

#commandGroonga::Command

Returns The command for the request.

Returns:

  • (Groonga::Command)

    The command for the request.



242
243
244
# File 'lib/groonga/client/response/base.rb', line 242

def command
  @command
end

#header::Array<Integer, Float, Float>

Returns The header of response. It consists of [return_code, start_time, elapsed_time_in_seconds] for success case. It consists of [return_code, start_time, elapsed_time_in_seconds, error_message, error_location] for error case.

Returns:

  • (::Array<Integer, Float, Float>)

    The header of response. It consists of [return_code, start_time, elapsed_time_in_seconds] for success case. It consists of [return_code, start_time, elapsed_time_in_seconds, error_message, error_location] for error case.

See Also:



251
252
253
# File 'lib/groonga/client/response/base.rb', line 251

def header
  @header
end

#rawString

Returns The unparsed response. It may be JSON, XML or Groonga command format.

Returns:

  • (String)

    The unparsed response. It may be JSON, XML or Groonga command format.



259
260
261
# File 'lib/groonga/client/response/base.rb', line 259

def raw
  @raw
end

#trace_logs::Array?

Returns The trace logs of response.

Returns:

  • (::Array, nil)

    The trace logs of response.

See Also:



263
264
265
# File 'lib/groonga/client/response/base.rb', line 263

def trace_logs
  @trace_logs
end

Class Method Details

.parse(command, raw_response) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/groonga/client/response/base.rb', line 68

def parse(command, raw_response)
  return_code = nil
  trace_logs = nil
  case command.output_type
  when :json
    callback = command["callback"]
    if callback and
        /\A#{Regexp.escape(callback)}\((.+)\);\z/ =~ raw_response
      json = $1
    else
      json = raw_response
    end
    begin
      response = JSON.parse(json)
    rescue JSON::ParserError => error
      raise InvalidResponse.new(command,
                                raw_response,
                                "invalid JSON: #{error}")
    end
    if response.is_a?(::Array)
      header, body = response
      return_code = header[0] if header
    else
      header = response["header"]
      trace_log = response["trace_log"]
      if trace_log
        names = trace_log["columns"].collect {|column| column["name"]}
        trace_logs = trace_log["logs"].collect do |log|
          Hash[names.zip(log)]
        end
      end
      body = response["body"]
      return_code = header["return_code"] if header
    end
  when :xml
    header, body = parse_xml(raw_response)
    return_code = header[0] if header
  when :tsv
    header, body = parse_tsv(raw_response)
    return_code = header["return_code"] if header
  when :arrow, :"apache-arrow"
    header, trace_logs, body = parse_apache_arrow(raw_response)
    return_code = header["return_code"] if header
  else
    header = nil
    body = raw_response
  end
  if header.nil? or return_code == 0
    response = new(command, header, body)
  else
    response = Error.new(command, header, body)
  end
  response.trace_logs = trace_logs
  response.raw = raw_response
  response
end

Instance Method Details

#elapsed_timeFloat

Returns The elapsed time of the request.

Returns:

  • (Float)

    The elapsed time of the request.

Since:

  • 0.1.0



307
308
309
310
311
312
313
314
315
# File 'lib/groonga/client/response/base.rb', line 307

def elapsed_time
  if header.nil?
    0.0
  elsif header_v1?
    header[2]
  else
    header["elapsed_time"]
  end
end

#error_messageString?

Returns The error message of the response.

Returns:

  • (String, nil)

    The error message of the response.

Since:

  • 0.2.4



319
320
321
322
323
324
325
326
327
# File 'lib/groonga/client/response/base.rb', line 319

def error_message
  if header.nil?
    nil
  elsif header_v1?
    header[3]
  else
    (header["error"] || {})["message"]
  end
end

#return_codeInteger

Returns The return code of the response.

Returns:

  • (Integer)

    The return code of the response.

Since:

  • 0.2.6



275
276
277
278
279
280
281
282
283
# File 'lib/groonga/client/response/base.rb', line 275

def return_code
  if header.nil?
    0
  elsif header_v1?
    header[0]
  else
    header["return_code"] || 0
  end
end

#start_timeTime

Returns The time of the request is accepted.

Returns:

  • (Time)

    The time of the request is accepted.

Since:

  • 0.1.0



295
296
297
298
299
300
301
302
303
# File 'lib/groonga/client/response/base.rb', line 295

def start_time
  if header.nil?
    Time.at(0)
  elsif header_v1?
    Time.at(header[1])
  else
    Time.at(header["start_time"])
  end
end

#status_codeInteger

Deprecated.

since 0.2.6. Use #return_code instead.

Returns The status code of the response.

Returns:

  • (Integer)

    The status code of the response.

Since:

  • 0.1.0



289
290
291
# File 'lib/groonga/client/response/base.rb', line 289

def status_code
  return_code
end

#success?Boolean

Returns true if the request is processed successfully, false otherwise.

Returns:

  • (Boolean)

    true if the request is processed successfully, false otherwise.

Since:

  • 0.1.0



332
333
334
# File 'lib/groonga/client/response/base.rb', line 332

def success?
  return_code.zero?
end