Class: LunaPark::Http::Request
- Inherits:
-
Object
- Object
- LunaPark::Http::Request
- Defined in:
- lib/luna_park/http/request.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Body of http request (defaults is ‘nil`).
-
#headers ⇒ Object
Http request headers (defaults is ‘{}`).
-
#method(name = nil) ⇒ Object
Http method of current request, defines a set of request methods to indicate the desired action to be performed for a given resource.
-
#open_timeout ⇒ Object
Http open timeout, is the timeout for opening the connection.
-
#read_timeout ⇒ Object
Http read timeout, is the timeout for reading the answer.
-
#sent_at ⇒ Object
readonly
Time when request is sent.
-
#title ⇒ Object
Business description for this request, help you make the domain model more expressive.
-
#url ⇒ Object
Http url to send request.
Instance Method Summary collapse
-
#call ⇒ Object
Send current request (we cannot call this method ‘send` because it reserved word in ruby).
-
#call! ⇒ Object
Send the current request.
-
#driver ⇒ Object
This method return which driver are use, to send current request.
-
#initialize(title:, method: nil, url: nil, body: nil, headers: nil, open_timeout: nil, read_timeout: nil, driver:) ⇒ Request
constructor
Create new request.
-
#initialize_dup(_other) ⇒ Object
When object is duplicated, we should reset send timestamp.
- #inspect ⇒ Object
-
#sent? ⇒ Boolean
This method shows if this request has been already sent.
- #to_h ⇒ Object
Constructor Details
#initialize(title:, method: nil, url: nil, body: nil, headers: nil, open_timeout: nil, read_timeout: nil, driver:) ⇒ Request
Create new request
rubocop:disable Metrics/ParameterLists, Layout/LineLength
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/luna_park/http/request.rb', line 99 def initialize(title:, method: nil, url: nil, body: nil, headers: nil, open_timeout: nil, read_timeout: nil, driver:) @title = title @method = method @url = url @body = body @headers = headers @read_timeout = read_timeout @open_timeout = open_timeout @driver = driver @sent_at = nil end |
Instance Attribute Details
#body ⇒ Object
Body of http request (defaults is ‘nil`)
53 54 55 |
# File 'lib/luna_park/http/request.rb', line 53 def body @body end |
#headers ⇒ Object
Http request headers (defaults is ‘{}`)
59 60 61 |
# File 'lib/luna_park/http/request.rb', line 59 def headers @headers end |
#method(name = nil) ⇒ Object
Http method of current request, defines a set of request methods to indicate the desired action to be performed for a given resource
30 31 32 |
# File 'lib/luna_park/http/request.rb', line 30 def method(name = nil) name.nil? ? @method : super(name) end |
#open_timeout ⇒ Object
Http open timeout, is the timeout for opening the connection. This is useful if you are calling servers with slow or shaky response times. (defaults is 10)
77 78 79 |
# File 'lib/luna_park/http/request.rb', line 77 def open_timeout @open_timeout end |
#read_timeout ⇒ Object
Http read timeout, is the timeout for reading the answer. This is useful to make sure you will not get stuck half way in the reading process, or get stuck reading a 5 MB file when you’re expecting 5 KB of JSON (default is 10)
69 70 71 |
# File 'lib/luna_park/http/request.rb', line 69 def read_timeout @read_timeout end |
#sent_at ⇒ Object (readonly)
Time when request is sent
86 87 88 |
# File 'lib/luna_park/http/request.rb', line 86 def sent_at @sent_at end |
#title ⇒ Object
Business description for this request, help you make the domain model more expressive
19 20 21 |
# File 'lib/luna_park/http/request.rb', line 19 def title @title end |
#url ⇒ Object
Http url to send request
40 41 42 |
# File 'lib/luna_park/http/request.rb', line 40 def url @url end |
Instance Method Details
#call ⇒ Object
This method implements a facade pattern. And you better use it than call the Http::Send class directly.
Send current request (we cannot call this method ‘send` because it reserved word in ruby). It always return Response object, even if the server returned an error such as 404 or 502.
After sending the request, the object is frozen. You should dup object to resend request.
133 134 135 136 |
# File 'lib/luna_park/http/request.rb', line 133 def call @sent_at = Time.now driver.call(self).tap { freeze } end |
#call! ⇒ Object
This method implements a facade pattern. And you better use it than call the Http::Send class directly.
Send the current request. It returns a Response object only on a successful response. If the response failed, the call! method should raise an Erros::Http exception.
After call! request you cannot change request attributes.
After sending the request, the object is frozen. You should dup object to resend request.
160 161 162 163 |
# File 'lib/luna_park/http/request.rb', line 160 def call! @sent_at = Time.now driver.call!(self).tap { freeze } end |
#driver ⇒ Object
This method return which driver are use, to send current request.
179 180 181 |
# File 'lib/luna_park/http/request.rb', line 179 def driver @driver ||= self.class.default_driver end |
#initialize_dup(_other) ⇒ Object
When object is duplicated, we should reset send timestamp
166 167 168 169 |
# File 'lib/luna_park/http/request.rb', line 166 def initialize_dup(_other) super @sent_at = nil end |
#inspect ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/luna_park/http/request.rb', line 193 def inspect "<#{self.class.name} " \ "@title=#{title.inspect} " \ "@url=#{url.inspect} " \ "@method=#{method.inspect} " \ "@headers=#{headers.inspect} " \ "@body=#{body.inspect} " \ "@sent_at=#{sent_at.inspect}>" end |
#sent? ⇒ Boolean
This method shows if this request has been already sent.
174 175 176 |
# File 'lib/luna_park/http/request.rb', line 174 def sent? !@sent_at.nil? end |
#to_h ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/luna_park/http/request.rb', line 211 def to_h { title: title, method: method, url: url, body: body, headers: headers, read_timeout: read_timeout, open_timeout: open_timeout, sent_at: sent_at } end |