relaton-bsi 1.11.0 → 1.11.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3111f0bbfe4aae50b70dbe3b55e5b3b8111eded6d6e808ced57dc1cec958115b
4
- data.tar.gz: 4b78d3666be1b4a5c765d18617112fe11735ab3e6323d26d8e96936e73c0ef65
3
+ metadata.gz: e6526b98460dccb16324ffdbc6cea2309b8e1eda6b5c06ed8272c4bae289b5fa
4
+ data.tar.gz: 4d64f8b75a735c5ccfbacf3f03816b62ebd22ea4cb2d564ebbb67b87643fbaa2
5
5
  SHA512:
6
- metadata.gz: f99e7b8408752f33397540fd8b4094505af023a47090ea737a80d4c723146eb97b0858c7bec74677a05a0824168bdfd4151c0e05985905907e2f85367d6b880b
7
- data.tar.gz: c14c76eaca4b46d77af4876d085d694118c19ea7226d40c8d171bd44bdb66f9295e98815e3af29876ce1a4ae4c3042a2715697102721402d49e2b096a422ccf4
6
+ metadata.gz: b640a7761c3eab1336b54904b31ce8b68477b1736bce1ec2d5b1f4775f8f2c31d9b35e9608e48a922c957e62000c4860f1fecfcb743d6602f2becc5446a7ebab
7
+ data.tar.gz: 1f80f9e3e3a9a0f613bf239e57b53bb8c9a0e189e841d11d9668a479d959eaa1accbd78a8e6ede2e1a78103a04d2658417eed19ab92712ac693ae1921aafe10d
@@ -35,7 +35,7 @@ module RelatonBsi
35
35
  def get(code, year = nil, opts = {})
36
36
  # y = code.split(":")[1]
37
37
  year ||= code_parts(code)[:year]
38
- ret = bib_get1(code, year, opts)
38
+ ret = bib_get(code, year, opts)
39
39
  return nil if ret.nil?
40
40
 
41
41
  ret = ret.to_most_recent_reference unless year || opts[:keep_year]
@@ -43,10 +43,27 @@ module RelatonBsi
43
43
  ret
44
44
  end
45
45
 
46
+ #
47
+ # Destruct code to its parts.
48
+ #
49
+ # @param [String] code document identifier
50
+ #
51
+ # @return [MatchData] parts of the code
52
+ #
53
+ def code_parts(code)
54
+ %r{
55
+ ^(?:BSI\s)?(?<code>(?:[A-Z]+\s)*[^:\s+]+)
56
+ (?::(?<year>\d{4}))?
57
+ (?:\+(?<a>[^:\s]+)(?::(?<y>\d{4}))?)?
58
+ (?:\s(?<rest>.+))?
59
+ }x.match code
60
+ end
61
+
46
62
  private
47
63
 
48
64
  def fetch_ref_err(code, year, missed_years) # rubocop:disable Metrics/MethodLength
49
- id = year ? "#{code}:#{year}" : code
65
+ y = code_parts(code)[:year]
66
+ id = year && !y ? "#{code}:#{year}" : code
50
67
  warn "[relaton-bsi] WARNING: no match found online for #{id}. "\
51
68
  "The code must be exactly like it is on the standards website."
52
69
  unless missed_years.empty?
@@ -65,29 +82,19 @@ module RelatonBsi
65
82
  nil
66
83
  end
67
84
 
68
- def search_filter(code) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
69
- # %r{^(?:BSI\s)?(?<code1>[^:]+)} =~ code
70
- # %r{^(?<code1>[^:]+)} =~ code.sub(/^BSI\s/, "")
71
- cp1 = code_parts code
85
+ #
86
+ # Search for a BSI standard.
87
+ #
88
+ # @param [String] code the BSI standard Code to look up
89
+ #
90
+ # @return [RelatonBsi::HitCollection] a collection of hits
91
+ #
92
+ def search_filter(code)
93
+ cp = code_parts code
72
94
  warn "[relaton-bsi] (\"#{code}\") fetching..."
73
- return [] unless cp1
74
-
75
- result = search(code)
76
- result.select do |i|
77
- # %r{^(?<code2>[^:]+)} =~ i.hit[:code]
78
- cp2 = code_parts i.hit[:code]
79
- cp2[:code] == cp1[:code] && (!cp1[:a] || cp2[:a] == cp1[:a]) &&
80
- (!cp1[:y] || cp2[:y] == cp1[:y])
81
- end
82
- end
95
+ return [] unless cp
83
96
 
84
- def code_parts(code)
85
- %r{
86
- ^(?:BSI\s)?(?<code>[^:]+)
87
- (?::(?<year>\d{4}))?
88
- (?:\+(?<a>[^:]+):)?
89
- (?::(?<y>\d{4}))?
90
- }x.match code
97
+ search(code).filter_hits!(cp)
91
98
  end
92
99
 
93
100
  # Sort through the results from Isobib, fetching them three at a time,
