Sha256: c098b63867306975b78d20996f41ff946fa7441a0ee3bbab74ccbcd884815771

Contents?: true

Size: 912 Bytes

Versions: 2

Compression:

Stored size: 912 Bytes

Contents

module Grenache
  class Http < Grenache::Base
    def listen(key, port,  opts={}, &block)
      start_http_service(port,&block)

      announce(key, port, opts) do |res|
        puts "#{key} announced #{res}"
      end
    end

    def start_http_service(port, &block)
      EM.defer {
        app = -> (env) {
          block.call(env)
        }
        server = Thin::Server.start('0.0.0.0', port, app, {signals: false})
      }
    end

    def request(key, payload, &block)
      services = lookup(key)
      if services.size > 0
        json = Oj.dump(payload)
        service = services.sample.sub("tcp://","http://")
        service.prepend("http://") unless service.start_with?("http://")
        resp = HTTParty.post(service,{body: json})
        return [nil, Oj.load(resp.body)]
      else
        return ["NoPeerFound",nil]
      end
    rescue Exception => e
      return [e, nil]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grenache-ruby-http-0.1.2 lib/grenache/http.rb
grenache-ruby-http-0.1.1 lib/grenache/http.rb