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.



160
161
162
163
164
165
# File 'lib/groonga/client/response/base.rb', line 160

def initialize(command, header, body)
  self.command = command
  self.header = header
  self.body = body
  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:



155
156
157
# File 'lib/groonga/client/response/base.rb', line 155

def body
  @body
end

#commandGroonga::Command

Returns The command for the request.

Returns:

  • (Groonga::Command)

    The command for the request.



141
142
143
# File 'lib/groonga/client/response/base.rb', line 141

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:



150
151
152
# File 'lib/groonga/client/response/base.rb', line 150

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.



158
159
160
# File 'lib/groonga/client/response/base.rb', line 158

def raw
  @raw
end

Class Method Details

.parse(command, raw_response) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/groonga/client/response/base.rb', line 63

def parse(command, raw_response)
  return_code = nil
  case command.output_type
  when :json
    response = JSON.parse(raw_response)
    if response.is_a?(::Array)
      header, body = response
      return_code = header[0] if header
    else
      header = response["header"]
      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
  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.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



201
202
203
204
205
206
207
208
209
# File 'lib/groonga/client/response/base.rb', line 201

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



213
214
215
216
217
218
219
220
221
# File 'lib/groonga/client/response/base.rb', line 213

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



169
170
171
172
173
174
175
176
177
# File 'lib/groonga/client/response/base.rb', line 169

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



189
190
191
192
193
194
195
196
197
# File 'lib/groonga/client/response/base.rb', line 189

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



183
184
185
# File 'lib/groonga/client/response/base.rb', line 183

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



226
227
228
# File 'lib/groonga/client/response/base.rb', line 226

def success?
  return_code.zero?
end