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

Constructor Details

#initialize(http_response, parser: nil) ⇒ Response

Creates a new Response wrapper

Parameters:

  • http_response (HTTP::Response)

    The HTTP response to wrap

  • parser (nil, #parse) (defaults to: nil)

    Optional parser for the response



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

#parsernil, #parse (readonly)

Returns Optional parser for the response.

Returns:

  • (nil, #parse)

    Optional parser for the response



15
16
17
# File 'lib/peddler/response.rb', line 15

def parser
  @parser
end

Class Method Details

.decorateObject

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

Parameters:

  • response (HTTP::Response)

    The HTTP response to wrap

  • parser (nil, #parse) (defaults to: nil)

    Optional parser for the response

Returns:

Raises:



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

Parameters:

  • key (String)

    one or more keys

See Also:



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

def_delegator :to_h, :dig

#parseObject



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

Parameters:

  • raise_on_server_errors (Boolean) (defaults to: Peddler.raise_on_server_errors)

    Whether to raise on 5xx errors (defaults to Peddler config)

Raises:



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_hHash

Converts the response body to a Hash

Returns:

  • (Hash)


61
62
63
# File 'lib/peddler/response.rb', line 61

def to_h
  __getobj__.parse
end