Sha256: 3e21faddd2082e9538331bd013e1875176f6516ed36954d811e80d60ab71ad7d

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require "faraday"
require "relaton_ieee/hit"
require "fileutils"

module RelatonIeee
  class HitCollection < RelatonBib::HitCollection
    DOMAIN = "https://standards.ieee.org".freeze
    DATADIR = File.expand_path ".relaton/ogc/", Dir.home
    DATAFILE = File.expand_path "bibliography.json", DATADIR
    ETAGFILE = File.expand_path "etag.txt", DATADIR

    # rubocop:disable Metrics/AbcSize

    # @param reference [Strig]
    # @param opts [Hash]
    def initialize(reference) # rubocop:disable Metrics/MethodLength
      super
      code1 = reference.sub(/^IEEE\s(Std\s)?/, "")
      url = "#{DOMAIN}/wp-admin/admin-ajax.php"
      query = reference.gsub("/", " ")
      resp = Faraday.post url, { action: "ieee_cloudsearch", q: query }
      json = JSON.parse resp.body
      html = Nokogiri::HTML json["html"]
      # @array = json["results"]["hits"]["hit"].reduce([]) do |s, hit|
      @array = html.xpath("//h4/a").reduce([]) do |s, hit|
        # fields = hit["fields"]
        # ref = html.at("//h4/a[@href='#{fields['doc_id_l']}']").text.strip
        ref = hit.text.strip
        /^(?:\w+\s)?(?<code2>[A-Z\d.]+)(?:-(?<year>\d{4}))?/ =~ ref
        next s unless code2 && code1 =~ %r{^#{code2}}

        hit_data = { ref: ref, year: year.to_i, url: hit[:href] }
        s << Hit.new(hit_data, self)
      end.sort_by { |h| h.hit[:year].to_s + h.hit[:url] }.reverse
    end
    # rubocop:enable Metrics/AbcSize
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
relaton-ieee-1.10.0 lib/relaton_ieee/hit_collection.rb