Sha256: 0ddb517beb464152ab304a863e3c66cf19a55ca5db1c34d81be491f41b618ab9
Contents?: true
Size: 1.36 KB
Versions: 2
Compression:
Stored size: 1.36 KB
Contents
module DockTest module Methods %w(get post put patch delete options head).each do |meth_name| define_method meth_name do |path, params = '', headers = {}, &block| uri = URI.join(DockTest.url, path) if DockTest.localhost? uri.port = DockTest.port end # add the params to the GET requests if meth_name == 'get' && !params.empty? if(params.is_a?(Hash)) uri.query = URI.encode_www_form(URI.decode_www_form(uri.query || '') + params.to_a) else uri.query = uri.query.nil? ? params : "#{uri.query}&#{params}" end end @last_request = Net::HTTP.const_get(meth_name.capitalize).new(uri.request_uri) # add the params to the body of other requests if meth_name != 'get' @last_request.body = params end # sets the headers headers.each do |key, value| @last_request.add_field(key, value) end # execute the request @last_response = Net::HTTP.start(uri.hostname, uri.port) do |http| http.request(@last_request) end yield @last_response if block_given? @last_response end private meth_name end private def last_response @last_response end def last_request @last_request end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dock_test-0.0.2 | lib/dock_test/methods.rb |
dock_test-0.0.1 | lib/dock_test/methods.rb |