lib/grenache/http.rb in grenache-ruby-http-0.1.10 vs lib/grenache/http.rb in grenache-ruby-http-0.2.0

- old
+ new

@@ -1,6 +1,5 @@ - module Grenache class Http < Grenache::Base def listen(key, port, opts={}, &block) start_http_service(port,&block) @@ -10,25 +9,26 @@ end def start_http_service(port, &block) EM.defer { app = -> (env) { - req = Message.parse(env['rack.input'].read) - resp = block.call(req) - [200,nil, Message.response_to(req,resp).to_json] + req = ServiceMessage.parse(env['rack.input'].read) + err, payload = block.call(req) + [200,nil, ServiceMessage.new(payload, err, req.rid).to_json] } server = Thin::Server.start('0.0.0.0', port, app, {signals: false}) } end def request(key, payload) services = lookup(key) if services.size > 0 - json = Message.req(key,payload).to_json + json = ServiceMessage.new(payload).to_json service = services.sample.sub("tcp://","http://") service.prepend("http://") unless service.start_with?("http://") resp = HTTParty.post(service,{body: json}) - return [false, Message.parse(resp.body)] + msg = ServiceMessage.parse(resp.body) + return [msg.err, msg.payload] else return ["NoPeerFound",nil] end rescue Exception => e return [e, nil]