Sha256: 15d2a36e41cb5dd0708ee04f9142105a7184e1781e5886d3670d0608e87eb4d6

Contents?: true

Size: 1.3 KB

Versions: 20

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Isobib
  # Hit.
  class Hit
    # @return [Isobib::HitCollection]
    attr_reader :hit_collection

    # @return [Array<Hash>]
    attr_reader :hit

    # @param hit [Hash]
    # @param hit_collection [Isobib:HitCollection]
    def initialize(hit, hit_collection = nil)
      @hit            = hit
      @hit_collection = hit_collection
    end

    # Parse page.
    # @return [Isobib::IsoBibliographicItem]
    def fetch
      @fetch ||= Scrapper.parse_page @hit
    end

    # @return [String]
    def to_s
      inspect
    end

    # @return [String]
    def inspect
      matched_words = @hit['_highlightResult']
                      .inject([]) { |a, (_k, v)| a + v['matchedWords'] }.uniq

      "<#{self.class}:#{format('%#.14x', object_id << 1)} "\
      "@text=\"#{@hit_collection&.hit_pages&.text}\" "\
      "@fullIdentifier=\"#{@fetch&.shortref}\" "\
      "@matchedWords=#{matched_words} "\
      "@category=\"#{@hit['category']}\" "\
      "@title=\"#{@hit['title']}\">"
    end

    # @return [String]
    def to_xml(opts = {})
      #if builder
        #fetch.to_xml builder, opts
      #else
        builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
          fetch.to_xml xml, opts
        end
        builder.doc.root.to_xml
      #end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
isobib-0.4.4 lib/isobib/hit.rb
isobib-0.4.3 lib/isobib/hit.rb
isobib-0.4.2 lib/isobib/hit.rb
isobib-0.4.1 lib/isobib/hit.rb
isobib-0.4.0 lib/isobib/hit.rb
isobib-0.3.1 lib/isobib/hit.rb
isobib-0.3.0 lib/isobib/hit.rb
isobib-0.2.6 lib/isobib/hit.rb
isobib-0.2.5 lib/isobib/hit.rb
isobib-0.2.3 lib/isobib/hit.rb
isobib-0.2.2 lib/isobib/hit.rb
isobib-0.2.1 lib/isobib/hit.rb
isobib-0.2.0 lib/isobib/hit.rb
isobib-0.1.10 lib/isobib/hit.rb
isobib-0.1.9 lib/isobib/hit.rb
isobib-0.1.8 lib/isobib/hit.rb
isobib-0.1.7 lib/isobib/hit.rb
isobib-0.1.6 lib/isobib/hit.rb
isobib-0.1.5 lib/isobib/hit.rb
isobib-0.1.4 lib/isobib/hit.rb