Module: EventMachine::UCEngine::EventMachineResponse

Included in:
Client, Client::Session
Defined in:
lib/em-ucengine/client_em.rb

Overview

Use response as block

Instance Method Summary collapse

Instance Method Details

#answer(req, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/em-ucengine/client_em.rb', line 13

def answer(req, &block)
  response = EM::DefaultDeferrable.new
  req.errback do
    error = UCEngine::Client::HttpError.new(0, "connect error", req.last_effective_url)
    response.fail error
    yield error, nil if block_given?
  end
  req.callback do
    data = req.response
    if data["error"]
      error = UCEngine::Client::UCError.new(req.response_header.status, data["error"], req.last_effective_url)
      response.fail error
      yield error, nil if block_given?
    else
      response.succeed data["result"]
      yield nil, data["result"] if block_given?
    end
  end
  response
end

#answer_bool(req, &block) ⇒ Object



34
35
36
37
38
39
# File 'lib/em-ucengine/client_em.rb', line 34

def answer_bool(req, &block)
  answer(req) do |err, result|
    result = (result == "true")
    yield nil, result if block_given?
  end
end

#answer_connect(http) ⇒ Object



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
91
92
# File 'lib/em-ucengine/client_em.rb', line 65

def answer_connect(http)
  response = EM::DefaultDeferrable.new
  http.errback do
    error = UCEngine::Client::HttpError.new(0, "connect error", http.uri)
    response.fail error
    yield error, nil if block_given?
  end
  http.callback do
    if http.response_header.status >= 500
      error = UCEngine::Client::HttpError.new(http.response_header.status, http.response, http.uri)
      response.fail error
      yield error, nil if block_given?
    else
      data = http.response
      if data['error']
        error = UCEngine::Client::UCError.new(http.response_header.status, data['error'], http.req.uri.to_s)
        response.fail error
        yield error, nil if block_given?
      else
        result = data["result"]
        session = UCEngine::Client::Session.new(self, result["uid"], result["sid"])
        response.succeed session
        yield nil, session if block_given?
      end
    end
  end
  response
end

#answer_download(req) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/em-ucengine/client_em.rb', line 41

def answer_download(req)
  response = EM::DefaultDeferrable.new
  req.errback do
    error = UCEngine::Client::HttpError.new(0, "connect error", req.last_effective_url)
    response.fail error
    yield error, nil if block_given?
  end
  req.callback do
    if req.response_header.status == 200
      data = req.response
      filename = req.response_header['CONTENT_DISPOSITION']
      file = Tempfile.new(filename)
      file.write(data)
      response.succeed file
      yield nil, file if block_given?
    else
      error = UCEngine::Client::HttpError.new(0, "download error")
      response.fail error
      yield error, nil if block_given?
    end
  end
  response
end