Sha256: 097fec7105e9f33926b74aa742b0e23c4b0148da375e9f644b4f8f03b884005b

Contents?: true

Size: 786 Bytes

Versions: 1

Compression:

Stored size: 786 Bytes

Contents

# frozen_string_literal: true

require 'delegate'
require 'forwardable'
require 'json'

module Vacuum
  # A wrapper around the API response
  class Response < SimpleDelegator
    extend Forwardable

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

    class << self
      attr_accessor :parser
    end

    def_delegator :to_h, :dig

    attr_writer :parser

    def parser
      @parser || self.class.parser
    end

    def parse
      parser ? parser.parse(body) : to_h
    end

    # Casts body to Hash
    # @return [Hash]
    def to_h
      JSON.parse(body)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vacuum-3.2.0 lib/vacuum/response.rb