relaton-iec 1.7.4 → 1.7.5

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: 8a149738910c8542c434a360acc80e28b47d0a506610644c46c59ac0b5275bd4
4
- data.tar.gz: cef0a735b8ee7049e2f7efe845f60773c9bba620a236422ea52d4dee9b63e83a
3
+ metadata.gz: b87aa92de24cc0120f04f0325e2ec419c366b92d775beb1b37ab2cc429c96c1d
4
+ data.tar.gz: 28aad2f843cd822bca23dc56e0de8526da1993a5e51cab74a80e957d3a125710
5
5
  SHA512:
6
- metadata.gz: cf5dced7765bf8060ed5b0b1f73a404f930d2ce3bad4e4b7f500b66c74b24f9d255495e0a049ebd1a3cae99b9c0af4d5f52710f42d5f8eb8e07de2b32a2abf3b
7
- data.tar.gz: 7367a1efe7c9ed2e8d976051286c7d2672da7253c0943a00c9e9c100637aff3ece058abb7e69cb8ee74fddb91c908c9df2f1d58741c7a27f1afa513102068c85
6
+ metadata.gz: 261ffff809b27f6b6ff84fd936959819fe315cfe187bb0ecc97b5ae48295fcaa515668d66cb229d3cfbb3f31bcb5127112c5da9064f8699671ce50f40fe5df2b
7
+ data.tar.gz: a7fc694951f0b4b1a0f57c5d3ea9fcb89c4729d4b28dfe945c6d6c3c7add1be498e961bbf2bc71760b7c88034a0a2ad57295c6cd432ee365c9cb2beb0dcf0950
@@ -8,5 +8,9 @@ module RelatonIec
8
8
  def fetch
9
9
  @fetch ||= Scrapper.parse_page @hit
10
10
  end
11
+
12
+ def part
13
+ @part ||= hit[:code].match(/(?<=-)[\w-]+/)&.to_s
14
+ end
11
15
  end
12
16
  end
@@ -6,6 +6,10 @@ require "addressable/uri"
6
6
  module RelatonIec
7
7
  # Page of hit collection.
8
8
  class HitCollection < RelatonBib::HitCollection
9
+ def_delegators :@array, :detect
10
+
11
+ attr_reader :part
12
+
9
13
  DOMAIN = "https://webstore.iec.ch"
10
14
 
11
15
  # @param ref_nbr [String]
@@ -13,43 +17,58 @@ module RelatonIec
13
17
  # @param part [String, nil]
14
18
  def initialize(ref_nbr, year = nil, part = nil)
15
19
  super ref_nbr, year
16
- @array = hits ref_nbr, year, part
20
+ @part = part
21
+ @array = hits ref_nbr, year
22
+ end
23
+
24
+ # @return [RelatonIec::IecBibliographicItem]
25
+ def to_all_parts # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity
26
+ parts = @array.reject { |h| h.part.nil? }
27
+ hit = parts.min_by &:part
28
+ return @array.first.fetch lang unless hit
29
+
30
+ bibitem = hit.fetch
31
+ all_parts_item = bibitem.to_all_parts
32
+ parts.reject { |h| h.hit[:code] == hit.hit[:code] }.each do |hi|
33
+ isobib = RelatonIec::IecBibliographicItem.new(
34
+ formattedref: RelatonBib::FormattedRef.new(content: hi.hit[:code])
35
+ )
36
+ all_parts_item.relation << RelatonBib::DocumentRelation.new(type: "partOf", bibitem: isobib)
37
+ end
38
+ all_parts_item
17
39
  end
18
40
 
19
41
  private
20
42
 
21
43
  # @param ref [String]
22
44
  # @param year [String, nil]
23
- # @param part [String, nil]
24
45
  # @return [Array<RelatonIec::Hit>]
25
- def hits(ref, year, part)
46
+ def hits(ref, year)
26
47
  from, to = nil
27
48
  if year
28
49
  from = Date.strptime year, "%Y"
29
50
  to = from.next_year.prev_day
30
51
  end
31
- get_results ref, from, to, part
52
+ get_results ref, from, to
32
53
  end
33
54
 
34
55
  # @param ref [String]
35
56
  # @param from [Date, nil]
36
57
  # @param to [Date, nil]
37
- # @param part [String, nil]
38
58
  # @return [Array<RelatonIec::Hit>]
39
- def get_results(ref, from, to, part = nil)
59
+ def get_results(ref, from, to)
40
60
  code = part ? ref.sub(/(?<=-\d)\d+/, "*") : ref
41
61
  [nil, "trf", "wr"].reduce([]) do |m, t|
42
62
  url = "#{DOMAIN}/searchkey"
