Class: HTTPClientPatch

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client_patch/include_client.rb

Overview

It is useful to re-use a HTTPClient instance for multiple requests, to re-use HTTP 1.1 persistent connections.

To do that, you sometimes want to store an HTTPClient instance in a global/ class variable location, so it can be accessed and re-used.

This mix-in makes it easy to create class-level access to one or more HTTPClient instances. The HTTPClient instances are lazily initialized on first use (to, for instance, avoid interfering with WebMock/VCR), and are initialized in a thread-safe manner. Note that a HTTPClient, once initialized, is safe for use in multiple threads.

Note that you ‘extend` HTTPClient::IncludeClient, not `include.

require 'httpclient/include_client'
class Widget
   extend HTTPClient::IncludeClient

   include_http_client
   # and/or, specify more stuff
   include_http_client('http://myproxy:8080', :method_name => :my_client) do |client|
      # any init you want
      client.set_cookie_store nil
      client.
   end
end

That creates two HTTPClient instances available at the class level. The first will be available from Widget.http_client (default method name for ‘include_http_client`), with default initialization.

The second will be available at Widget.my_client, with the init arguments provided, further initialized by the block provided.

In addition to a class-level method, for convenience instance-level methods are also provided. Widget.http_client is identical to Widget.new.http_client

Defined Under Namespace

Modules: IncludeClient