class Solr4R::Response

Constants

DEFAULT_CHARSET

Attributes

request[R]
request_body[RW]
request_headers[RW]
request_method[RW]
request_params[RW]
request_url[RW]
response_body[RW]
response_charset[RW]
response_code[RW]
response_headers[RW]

Public Class Methods

new(request, req = nil, res = nil) click to toggle source
# File lib/solr4r/response.rb, line 36
def initialize(request, req = nil, res = nil)
  @request, @evaluate = request, req && req.response_body_permitted?

  if req
    self.request_body    = req.body
    self.request_headers = req.to_hash
    self.request_method  = req.method.downcase.to_sym
    self.request_params  = req.uri.params
    self.request_url     = req.uri.to_s
  end

  if res
    self.response_body    = res.body
    self.response_headers = res.to_hash
    self.response_charset = res.type_params['charset']
    self.response_code    = res.code.to_i
  end
end

Public Instance Methods

inspect() click to toggle source
# File lib/solr4r/response.rb, line 84
def inspect
  '#<%s:0x%x @request_url=%p, @request_headers=%p, @response_headers=%p, @response_code=%p>' % [
    self.class, object_id, request_url, request_headers, response_headers, response_code
  ]
end
num_found() click to toggle source
# File lib/solr4r/response.rb, line 74
def num_found
  @num_found ||= evaluate_count('numFound') if @evaluate
end
Also aliased as: to_i
request_line() click to toggle source
# File lib/solr4r/response.rb, line 62
def request_line
  '"%s %s" %d' % [request_method.upcase, request_url, response_code]
end
result() click to toggle source
# File lib/solr4r/response.rb, line 70
def result
  @result ||= evaluate_result if @evaluate
end
success?() click to toggle source
# File lib/solr4r/response.rb, line 66
def success?
  response_code / 100 == 2
end
to_i()
Alias for: num_found
to_s() click to toggle source
# File lib/solr4r/response.rb, line 80
def to_s
  @to_s ||= response_body.to_s.force_encoding(response_charset || DEFAULT_CHARSET)
end

Private Instance Methods

evaluate_count(name) click to toggle source
# File lib/solr4r/response.rb, line 101
def evaluate_count(name)
  case wt = request_params[:wt]
    when 'xml'  then extract_int(name, '\s%s="%s"')  #  numFound="35"
    when 'ruby' then extract_int(name, "'%s'=>%s")   # 'numFound'=>35
    when 'json' then extract_int(name, '"%s":%s')    # "numFound":35
    when Symbol then result % ['response', name]     # {"response"=>{"numFound"=>35}}
    else raise 'The count cannot be extracted: wt=%p not supported.' % wt
  end
end
evaluate_result() click to toggle source
# File lib/solr4r/response.rb, line 92
def evaluate_result
  case wt = request_params[:wt]
    when String then to_s
    when :ruby  then result_object(eval(to_s))
    when :json  then result_object(JSON.parse(to_s))
    else raise 'The response cannot be evaluated: wt=%p not supported.' % wt
  end
end
extract_int(name, pattern) click to toggle source
# File lib/solr4r/response.rb, line 111
def extract_int(name, pattern)
  Integer(result[Regexp.new(pattern % [name, '(\d+)']), 1])
end
result_object(object) click to toggle source
# File lib/solr4r/response.rb, line 115
def result_object(object)
  object.is_a?(Hash) ? Result.new(self, object) : object
end