Class: Peddler::Response
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Peddler::Response
- Extended by:
- Forwardable
- Defined in:
- lib/peddler/response.rb
Overview
Wraps HTTP::Response to allow custom parsing
Instance Attribute Summary collapse
-
#parser ⇒ nil, #parse
readonly
Optional parser for the response.
Class Method Summary collapse
-
.decorate ⇒ Object
deprecated
Deprecated.
Use new instead
-
.wrap(response, parser: nil) ⇒ Response
Wraps an HTTP response with error checking.
Instance Method Summary collapse
-
#dig(*key) ⇒ Object
Delegates to the Hash returned by #to_h to extract a nested value specified by the sequence of keys.
-
#initialize(http_response, parser: nil) ⇒ Response
constructor
Creates a new Response wrapper.
- #parse ⇒ Object
-
#raise_for_status!(raise_on_server_errors: Peddler.raise_on_server_errors) ⇒ void
Raises an error if the HTTP response status indicates failure.
-
#to_h ⇒ Hash
Converts the response body to a Hash.
Constructor Details
#initialize(http_response, parser: nil) ⇒ Response
Creates a new Response wrapper
49 50 51 52 |
# File 'lib/peddler/response.rb', line 49 def initialize(http_response, parser: nil) super(http_response) @parser = parser end |
Instance Attribute Details
#parser ⇒ nil, #parse (readonly)
Returns Optional parser for the response.
15 16 17 |
# File 'lib/peddler/response.rb', line 15 def parser @parser end |
Class Method Details
.decorate ⇒ Object
Deprecated.
Use new instead
39 40 41 42 |
# File 'lib/peddler/response.rb', line 39 def decorate(...) warn("Response.decorate is deprecated and will be removed in v5.0. Use Response.new instead.", uplevel: 1) new(...) end |
.wrap(response, parser: nil) ⇒ Response
Wraps an HTTP response with error checking
32 33 34 35 36 |
# File 'lib/peddler/response.rb', line 32 def wrap(response, parser: nil) wrapped = new(response, parser:) wrapped.raise_for_status! wrapped end |
Instance Method Details
#dig(*key) ⇒ Object
Delegates to the Hash returned by #to_h to extract a nested value specified by the sequence of keys
23 |
# File 'lib/peddler/response.rb', line 23 def_delegator :to_h, :dig |
#parse ⇒ Object
54 55 56 |
# File 'lib/peddler/response.rb', line 54 def parse parser ? parser.parse(to_h) : to_h end |
#raise_for_status!(raise_on_server_errors: Peddler.raise_on_server_errors) ⇒ void
This method returns an undefined value.
Raises an error if the HTTP response status indicates failure
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/peddler/response.rb', line 70 def raise_for_status!(raise_on_server_errors: Peddler.raise_on_server_errors) return if status < 400 # Client errors (4xx) always raise if status < 500 error = Error.build(__getobj__) raise error || Error.new(status, __getobj__) # Server errors (5xx) - check configuration elsif raise_on_server_errors error = Error.build(__getobj__) raise error || Error.new(status, __getobj__) else # Emit deprecation warning for v4 behavior warn_about_server_error_handling end end |
#to_h ⇒ Hash
Converts the response body to a Hash
61 62 63 |
# File 'lib/peddler/response.rb', line 61 def to_h __getobj__.parse end |