Class: Peddler::Response

Inherits:
SimpleDelegator
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#parser#call

Returns:

  • (#call)


80
81
82
# File 'lib/peddler/response.rb', line 80

def parser
  @parser
end

Class Method Details

.decorateObject

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

Parameters:

  • response (HTTP::Response)
  • parser (nil, #call) (defaults to: nil)

    (if any)

Returns:

Raises:

  • (Error)

    if response status indicates an error (>= 400)



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

Parameters:

  • key (String)

    one or more keys

See Also:



20
# File 'lib/peddler/response.rb', line 20

def_delegator :to_h, :dig

#parseObject



82
83
84
# File 'lib/peddler/response.rb', line 82

def parse
  parser ? parser.call(__getobj__) : __getobj__.parse
end

#to_hHash

Converts the response body to a Hash

Returns:

  • (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