relaton-bsi 1.8.3 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "mechanize"
3
+ # require "mechanize"
4
4
  require "relaton_iso_bib"
5
5
  require "relaton_bsi/bsi_bibliographic_item"
6
6
  require "relaton_bsi/scrapper"
@@ -20,8 +20,8 @@ module RelatonBsi
20
20
  HitCollection.new code, year
21
21
  rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
22
22
  EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
23
- Net::ProtocolError
24
- raise RelatonBib::RequestError, "Could not access #{HitCollection::DOMAIN}"
23
+ Net::ProtocolError, Algolia::AlgoliaUnreachableHostError => e
24
+ raise RelatonBib::RequestError, e.message
25
25
  end
26
26
 
27
27
  # @param code [String] the BSI standard Code to look up
@@ -68,8 +68,6 @@ module RelatonBsi
68
68
 
69
69
  result = search(code)
70
70
  result.select do |i|
71
- next true unless i.hit[:code]
72
-
73
71
  %r{^(?<code2>[^:]+)} =~ i.hit[:code]
74
72
  code2.include?(code1)
75
73
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "algolia"
3
4
  require "relaton_bsi/hit"
4
5
 
5
6
  module RelatonBsi
@@ -8,29 +9,39 @@ module RelatonBsi
8
9
  DOMAIN = "https://shop.bsigroup.com"
9
10
 
10
11
  # @return [Mechanize]
11
- attr_reader :agent
12
+ # attr_reader :agent
12
13
 
13
14
  # @param ref [String]
14
15
  # @param year [String]
15
16
  def initialize(ref, year = nil)
16
17
  super ref, year
17
- @agent = Mechanize.new
18
- resp = agent.get "#{DOMAIN}/SearchResults/?q=#{ref}"
19
- @array = hits resp
18
+ # @agent = Mechanize.new
19
+ # resp = agent.get "#{DOMAIN}/SearchResults/?q=#{ref}"
20
+ config = Algolia::Search::Config.new(application_id: "575YE157G9", api_key: "a057b4e74099445df2eddb7940828a10")
21
+ client = Algolia::Search::Client.new config, logger: ::Logger.new($stderr)
22
+ index = client.init_index "shopify_products"
23
+ resp = index.search text # , facetFilters: "product_type:standard"
24
+ @array = hits resp[:hits]
20
25
  end
21
26
 
22
27
  private
23
28
 
24
- # @param resp [Mechanize::Page]
29
+ # @param hits [Array<Hash>]
25
30
  # @return [Array<RelatonBsi::Hit>]
26
- def hits(resp)
27
- resp.xpath("//div[@class='resultsInd']").map do |h|
28
- ref = h.at('div/h2/a')
29
- code = ref.text.strip
30
- url = ref[:href]
31
- d = h.at("//div/strong[.='Published Date:']/following-sibling::strong").text
32
- date = Date.parse(d)
33
- Hit.new({ code: code, url: url, date: date }, self)
31
+ def hits(hits) # rubocop:disable Metrics/MethodLength
32
+ hits.map do |h|
33
+ Hit.new(
34
+ {
35
+ code: h[:meta][:global][:primaryDesignator],
36
+ title: h[:title],
37
+ url: h[:handle],
38
+ date: h[:meta][:global][:publishedDate],
39
+ publisher: h[:meta][:global][:publisher],
40
+ status: h[:meta][:global][:status],
41
+ ics: h[:meta][:global][:icsCodesAlgoliaStringArray],
42
+ doctype: h[:product_type],
43
+ }, self
44
+ )
34
45
  end
35
46
  end
36
47
  end