Class: Cybertrain::HttpClient
- Inherits:
-
Object
- Object
- Cybertrain::HttpClient
- Defined in:
- cybertrain/http_client.rb
Overview
Sends GET and POST requests to other servers, with bounded waits.
Defined Under Namespace
Classes: BadResponseError, BlockedAddressError, Error, RefusedError, Response, TLSError, TimeoutError, TooLargeError
Instance Attribute Summary collapse
-
#allow_private ⇒ Boolean
readonly
Whether non-public destinations are allowed.
-
#deadline ⇒ Float
readonly
Seconds the whole request may take.
-
#max_body ⇒ Integer
readonly
The largest body accepted, in bytes.
-
#timeout ⇒ Float
readonly
Seconds any one wait may take.
Instance Method Summary collapse
-
#get(url, headers: NO_HEADERS) ⇒ Response
GET
url. -
#initialize(timeout: 5, deadline: 15, max_body: 1_048_576, allow_private: Cybertrain.config.http_allow_private) ⇒ HttpClient
constructor
A new instance of HttpClient.
-
#post(url, body, content_type: "application/json", headers: NO_HEADERS) ⇒ Response
POST
bodytourl.
Constructor Details
#initialize(timeout: 5, deadline: 15, max_body: 1_048_576, allow_private: Cybertrain.config.http_allow_private) ⇒ HttpClient
Returns a new instance of HttpClient.
184 185 186 187 188 189 190 191 192 |
# File 'cybertrain/http_client.rb', line 184 def initialize(timeout: 5, deadline: 15, max_body: 1_048_576, allow_private: Cybertrain.config.http_allow_private) @timeout = timeout.to_f @deadline = deadline.to_f @max_body = max_body.to_i @allow_private = allow_private == true raise ArgumentError, "timeout must be positive" unless @timeout > 0.0 raise ArgumentError, "deadline must be positive" unless @deadline > 0.0 raise ArgumentError, "max_body must be at least 0" if @max_body < 0 end |
Instance Attribute Details
#allow_private ⇒ Boolean (readonly)
Returns whether non-public destinations are allowed.
173 174 175 |
# File 'cybertrain/http_client.rb', line 173 def allow_private @allow_private end |
#deadline ⇒ Float (readonly)
Returns seconds the whole request may take.
167 168 169 |
# File 'cybertrain/http_client.rb', line 167 def deadline @deadline end |
#max_body ⇒ Integer (readonly)
Returns the largest body accepted, in bytes.
170 171 172 |
# File 'cybertrain/http_client.rb', line 170 def max_body @max_body end |
#timeout ⇒ Float (readonly)
Returns seconds any one wait may take.
164 165 166 |
# File 'cybertrain/http_client.rb', line 164 def timeout @timeout end |
Instance Method Details
#get(url, headers: NO_HEADERS) ⇒ Response
GET url.
200 201 202 |
# File 'cybertrain/http_client.rb', line 200 def get(url, headers: NO_HEADERS) perform("GET", url, "", "", headers) end |
#post(url, body, content_type: "application/json", headers: NO_HEADERS) ⇒ Response
POST body to url.
212 213 214 |
# File 'cybertrain/http_client.rb', line 212 def post(url, body, content_type: "application/json", headers: NO_HEADERS) perform("POST", url, body.to_s, content_type.to_s, headers) end |