43
63
  url += "&type=#{t}" if t
44
64
  url += "&RefNbr=#{code}&From=#{from}&To=#{to}&start=1"
45
- m + results(Addressable::URI.parse(url).normalize, part)
65
+ m + results(Addressable::URI.parse(url).normalize)
46
66
  end
47
67
  end
48
68
 
49
69
  # @param url [String]
50
- # @param part [String, nil]
51
70
  # @return [Array<RelatonIec::Hit>]
52
- def results(uri, part)
71
+ def results(uri)
53
72
  contains = "[contains(.,'Part #{part}:')]" if part
54
73
  resp = OpenURI.open_uri(uri, "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) "\
55
74
  "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36")
@@ -32,23 +32,21 @@ module RelatonIec
32
32
  # reference is required
33
33
  # @return [String] Relaton XML serialisation of reference
34
34
  def get(code, year = nil, opts = {}) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
35
+ opts[:all_parts] ||= code.match? /\s\(all parts\)/
36
+ ref = code.sub /\s\(all parts\)/, ""
35
37
  if year.nil?
36
- /^(?<code1>[^:]+):(?<year1>[^:]+)/ =~ code
38
+ /^(?<code1>[^:]+):(?<year1>[^:]+)/ =~ ref
37
39
  unless code1.nil?
38
- code = code1
40
+ ref = code1
39
41
  year = year1
40
42
  end
41
43
  end
44
+ return iev if ref.casecmp("IEV").zero?
42
45
 
43
- return iev if code.casecmp("IEV").zero?
44
-
45
- opts[:all_parts] ||= !(code =~ / \(all parts\)/).nil?
46
- code = code.sub(/ \(all parts\)/, "")
47
- ret = iecbib_get1(code, year, opts)
46
+ ret = iecbib_get(ref, year, opts)
48
47
  return nil if ret.nil?
49
48
 
50
49
  ret = ret.to_most_recent_reference unless year || opts[:keep_year]
51
- ret = ret.to_all_parts if opts[:all_parts]
52
50
  ret
53
51
  end
54
52
 
@@ -77,17 +75,17 @@ module RelatonIec
77
75
  # @param hits [Array<RelatonIec::Hit>]
78
76
  # @param threads [Integer]
79
77
  # @return [Array<RelatonIec::Hit>]
