lib/hatetepe/client.rb in hatetepe-0.5.0.pre.1 vs lib/hatetepe/client.rb in hatetepe-0.5.0.pre.2

- old
+ new

@@ -54,11 +54,11 @@ # @builder.on_write {|data| p "|--> #{data}" } @parser.on_response << method(:receive_response) @queue = [] - @app = method(:send_request) + @app = proc {|request| send_request(request) } self.comm_inactivity_timeout = config[:timeout] self.pending_connect_timeout = config[:connect_timeout] end @@ -102,11 +102,13 @@ if response && request.verb == "HEAD" response.body.close_write end - if !response || response.failure? + if !response + request.fail(nil, self) + elsif response.failure? request.fail(response) else request.succeed(response) end end.resume @@ -126,11 +128,11 @@ # # @return [Hatetepe::Response, nil] # # @api public def request(verb, uri, headers = {}, body = []) - request = Hatetepe::Request.new(verb, uri, headers, body) + request = Hatetepe::Request.new(verb, URI(uri), headers, body) self << request EM::Synchrony.sync(request) end # Gracefully stops the client. @@ -180,10 +182,15 @@ EM.connect(config[:host], config[:port], self, config) end # @api public def self.request(verb, uri, headers = {}, body = []) + uri = URI(uri) + client = start(host: uri.host, port: uri.port) + client.request(verb, uri, headers, body) + ensure + client.stop end # Feeds the request into the builder and blocks while waiting for the # response to arrive. # @@ -246,6 +253,19 @@ job.fiber.resume else raise "Received response but didn't expect one: #{response.status}" end end + + module VerbMethods + [ + :get, :head, :options, :put, :post, :delete, :patch, :connect + ].each do |verb| + define_method(verb) do |uri, headers = {}, body = []| + request(verb, uri, headers, body) + end + end + end + + include VerbMethods + extend VerbMethods end