Sha256: 64fb6be50c0ea5dd10d862b1f7d80244bc88c65e8636948ba25f6467914daf6c

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

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

module RelatonIec
  # Page of hit collection.
  class HitCollection < RelatonBib::HitCollection
    DOMAIN = "https://webstore.iec.ch"

    # @param ref_nbr [String]
    # @param year [String, nil]
    # @param part [String, nil]
    def initialize(ref_nbr, year = nil, part = nil)
      super ref_nbr, year
      @array = hits ref_nbr, year, part
    end

    private

    # @param ref [String]
    # @param year [String, nil]
    # @param part [String, nil]
    # @return [Array<RelatonIec::Hit>]
    def hits(ref, year, part)
      from, to = nil
      if year
        from = Date.strptime year, "%Y"
        to   = from.next_year.prev_day
      end
      get_results ref, from, to, part
    end

    # @param ref [String]
    # @param from [Date, nil]
    # @param to [Date, nil]
    # @param part [String, nil]
    # @return [Array<RelatonIec::Hit>]
    def get_results(ref, from, to, part = nil)
      code = part ? ref.sub(/(?<=-\d)\d+/, "*") : ref
      [nil, "trf", "wr"].reduce([]) do |m, t|
        url = "#{DOMAIN}/searchkey"
        url += "&type=#{t}" if t
        url += "&RefNbr=#{code}&From=#{from}&To=#{to}&start=1"
        m + results(Addressable::URI.parse(url).normalize, part)
      end
    end

    # @param url [String]
    # @param part [String, nil]
    # @return [Array<RelatonIec::Hit>]
    def results(uri, part)
      contains = "[contains(.,'Part #{part}:')]" if part
      Nokogiri::HTML(OpenURI.open_uri(uri)).xpath(
        "//body/li#{contains}",
        "//ul[contains(@class,'search-results')]/li#{contains}",
        "//ul[contains(@class,'morethesame')]/li#{contains}"
      ).map { |h| make_hit h }
    end

    def make_hit(hit)
      link  = hit.at('a[@href!="#"]')
      code  = link.text.tr [194, 160].pack("c*").force_encoding("UTF-8"), ""
      title = hit.xpath("text()").text.gsub(/[\r\n]/, "")
      Hit.new({ code: code, title: title, url: DOMAIN + link[:href] }, self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
relaton-iec-1.7.0 lib/relaton_iec/hit_collection.rb