Class: HTTP::Client
- Inherits:
-
Object
- Object
- HTTP::Client
- Extended by:
- Forwardable
- Includes:
- Chainable
- Defined in:
- lib/http/client.rb
Overview
Clients make requests and receive responses
Direct Known Subclasses
Constant Summary collapse
- HTTP_OR_HTTPS_RE =
%r{^https?://}i
Instance Method Summary collapse
-
#build_request(verb, uri, opts = {}) ⇒ Object
Prepare an HTTP request.
- #close ⇒ Object
-
#initialize(default_options = {}) ⇒ Client
constructor
A new instance of Client.
-
#perform(req, options) ⇒ Object
Perform a single (no follow) HTTP request.
-
#persistent? ⇒ Boolean
Whenever client is persistent.
-
#request(verb, uri, opts = {}) ⇒ Object
Make an HTTP request.
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, #retriable, #timeout, #trace, #use, #via
Methods included from Base64
Constructor Details
Instance Method Details
#build_request(verb, uri, opts = {}) ⇒ Object
Prepare an HTTP request
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/http/client.rb', line 40 def build_request(verb, uri, opts = {}) opts = @default_options.merge(opts) uri = make_request_uri(uri, opts) headers = make_request_headers(opts) body = make_request_body(opts, headers) req = HTTP::Request.new({ verb: verb, uri: uri, uri_normalizer: opts.feature(:normalize_uri)&.normalizer, proxy: opts.proxy, headers: headers, body: body }) wrap_request(req, opts) end |
#close ⇒ Object
97 98 99 100 101 |
# File 'lib/http/client.rb', line 97 def close @connection&.close @connection = nil @state = :clean end |
#perform(req, options) ⇒ Object
Perform a single (no follow) HTTP request
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 93 94 95 |
# File 'lib/http/client.rb', line 64 def perform(req, ) verify_connection!(req.uri) @state = :dirty begin @connection ||= HTTP::Connection.new(req, ) unless @connection.failed_proxy_connect? @connection.send_request(req) @connection.read_headers! end rescue Error => e .features.each_value do |feature| feature.on_error(req, e) end raise end res = build_response(req, ) res = .features.values.reverse.inject(res) do |response, feature| feature.wrap_response(response) end @connection.finish_response if req.verb == :head @state = :clean res rescue close raise end |
#persistent? ⇒ Boolean
Returns whenever client is persistent.
61 |
# File 'lib/http/client.rb', line 61 def_delegator :default_options, :persistent? |
#request(verb, uri, opts = {}) ⇒ Object
Make an HTTP request
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/http/client.rb', line 28 def request(verb, uri, opts = {}) 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(wrap_request(request, opts), opts) end end |