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)


33
34
35
# File 'lib/peddler/response.rb', line 33

def parser
  @parser
end

Class Method Details

.decorate(response, parser: nil) ⇒ ResponseDecorator

Decorates an HTTP::Response

Parameters:

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

    (if any)

Returns:

  • (ResponseDecorator)


25
26
27
28
29
# File 'lib/peddler/response.rb', line 25

def decorate(response, parser: nil)
  new(response).tap do |decorator|
    decorator.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:



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

def_delegator :to_h, :dig

#parseObject



35
36
37
# File 'lib/peddler/response.rb', line 35

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

#to_hHash

Converts the response body to a Hash

Returns:

  • (Hash)


42
43
44
# File 'lib/peddler/response.rb', line 42

def to_h
  (parser && parser.respond_to?(:to_h) ? parser : parse).to_h
end