Sha256: c5f16a685d376acf2bc9261980960d00ddffe4794447906cc7c01f637e5d9bdc

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Trav3
  class ResponseCollection
    extend Forwardable
    def_delegators :@collection, :count, :keys, :values, :has_key?, :key?
    def initialize(travis, collection)
      @travis = travis
      @collection = collection
    end

    def [](target)
      result = collection[target]
      return ResponseCollection.new(travis, result) if collection?(result)

      result
    end

    def dig(*target)
      return collection.dig(*target) if target.length != 1

      result = collection.dig(*target)
      return ResponseCollection.new(travis, result) if collection?(result)

      result
    end

    def each(&block)
      return collection.each(&block) if hash?

      collection.each do |item|
        yield ResponseCollection.new(travis, item)
      end
    end

    def fetch(idx)
      result = collection.fetch(idx) { nil }
      return ResponseCollection.new(travis, result) if collection?(result)
      return result if result

      # For error raising behavior
      collection.fetch(idx) unless block_given?

      yield
    end

    def first
      self[0]
    end

    def follow(idx = nil)
      if href? && !idx
        url = collection.fetch('@href')
        return travis.send(:get_path_with_opts, url)
      end

      result = fetch(idx)
      result.follow
    end

    def hash?
      collection.is_a? Hash
    end

    def last
      self[-1]
    end

    private

    def collection?(input)
      [Array, Hash].include? input.class
    end

    def href?
      collection.respond_to?(:key?) and collection.key?('@href')
    end

    attr_reader :travis
    attr_reader :collection
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trav3-0.3.0 lib/trav3/response/response_collection.rb