Class: X::RedirectHandler
- Inherits:
-
Object
- Object
- X::RedirectHandler
- Defined in:
- lib/x/redirect_handler.rb
Constant Summary collapse
- DEFAULT_MAX_REDIRECTS =
10
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
-
#request_builder ⇒ Object
readonly
Returns the value of attribute request_builder.
Instance Method Summary collapse
- #handle(response:, request:, base_url:, authenticator: Authenticator.new, redirect_count: 0) ⇒ Object
-
#initialize(connection: Connection.new, request_builder: RequestBuilder.new, max_redirects: DEFAULT_MAX_REDIRECTS) ⇒ RedirectHandler
constructor
A new instance of RedirectHandler.
Constructor Details
#initialize(connection: Connection.new, request_builder: RequestBuilder.new, max_redirects: DEFAULT_MAX_REDIRECTS) ⇒ RedirectHandler
Returns a new instance of RedirectHandler.
15 16 17 18 19 20 |
# File 'lib/x/redirect_handler.rb', line 15 def initialize(connection: Connection.new, request_builder: RequestBuilder.new, max_redirects: DEFAULT_MAX_REDIRECTS) @connection = connection @request_builder = request_builder @max_redirects = max_redirects end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
13 14 15 |
# File 'lib/x/redirect_handler.rb', line 13 def connection @connection end |
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
12 13 14 |
# File 'lib/x/redirect_handler.rb', line 12 def max_redirects @max_redirects end |
#request_builder ⇒ Object (readonly)
Returns the value of attribute request_builder.
13 14 15 |
# File 'lib/x/redirect_handler.rb', line 13 def request_builder @request_builder end |
Instance Method Details
#handle(response:, request:, base_url:, authenticator: Authenticator.new, redirect_count: 0) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/x/redirect_handler.rb', line 22 def handle(response:, request:, base_url:, authenticator: Authenticator.new, redirect_count: 0) if response.is_a?(Net::HTTPRedirection) raise TooManyRedirects, "Too many redirects" if redirect_count > max_redirects new_uri = build_new_uri(response, base_url) new_request = build_request(request, new_uri, Integer(response.code), authenticator) new_response = connection.perform(request: new_request) handle(response: new_response, request: new_request, base_url: base_url, redirect_count: redirect_count + 1) else response end end |