Class: Grenache::Http
- Inherits:
-
Base
- Object
- Base
- Grenache::Http
- Defined in:
- lib/grenache/http.rb
Instance Method Summary collapse
- #listen(key, port, opts = {}, &block) ⇒ Object
- #request(key, payload, &block) ⇒ Object
- #start_http_service(port, &block) ⇒ Object
Instance Method Details
#listen(key, port, opts = {}, &block) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/grenache/http.rb', line 4 def listen(key, port, opts={}, &block) start_http_service(port,&block) announce(key, port, opts) do |res| puts "#{key} announced #{res}" end end |
#request(key, payload, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/grenache/http.rb', line 21 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 |
#start_http_service(port, &block) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/grenache/http.rb', line 12 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 |