Sha256: ad068727b7b21849d8260e490f376128b4363cfc4a43475145644c5bf6a5532d

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module LinkHeaderParser
  class LinkHeadersCollection
    extend Forwardable

    include Enumerable

    def_delegators :members, :[], :<<, :each, :last, :length, :push

    attr_reader :headers

    # @param headers [Array<String>]
    # @param base [String]
    def initialize(*headers, base:)
      @headers = headers.to_ary.flatten.map(&:to_str)
      @base = base.to_str

      distinct_headers.each { |header| push(LinkHeader.new(header, base: base)) }
    end

    # @return [Hash{Symbol => Array<LinkHeaderParser::LinkHeader>}]
    def group_by_relation_type
      relation_types.map do |relation_type|
        [relation_type, find_all { |member| member.relation_types.include?(relation_type) }]
      end.to_h.transform_keys(&:to_sym)
    end

    def inspect
      format(%(#<#{self.class.name}:%#0x headers: #{headers.inspect}, relation_types: #{relation_types.inspect}>), object_id)
    end

    # @return [Array<String>]
    def relation_types
      @relation_types ||= flat_map(&:relation_types).uniq.sort
    end

    private

    attr_reader :base

    def distinct_headers
      @distinct_headers ||= headers.flat_map { |header| header.split(/,(?=[\s|<])/) }.map(&:strip)
    end

    def members
      @members ||= []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
link-header-parser-3.0.0 lib/link_header_parser/link_headers_collection.rb