Class: HTTP::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Chainable
Defined in:
lib/http/client.rb

Overview

Clients make requests and receive responses

Constant Summary collapse

HTTP_OR_HTTPS_RE =
%r{^https?://}i

Instance Method Summary collapse

Methods included from Chainable

#accept, #auth, #basic_auth, #connect, #cookies, #default_options, #default_options=, #delete, #encoding, #follow, #get, #head, #headers, #nodelay, #options, #patch, #persistent, #post, #put, #timeout, #trace, #use, #via

Constructor Details

#initialize(default_options = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
# File 'lib/http/client.rb', line 20

def initialize(default_options = {})
  @default_options = HTTP::Options.new(default_options)
  @connection = nil
  @state = :clean
end

Instance Method Details

#build_request(verb, uri, opts = {}) ⇒ Object

Prepare an HTTP request



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/http/client.rb', line 39

def build_request(verb, uri, opts = {}) # rubocop:disable Style/OptionHash
  opts    = @default_options.merge(opts)
  uri     = make_request_uri(uri, opts)
  headers = make_request_headers(opts)
  body    = make_request_body(opts, headers)
  proxy   = opts.proxy

  HTTP::Request.new(
    :verb         => verb,
    :uri          => uri,
    :headers      => headers,
    :proxy        => proxy,
    :body         => body,
    :auto_deflate => opts.feature(:auto_deflate)
  )
end

#closeObject



94
95
96
97
98
# File 'lib/http/client.rb', line 94

def close
  @connection.close if @connection
  @connection = nil
  @state = :clean
end

#perform(req, options) ⇒ Object

Perform a single (no follow) HTTP request



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/http/client.rb', line 62

def perform(req, options)
  verify_connection!(req.uri)

  @state = :dirty

  @connection ||= HTTP::Connection.new(req, options)

  unless @connection.failed_proxy_connect?
    @connection.send_request(req)
    @connection.read_headers!
  end

  res = Response.new(
    :status        => @connection.status_code,
    :version       => @connection.http_version,
    :headers       => @connection.headers,
    :proxy_headers => @connection.proxy_response_headers,
    :connection    => @connection,
    :encoding      => options.encoding,
    :auto_inflate  => options.feature(:auto_inflate),
    :uri           => req.uri
  )

  @connection.finish_response if req.verb == :head
  @state = :clean

  res
rescue
  close
  raise
end

#persistent?Boolean

Returns whenever client is persistent.

Returns:

  • (Boolean)

    whenever client is persistent

See Also:



59
# File 'lib/http/client.rb', line 59

def_delegator :default_options, :persistent?

#request(verb, uri, opts = {}) ⇒ Object

Make an HTTP request



27
28
29
30
31
32
33
34
35
36
# File 'lib/http/client.rb', line 27

def request(verb, uri, opts = {}) # rubocop:disable Style/OptionHash
  opts = @default_options.merge(opts)
  req = build_request(verb, uri, opts)
  res = perform(req, opts)
  return res unless opts.follow

  Redirector.new(opts.follow).perform(req, res) do |request|
    perform(request, opts)
  end
end