lib/relaton_ieee/hit_collection.rb in relaton-ieee-1.9.4 vs lib/relaton_ieee/hit_collection.rb in relaton-ieee-1.10.0

- old
+ new

@@ -9,26 +9,30 @@ DATAFILE = File.expand_path "bibliography.json", DATADIR ETAGFILE = File.expand_path "etag.txt", DATADIR # rubocop:disable Metrics/AbcSize - # @param ref [Strig] + # @param reference [Strig] # @param opts [Hash] - def initialize(ref) # rubocop:disable Metrics/MethodLength + def initialize(reference) # rubocop:disable Metrics/MethodLength super - code = ref.sub(/^IEEE\s(Std\s)?/, "") - search = CGI.escape({ data: { searchTerm: code } }.to_json) - url = "#{DOMAIN}/bin/standards/search?data=#{search}" - resp = Faraday.get url - resp_json = JSON.parse resp.body - json = JSON.parse resp_json["message"] - @array = json["response"]["searchResults"]["resultsMapList"] - .reduce([]) do |s, hit| - /^(?:\w+\s)?(?<id>[A-Z\d.]+)(?:-(?<year>\d{4}))?/ =~ hit["record"]["recordTitle"] - next s unless id && code =~ %r{^#{id}} + 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}} - s << Hit.new(hit["record"].merge(code: id, year: year.to_i), self) - end.sort_by { |h| h.hit[:year].to_s + h.hit["recordURL"] }.reverse + 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