Class: PatientHttp::Request
- Inherits:
-
Object
- Object
- PatientHttp::Request
- Defined in:
- lib/patient_http/request.rb
Overview
Represents an async HTTP request that will be processed by the async processor.
Constant Summary collapse
- VALID_METHODS =
Valid HTTP methods
%i[get post put patch delete].freeze
Instance Attribute Summary collapse
-
#headers ⇒ HttpHeaders
readonly
Request headers.
-
#http_method ⇒ Symbol
readonly
HTTP method (:get, :post, :put, :patch, :delete).
-
#max_redirects ⇒ Integer?
readonly
Maximum number of redirects to follow (nil uses config default, 0 disables).
-
#timeout ⇒ Numeric?
readonly
Overall timeout in seconds.
-
#url ⇒ String
readonly
The request URL.
Class Method Summary collapse
-
.load(hash) ⇒ Request
Reconstruct a Request from a hash.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Serialize to JSON hash.
-
#body ⇒ String?
Returns the request body, decoding it from the payload if necessary.
-
#initialize(http_method, url, headers: {}, body: nil, json: nil, params: nil, timeout: nil, max_redirects: nil) ⇒ Request
constructor
Initializes a new Request.
Constructor Details
#initialize(http_method, url, headers: {}, body: nil, json: nil, params: nil, timeout: nil, max_redirects: nil) ⇒ Request
Initializes a new 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 96 97 98 |
# File 'lib/patient_http/request.rb', line 64 def initialize( http_method, url, headers: {}, body: nil, json: nil, params: nil, timeout: nil, max_redirects: nil ) @http_method = http_method.is_a?(String) ? http_method.downcase.to_sym : http_method unless url.nil? || url.is_a?(String) || url.is_a?(URI::Generic) raise ArgumentError.new("url must be a String or URI, got: #{url.class}") end @url = normalized_url(url, params) @headers = headers.is_a?(HttpHeaders) ? headers : HttpHeaders.new(headers) @body = (body == "") ? nil : body @timeout = timeout @max_redirects = max_redirects if json raise ArgumentError.new("Cannot provide both body and json") if @body @body = JSON.generate(json) @headers["content-type"] ||= "application/json; charset=utf-8" end validate! encoding, encoded_body, charset = Payload.encode(@body, @headers["content-type"]) @payload = Payload.new(encoding, encoded_body, charset) unless @body.nil? @body = UNDEFINED end |
Instance Attribute Details
#headers ⇒ HttpHeaders (readonly)
Returns Request headers.
29 30 31 |
# File 'lib/patient_http/request.rb', line 29 def headers @headers end |
#http_method ⇒ Symbol (readonly)
Returns HTTP method (:get, :post, :put, :patch, :delete).
23 24 25 |
# File 'lib/patient_http/request.rb', line 23 def http_method @http_method end |
#max_redirects ⇒ Integer? (readonly)
Returns Maximum number of redirects to follow (nil uses config default, 0 disables).
35 36 37 |
# File 'lib/patient_http/request.rb', line 35 def max_redirects @max_redirects end |
#timeout ⇒ Numeric? (readonly)
Returns Overall timeout in seconds.
32 33 34 |
# File 'lib/patient_http/request.rb', line 32 def timeout @timeout end |
#url ⇒ String (readonly)
Returns The request URL.
26 27 28 |
# File 'lib/patient_http/request.rb', line 26 def url @url end |
Class Method Details
.load(hash) ⇒ Request
Reconstruct a Request from a hash
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/patient_http/request.rb', line 42 def load(hash) new( hash["http_method"].to_sym, hash["url"], headers: hash["headers"], body: Payload.load(hash["body"])&.value, timeout: hash["timeout"], max_redirects: hash["max_redirects"] ) end |
Instance Method Details
#as_json ⇒ Hash
Serialize to JSON hash.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/patient_http/request.rb', line 111 def as_json { "http_method" => @http_method.to_s, "url" => @url.to_s, "headers" => @headers.to_h, "body" => @payload&.as_json, "timeout" => @timeout, "max_redirects" => @max_redirects } end |
#body ⇒ String?
Returns the request body, decoding it from the payload if necessary.
103 104 105 106 |
# File 'lib/patient_http/request.rb', line 103 def body @body = @payload&.value if @body.equal?(UNDEFINED) @body end |