Class: RPC::Clients::EmHttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rpc/lib/rpc/clients/em-http-request.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ EmHttpRequest

Returns a new instance of EmHttpRequest.



18
19
20
21
# File 'lib/rpc/lib/rpc/clients/em-http-request.rb', line 18

def initialize(uri)
  @client = EventMachine::HttpRequest.new(uri)
  @in_progress = 0
end

Instance Method Details

#async?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rpc/lib/rpc/clients/em-http-request.rb', line 53

def async?
  true
end

#connectObject



23
24
# File 'lib/rpc/lib/rpc/clients/em-http-request.rb', line 23

def connect
end

#disconnectObject



26
27
# File 'lib/rpc/lib/rpc/clients/em-http-request.rb', line 26

def disconnect
end

#run(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rpc/lib/rpc/clients/em-http-request.rb', line 29

def run(&block)
  EM.run do
    block.call

    # Note: There's no way how to stop the
    # reactor when there are no remaining events.
    EM.add_periodic_timer(0.1) do
      EM.stop if @in_progress == 0
    end
  end
end

#send(data, &callback) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rpc/lib/rpc/clients/em-http-request.rb', line 41

def send(data, &callback)
  request = @client.post(head: HEADERS, body: data)
  @in_progress += 1
  request.callback do |response|
    if callback
      callback.call(response.response)
    end

    @in_progress -= 1
  end
end