@@ -96,10 +103,10 @@ module RelatonBsi
96
103
  # Only expects the first page of results to be populated.
97
104
  # Does not match corrigenda etc (e.g. ISO 3166-1:2006/Cor 1:2007)
98
105
  # If no match, returns any years which caused mismatch, for error reporting
99
- def isobib_results_filter(result, year)
106
+ def results_filter(result, year)
100
107
  missed_years = []
101
108
  result.each do |r|
102
- /:(?<pyear>\d{4})/ =~ r.hit[:code]
109
+ pyear = code_parts(r.hit[:code])[:year]
103
110
  if !year || year == pyear
104
111
  ret = r.fetch
105
112
  return { ret: ret } if ret
@@ -110,9 +117,9 @@ module RelatonBsi
110
117
  { years: missed_years }
111
118
  end
112
119
 
113
- def bib_get1(code, year, _opts)
120
+ def bib_get(code, year, _opts)
114
121
  result = search_filter(code) || return
115
- ret = isobib_results_filter(result, year)
122
+ ret = results_filter(result, year)
116
123
  if ret[:ret]
117
124
  warn "[relaton-bsi] (\"#{code}\") found #{ret[:ret].docidentifier.first&.id}"
118
125
  ret[:ret]
@@ -8,8 +8,12 @@ module RelatonBsi
8
8
  class HitCollection < RelatonBib::HitCollection
9
9
  DOMAIN = "https://shop.bsigroup.com"
10
10
 
11
- # @param ref [String]
12
- # @param year [String]
11
+ #
12
+ # Initialize a new HitCollection.
13
+ #
14
+ # @param ref [String] reference
15
+ # @param year [String] year
16
+ #
13
17
  def initialize(ref, year = nil)
14
18
  super ref, year
15
19
  config = Algolia::Search::Config.new(
@@ -19,19 +23,39 @@ module RelatonBsi
19
23
  client = Algolia::Search::Client.new config, logger: ::Logger.new($stderr)
20
24
  index = client.init_index "shopify_products"
21
25
  resp = index.search text # , facetFilters: "product_type:standard"
22
- @array = hits resp[:hits]
26
+ @array = create_hits resp[:hits]
27
+ end
28
+
29
+ #
30
+ # Filter the search results for a BSI standard.
31
+ #
32
+ # @param [MatchData] code_parts parts of document identifier
33
+ #
34
+ # @return [self] filtered search results
35
+ #
36
+ def filter_hits!(code_parts)
37
+ hits = filter code_parts
38
+ hits = filter code_parts, skip_rest: true if hits.empty?
39
+ hits = filter code_parts, drop_amd: true if hits.empty?
40
+ @array = hits
41
+ self
23
42
  end
24
43
 
25
44
  private
26
45
 
27
- # @param hits [Array<Hash>]
28
- # @return [Array<RelatonBsi::Hit>]
29
- def hits(hits) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
46
+ #
47
+ # Create hits from search results.
48
+ #
49
+ # @param hits [Array<Hash>] search results
50
+ #
51
+ # @return [Array<RelatonBsi::Hit>] hits
52
+ #
53
+ def create_hits(hits) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
30
54
  hits.each_with_object([]) do |h, obj|
31
55
  next unless h[:meta][:global][:publishedDate]
32
56
 
33
57
  code = h[:meta][:global][:primaryDesignator]
34
- .sub(/\s(?:LOOSELEAF|\(A5 LAMINATED\)|-\sTC$)/, "")
58
+ .sub(/\s?(?:LOOSELEAF|\(A5 LAMINATED\)|-\s?TC$)/, "")
35
59
  obj << Hit.new(
36
60
  {
37
61
  code: code,
@@ -46,5 +70,24 @@ module RelatonBsi
46
70
  )
47
71
  end.sort_by { |h| h.hit[:date] }.reverse
48
72
  end
73
+
74
+ #
75
+ # Select hits that match the document identifier.
76
+ #
77
+ # @param [MatchData] code_parts parts of document identifier
78
+ # @param [Boolean] drop_amd drop amendments and corrigendums
79
+ # @param [Boolean] skip_rest skip rest suffix of document identifier
80
+ #
81
+ def filter(code_parts, drop_amd: false, skip_rest: false) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
82
+ @array.select do |i|
83
+ code = drop_amd ? i.hit[:code].sub(/\+[AC]\d+.*$/, "") : i.hit[:code]
84
+ cp = BsiBibliography.code_parts code
85
+ match = cp[:code] == code_parts[:code] && cp[:a] == code_parts[:a] &&
86
+ (!code_parts[:y] || cp[:y] == code_parts[:y]) &&
87
+ (skip_rest || cp[:rest] == code_parts[:rest])
88
+ i.hit[:code] = code if drop_amd && match
89
+ match
90
+ end
91
+ end
49
92
  end
50
93
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RelatonBsi
4
- VERSION = "1.11.0"
4
+ VERSION = "1.11.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-bsi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-10 00:00:00.000000000 Z
11
+ date: 2022-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: equivalent-xml