80
- def fetch_pages(hits, threads)
81
- workers = RelatonBib::WorkersPool.new threads
82
- workers.worker { |w| { i: w[:i], hit: w[:hit].fetch } }
83
- hits.each_with_index { |hit, i| workers << { i: i, hit: hit } }
84
- workers.end
85
- workers.result.sort_by { |a| a[:i] }.map { |x| x[:hit] }
86
- end
87
-
88
- def isobib_search_filter(reference, year, opts) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
78
+ # def fetch_pages(hits, threads)
79
+ # workers = RelatonBib::WorkersPool.new threads
80
+ # workers.worker { |w| { i: w[:i], hit: w[:hit].fetch } }
81
+ # hits.each_with_index { |hit, i| workers << { i: i, hit: hit } }
82
+ # workers.end
83
+ # workers.result.sort_by { |a| a[:i] }.map { |x| x[:hit] }
84
+ # end
85
+
86
+ def search_filter(reference, year, opts) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
89
87
  %r{
90
- ^(?<code>(?:ISO|IEC)[^\d]*\s[\d-]+\w?)
88
+ ^(?<code>(?:ISO|IEC)[^\d]*\s\d+((?:-\w+)+)?)
91
89
  (:(?<year1>\d{4}))?
92
90
  (?<bundle>\+[^\s\/]+)?
93
91
  (\/(?<corr>AMD\s\d+))?
@@ -96,23 +94,24 @@ module RelatonIec
96
94
  corr&.sub! " ", ""
97
95
  warn "[relaton-iec] (\"#{reference}\") fetching..."
98
96
  result = search(code, year)
99
- if result.empty? && /(?<=-)(?<part>\d+)/ =~ code
97
+ if result.empty? && /(?<=-)(?<part>[\w-]+)/ =~ code
100
98
  # try to search packaged standard
101
99
  result = search code, year, part
102
- ref = code.sub /(?<=-\d)\d+/, ""
103
- else ref = code
100
+ # ref = code.sub /(?<=-\d)\w+/, ""
101
+ # else ref = code
104
102
  end
103
+ result = search code if result.empty?
104
+ code.sub! /((?:-\w+)+)/, ""
105
105
  result.select do |i|
106
106
  %r{
107
- ^(?<code2>(?:ISO|IEC)[^\d]*\s\d+(-\w+)?)
107
+ ^(?<code2>(?:ISO|IEC)[^\d]*\s\d+)((?:-\w+)+)?
108
108
  (:(?<year2>\d{4}))?
109
109
  (?<bundle2>\+[^\s\/]+)?
110
110
  (\/(?<corr2>AMD\d+))?
111
111
  }x =~ i.hit[:code]
112
- code2.sub! /(?<=-\d)\w*/, "" if part
113
- code2.sub! /-\d+\w*/, "" if opts[:all_parts]
114
- ref == code2 && (year.nil? || year == year2) && bundle == bundle2 &&
115
- corr == corr2
112
+ # code2.sub! /(?<=-\d)\w*/, "" if part
113
+ # code2.sub! /((?:-\w+)+)/, "" if opts[:all_parts]
114
+ code == code2 && bundle == bundle2 && corr == corr2 # (year.nil? || year == year2) &&
116
115
  end
117
116
  end
118
117
 
@@ -157,30 +156,56 @@ module RelatonIec
157
156
  # Does not match corrigenda etc (e.g. ISO 3166-1:2006/Cor 1:2007)
158
157
  # If no match, returns any years which caused mismatch, for error
159
158
  # reporting
160
- def isobib_results_filter(result, year) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
159
+ def results_filter(result, ref, year, opts) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
160
+ r_code, r_year = code_year ref, result.part
161
+ r_year ||= year
161
162
  missed_years = []
162
- result.each_slice(3) do |s| # ISO website only allows 3 connections
163
- fetch_pages(s, 3).each_with_index do |r, _i|
164
- return { ret: r } if !year
165
-
166
- r.date.select { |d| d.type == "published" }.each do |d|
167
- return { ret: r } if year.to_i == d.on(:year)
163
+ missed_parts = false
164
+ # result.each_slice(3) do |s| # ISO website only allows 3 connections
165
+ ret = if opts[:all_parts]
166
+ result.to_all_parts
167
+ else
168
+ result.detect do |h|
169
+ h_code, h_year = code_year h.hit[:code], result.part
170
+ missed_parts ||= !opts[:all_parts] && r_code != h_code
171
+ missed_years << h_year unless !r_year || h_year == r_year
172
+ r_code == h_code && (!year || h_year == r_year)
173
+ # fetch_pages(s, 3).each_with_index do |r, _i|
174
+ # return { ret: r } if !year
175
+
176
+ # r.date.select { |d| d.type == "published" }.each do |d|
177
+ # return { ret: r } if year.to_i == d.on(:year)
178
+
179
+ # missed_years << d.on(:year)
180
+ # end
181
+ # end
182
+ end&.fetch
183
+ end
184
+ { ret: ret, years: missed_years, missed_parts: missed_parts }
185
+ end
168
186
 
169
- missed_years << d.on(:year)
170
- end
171
- end
172
- end
173
- { years: missed_years }
187
+ def code_year(ref, part)
188
+ %r{
189
+ ^(?<code>(?:ISO|IEC)[^\d]*\s\d+((?:-\w+)+)?)
190
+ (:(?<year>\d{4}))?
191
+ }x =~ ref
192
+ code.sub!(/-\d+/, "") if part
193
+ [code, year]
174
194
  end
175
195
 
176
- def iecbib_get1(code, year, opts)
177
- return iev if code.casecmp("IEV").zero?
196
+ def iecbib_get(code, year, opts)
197
+ # return iev if code.casecmp("IEV").zero?
178
198
 
179
- result = isobib_search_filter(code, year, opts) || return
180
- ret = isobib_results_filter(result, year)
199
+ result = search_filter(code, year, opts) || return
200
+ ret = results_filter(result, code, year, opts)
181
201
  if ret[:ret]
182
- warn "[relaton-iec] (\"#{code}\") found "\
183
- "#{ret[:ret].docidentifier.first.id}"
202
+ if ret[:missed_parts]
203
+ warn "[relaton-iec] WARNING: #{code} found as #{ret[:ret].docidentifier.first.id} "\
204
+ "but also contain parts. If you wanted to cite all document parts for the reference, use "\
205
+ "\"#{code} (all parts)\""
206
+ else
207
+ warn "[relaton-iec] (\"#{code}\") found #{ret[:ret].docidentifier.first.id}"
208
+ end
184
209
  ret[:ret]
185
210
  else
186
211
  fetch_ref_err(code, year, ret[:years])
@@ -1,3 +1,3 @@
1
1
  module RelatonIec
2
- VERSION = "1.7.4".freeze
2
+ VERSION = "1.7.5".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-iec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.4
4
+ version: 1.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-17 00:00:00.000000000 Z
11
+ date: 2021-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: debase