Custom async classes

30 views
Skip to first unread message

Gavin Joyce

unread,
Nov 16, 2012, 3:30:13 PM11/16/12
to golia...@googlegroups.com
What is the best approach for execution a number of custom classes in parellel? If API1..3 are custom classes that HTTP GET or POST and parse the response, what should they inherit from?

EM::MultiRequest.new
   API1.new.execute()
   API2.new.execute()
   API3.new.execute()

   multi.callback do
      #process all API responses here
   end
end


Oskar Szrajer

unread,
Nov 16, 2012, 4:25:14 PM11/16/12
to golia...@googlegroups.com

Celluloid?

Gavin Joyce

unread,
Nov 16, 2012, 5:26:21 PM11/16/12
to golia...@googlegroups.com
I've got it working with EM:Deferrable. Is this the correct approach for goliath or is another technique preferred?

require 'goliath'
require 'em-synchrony/em-http'

class GetLength
  include EM::Deferrable
  attr_accessor :data

  def execute(url)
    req = EM::HttpRequest.new(url).aget
    req.callback do
      @data = req.response.length.to_s
      succeed()
    end
    req.errback { fail }
    self
  end
  
end

class Hello < Goliath::API
  def response(env)
    multi = EventMachine::Synchrony::Multi.new
    multi.add :google, GetLength.new.execute('http://www.google.ie/')
    multi.add :yahoo, GetLength.new.execute('http://www.yahoo.com/')
    
    response = multi.perform.responses[:callback]
    [200, {}, {
      :google =>  response[:google].data,
      :yahoo =>  response[:yahoo].data
    }]
  end
end

Ilya Grigorik

unread,
Nov 16, 2012, 8:28:40 PM11/16/12
to golia...@googlegroups.com
Yep, looks good.
Reply all
Reply to author
Forward
0 new messages