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
Class Method Summary collapse
-
.decorate ⇒ Object
deprecated
Deprecated.
Use Response.wrap instead
-
.wrap(response, parser: nil) ⇒ Response
Wraps an HTTP::Response with parsing capabilities.
Instance Method Summary collapse
-
#dig(*key) ⇒ Object
Delegates to the Hash returned by ResponseDecorator#to_h to extract a nested value specified by the sequence of keys.
- #parse ⇒ Object
-
#to_h ⇒ Hash
Converts the response body to a Hash.
Instance Attribute Details
#parser ⇒ #call
80 81 82 |
# File 'lib/peddler/response.rb', line 80 def parser @parser end |
Class Method Details
.decorate ⇒ Object
Deprecated.
Use wrap instead
52 53 54 55 |
# File 'lib/peddler/response.rb', line 52 def decorate(...) warn("Response.decorate is deprecated and will be removed in v5.0. Use Response.wrap instead.", uplevel: 1) wrap(...) end |
.wrap(response, parser: nil) ⇒ Response
Wraps an HTTP::Response with parsing capabilities
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/peddler/response.rb', line 29 def wrap(response, parser: nil) # Check for HTTP errors and raise custom Peddler errors if response.status >= 400 # Client errors (4xx) always raise if response.status < 500 error = Error.build(response) raise error || Error.new(response.status, response) # Server errors (5xx) - check configuration elsif Peddler.raise_on_server_errors error = Error.build(response) raise error || Error.new(response.status, response) else # Emit deprecation warning for v4 behavior warn_about_server_error_handling end end new(response).tap do |wrapper| wrapper.parser = parser end end |
Instance Method Details
#dig(*key) ⇒ Object
Delegates to the Hash returned by ResponseDecorator#to_h to extract a nested value specified by the sequence of keys
20 |
# File 'lib/peddler/response.rb', line 20 def_delegator :to_h, :dig |
#parse ⇒ Object
82 83 84 |
# File 'lib/peddler/response.rb', line 82 def parse parser ? parser.call(__getobj__) : __getobj__.parse end |
#to_h ⇒ Hash
Converts the response body to a Hash
89 90 91 |
# File 'lib/peddler/response.rb', line 89 def to_h (parser && parser.respond_to?(:to_h) ? parser : parse).to_h end |