Executes HTTP requests using a predefined adapter. All request methods accept an HTTPI::Request and an optional adapter. They may also offer shortcut methods for executing basic requests. Also they all return an HTTPI::Response.
request = HTTPI::Request.new :url => "http://example.com" HTTPI.get request, :httpclient
HTTPI.get "http://example.com", :curb
request = HTTPI::Request.new request.url = "http://example.com" request.body = "<some>xml</some>" HTTPI.post request, :httpclient
HTTPI.post "http://example.com", "<some>xml</some>", :curb
request = HTTPI::Request.new :url => "http://example.com" HTTPI.head request, :httpclient
HTTPI.head "http://example.com", :curb
request = HTTPI::Request.new request.url = "http://example.com" request.body = "<some>xml</some>" HTTPI.put request, :httpclient
HTTPI.put "http://example.com", "<some>xml</some>", :curb
request = HTTPI::Request.new :url => "http://example.com" HTTPI.delete request, :httpclient
HTTPI.delete "http://example.com", :curb
If you need more control over your request, you can access the HTTP client instance represented by your adapter in a block.
HTTPI.get request do |http| http.follow_redirect_count = 3 # HTTPClient example end
Shortcut for setting the default adapter to use.
# File lib/httpi.rb, line 137 def adapter=(adapter) Adapter.use = adapter end
Executes an HTTP DELETE request.
# File lib/httpi.rb, line 121 def delete(request, adapter = nil) request = Request.new :url => request if request.kind_of? String with_adapter :delete, request, adapter do |adapter| yield adapter.client if block_given? adapter.delete request end end
Executes an HTTP GET request.
# File lib/httpi.rb, line 81 def get(request, adapter = nil) request = Request.new :url => request if request.kind_of? String with_adapter :get, request, adapter do |adapter| yield adapter.client if block_given? adapter.get request end end
Executes an HTTP HEAD request.
# File lib/httpi.rb, line 101 def head(request, adapter = nil) request = Request.new :url => request if request.kind_of? String with_adapter :head, request, adapter do |adapter| yield adapter.client if block_given? adapter.head request end end
Logs given messages.
# File lib/httpi.rb, line 166 def log(*messages) level = Symbol === messages.first ? messages.shift : log_level logger.send level, messages.join(" ") if log? end
Returns whether to log HTTP requests. Defaults to true.
# File lib/httpi.rb, line 145 def log? @log != false end
Returns the log level. Defaults to :debug.
# File lib/httpi.rb, line 161 def log_level @log_level ||= DEFAULT_LOG_LEVEL end
Returns the logger. Defaults to an instance of Logger writing to STDOUT.
# File lib/httpi.rb, line 153 def logger @logger ||= ::Logger.new STDOUT end
Executes an HTTP POST request.
# File lib/httpi.rb, line 91 def post(*args) request, adapter = request_and_adapter_from(args) with_adapter :post, request, adapter do |adapter| yield adapter.client if block_given? adapter.post request end end
Executes an HTTP PUT request.
# File lib/httpi.rb, line 111 def put(*args) request, adapter = request_and_adapter_from(args) with_adapter :put, request, adapter do |adapter| yield adapter.client if block_given? adapter.put request end end
Executes an HTTP request for the given method.
# File lib/httpi.rb, line 131 def request(method, request, adapter = nil) raise ArgumentError, "Invalid request method: #{method}" unless REQUEST_METHODS.include? method send method, request, adapter end
Generated with the Darkfish Rdoc Generator 2.