Class: RSolr::EM

Inherits:
Object
  • Object
show all
Defined in:
lib/rsolr-em.rb

Instance Method Summary collapse

Instance Method Details

#execute(client, request_context) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rsolr-em.rb', line 16

def execute client, request_context
  method = request_context[:method]
  options = {}
  options[:head] = request_context[:headers] if request_context[:headers]
  options[:body] = request_context[:data] if request_context[:body].to_s
  options[:proxy] ||= request_context[:proxy] if request_context[:proxy]
  
  uri = request_context[:uri]
  http_request = EventMachine::HttpRequest.new(uri.to_s)
  http = http_request.send(method, options)
  
  success_cb = request_context[:success] || request_context[:callback]
  error_cb = request_context[:error] || request_context[:errback]
  
  http.callback { |http|
    begin
      response_hash = {
        :body => http.response,
        :status => http.response_header.status,
        :headers => http.response_header
      }
      solr_response = client.adapt_response(request_context, response_hash)
      success_cb_args = case success_cb.arity
      when 1
        [solr_response]
      when 2
        [solr_response, request_context]
      when 3
        [solr_response, request_context, response_hash]
      end
      success_cb.call *success_cb_args
    rescue
      if error_cb
        error_cb_args = case error_cb.arity
        when 1
          [$!]
        when 2
          [$!, request_context, response_hash]
        when 3
          [$!, request_context, response_hash]
        end
        error_cb.call *error_cb_args
      else
        raise $!
      end
    end # begin/rescue clause
  }
  http.errback {
    if error_cb
      error_cb.call request_context, {}, nil
    end
  }
  nil
end