Class: MTProto::Client::RPC::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/client/rpc/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg_id, response_class, body_bytes) ⇒ Response

Returns a new instance of Response.



11
12
13
14
15
16
17
18
# File 'lib/mtproto/client/rpc/response.rb', line 11

def initialize(msg_id, response_class, body_bytes)
  @msg_id = msg_id
  @response_class = response_class
  @body_bytes = body_bytes
  @condition = ::Async::Condition.new
  @result = nil
  @error = nil
end

Instance Attribute Details

#body_bytesObject (readonly)

Returns the value of attribute body_bytes.



9
10
11
# File 'lib/mtproto/client/rpc/response.rb', line 9

def body_bytes
  @body_bytes
end

#msg_idObject (readonly)

Returns the value of attribute msg_id.



9
10
11
# File 'lib/mtproto/client/rpc/response.rb', line 9

def msg_id
  @msg_id
end

#response_classObject (readonly)

Returns the value of attribute response_class.



9
10
11
# File 'lib/mtproto/client/rpc/response.rb', line 9

def response_class
  @response_class
end

Instance Method Details

#bodyObject

Raises:

  • (@error)


38
39
40
41
42
43
# File 'lib/mtproto/client/rpc/response.rb', line 38

def body
  raise 'Response not received yet' unless received?
  raise @error if @error

  @result
end

#received?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/mtproto/client/rpc/response.rb', line 20

def received?
  !@result.nil? || !@error.nil?
end

#signal(raw_body) ⇒ Object



45
46
47
48
# File 'lib/mtproto/client/rpc/response.rb', line 45

def signal(raw_body)
  @result = @response_class.deserialize(raw_body)
  @condition.signal
end

#signal_error(error) ⇒ Object



50
51
52
53
# File 'lib/mtproto/client/rpc/response.rb', line 50

def signal_error(error)
  @error = error
  @condition.signal
end

#wait!(timeout = nil) ⇒ Object

Raises:

  • (@error)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mtproto/client/rpc/response.rb', line 24

def wait!(timeout = nil)
  unless received?
    if timeout
      ::Async::Task.current.with_timeout(timeout) { receive_signal }
    else
      receive_signal
    end
  end

  raise @error if @error

  @result
end