Sha256: 1c60d41be55fd1427c825ab14a3202a9c0c2d8cdbc26d4fadc1589bbe0159535

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "delegate"
require "forwardable"

module Peddler
  # Wraps HTTP::Response to allow custom parsing
  class Response < SimpleDelegator
    extend Forwardable

    # @!method dig(*key)
    #   Delegates to the Hash returned by {ResponseDecorator#to_h} to extract a nested
    #   value specified by the sequence of keys
    #
    #   @param [String] key one or more keys
    #   @see https://ruby-doc.org/core/Hash.html#method-i-dig
    def_delegator :to_h, :dig

    class << self
      # Decorates an HTTP::Response
      #
      # @param [HTTP::Response] response
      # @param [nil, #call] parser (if any)
      # @return [ResponseDecorator]
      def decorate(response, parser: nil)
        new(response).tap do |decorator|
          decorator.parser = parser
        end
      end
    end

    # @return [#call]
    attr_accessor :parser

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

    # Converts the response body to a Hash
    #
    # @return [Hash]
    def to_h
      (parser && parser.respond_to?(:to_h) ? parser : parse).to_h
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
peddler-4.1.1 lib/peddler/response.rb
peddler-4.1.0 lib/peddler/response.rb
peddler-4.0.1 lib/peddler/response.rb
peddler-4.0.0 lib/peddler/response.rb