Sha256: fe08af4f9aa1fdd0f561d6386ccb59a53372e06e6223cb5705ea27f9f9e90a3a

Contents?: true

Size: 1.58 KB

Versions: 18

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require "relaton_iec/hit"
require "addressable/uri"

module RelatonIec
  # Page of hit collection.
  class HitCollection < Array

    DOMAIN = "https://webstore.iec.ch"

    # @return [TrueClass, FalseClass]
    attr_reader :fetched

    # @return [String]
    attr_reader :text

    # @return [String]
    attr_reader :year

    # @param ref_nbr [String]
    # @param year [String]
    def initialize(ref_nbr, year = nil) #(text, hit_pages = nil)
      @text = ref_nbr
      @year = year
      from, to = nil
      if year
        from = Date.strptime year, "%Y"
        to   = from.next_year.prev_day
      end
      url  = "#{DOMAIN}/searchkey&RefNbr=#{ref_nbr}&From=#{from}&To=#{to}&start=1"
      doc  = Nokogiri::HTML OpenURI.open_uri(::Addressable::URI.parse(url).normalize)
      hits = doc.css("ul.search-results > li").map do |h|
        link  = h.at('a[@href!="#"]')
        code  = link.text.tr [194, 160].pack("c*").force_encoding("UTF-8"), ""
        title = h.xpath("text()").text.gsub(/[\r\n]/, "")
        url   = DOMAIN + link[:href]
        Hit.new({ code: code, title: title, url: url }, self)
      end
      concat hits
      @fetched = false
    end

    # @return [RelatonIec::HitCollection]
    def fetch
      workers = RelatonBib::WorkersPool.new 4
      workers.worker(&:fetch)
      each do |hit|
        workers << hit
      end
      workers.end
      workers.result
      @fetched = true
      self
    end

    def to_s
      inspect
    end

    def inspect
      "<#{self.class}:#{format('%#.14x', object_id << 1)} @fetched=#{@fetched}>"
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
relaton-iec-0.8.0 lib/relaton_iec/hit_collection.rb
relaton-iec-0.7.0 lib/relaton_iec/hit_collection.rb
relaton-iec-0.6.0 lib/relaton_iec/hit_collection.rb
relaton-iec-0.5.1 lib/relaton_iec/hit_collection.rb
relaton-iec-0.5.0 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.12 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.11 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.10 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.9 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.8 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.6 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.4 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.3 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.2 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.1 lib/relaton_iec/hit_collection.rb
relaton-iec-0.4.0 lib/relaton_iec/hit_collection.rb
relaton-iec-0.3.2 lib/relaton_iec/hit_collection.rb
relaton-iec-0.3.1 lib/relaton_iec/hit_collection.rb