relaton 3.1.5 → 3.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ require "cgi"
2
+ require "mechanize"
3
+ require "openssl"
4
+ require "yeptris"
5
+
6
+ module Relaton
7
+ module Doi
8
+ #
9
+ # JSON GET for the DOI services other than the Crossref works API:
10
+ # doi.org's registration agency lookup and the DataCite REST API.
11
+ #
12
+ # A 404 answers nil. Any other failure retries twice, then raises
13
+ # Relaton::RequestError, so that Relaton::Db retries it and never caches a
14
+ # transport failure as "not found" (see docs/doi.md, "Rate limiting").
15
+ #
16
+ module Http
17
+ extend self
18
+
19
+ MAX_RETRIES = 2
20
+
21
+ # A transport failure: the service did not give a readable answer.
22
+ # A non-JSON 200 (a proxy's or a CDN's error page) is one too.
23
+ NETWORK_ERRORS = [
24
+ SocketError, Timeout::Error, SystemCallError, IOError, # IOError covers EOFError
25
+ OpenSSL::SSL::SSLError, Net::HTTP::Persistent::Error,
26
+ Mechanize::ResponseReadError, Mechanize::RedirectLimitReachedError,
27
+ Mechanize::RedirectNotGetOrHeadError, ::Yeptris::JSON::ParseError
28
+ ].freeze
29
+
30
+ MAX_RETRY_DELAY = 60
31
+
32
+ #
33
+ # @param [String] url
34
+ # @param [String] service the service name for the error message
35
+ #
36
+ # @return [Hash, Array, nil] the parsed body, or nil on a 404
37
+ #
38
+ def get_json(url, service) # rubocop:disable Metrics/MethodLength
39
+ attempt = 0
40
+ begin
41
+ Relaton::Json.parse_external agent.get(url).body
42
+ rescue Mechanize::ResponseCodeError => e
43
+ return nil if e.response_code == "404"
44
+
45
+ attempt += 1
46
+ fail_after_retries attempt, service, e.page.body
47
+ sleep backoff(e.page.response, attempt)
48
+ retry
49
+ rescue *NETWORK_ERRORS => e
50
+ attempt += 1
51
+ fail_after_retries attempt, service, e.message
52
+ sleep backoff({}, attempt)
53
+ retry
54
+ end
55
+ end
56
+
57
+ #
58
+ # Seconds to wait before retry `num`: the numeric `Retry-After` when the
59
+ # service sends one, else 1 second, times the attempt, at most
60
+ # MAX_RETRY_DELAY. A missing header never means "retry at once".
61
+ #
62
+ # @param [Hash, nil] headers
63
+ # @param [Integer] num
64
+ #
65
+ # @return [Integer]
66
+ #
67
+ def backoff(headers, num)
68
+ after = headers.to_h["retry-after"].to_s
69
+ seconds = after.match?(/\A\d+\z/) ? after.to_i : 1
70
+ [[seconds, 1].max * num, MAX_RETRY_DELAY].min
71
+ end
72
+
73
+ def agent
74
+ @agent ||= Mechanize.new { |a| a.user_agent = Crossref::USER_AGENT }
75
+ end
76
+
77
+ private
78
+
79
+ def fail_after_retries(attempt, service, detail)
80
+ return if attempt <= MAX_RETRIES
81
+
82
+ raise Relaton::RequestError, "#{service} error: #{detail}"
83
+ end
84
+ end
85
+ end
86
+ end
@@ -19,7 +19,7 @@ module Relaton
19
19
  # @return [RelatonBib::BibliographicItem]
20
20
  def get(code, _date, _opts)
21
21
  require_relative "../doi"
22
- Crossref.get(code)
22
+ Doi.get(code)
23
23
  end
24
24
 
25
25
  #
@@ -0,0 +1,30 @@
1
+ module Relaton
2
+ module Doi
3
+ #
4
+ # Which registration agency holds a DOI's metadata (relaton#577).
5
+ #
6
+ # Each DOI is registered with one agency, and only that agency's API has
7
+ # its metadata: a DataCite DOI (ResearchGate, Zenodo, data repositories) is
8
+ # a 404 at Crossref. doi.org answers the agency for any DOI.
9
+ #
10
+ module RegistrationAgency
11
+ extend self
12
+
13
+ URL = "https://doi.org/ra/%<doi>s".freeze
14
+
15
+ #
16
+ # @param [String] doi the bare DOI (`<prefix>/<suffix>`)
17
+ #
18
+ # @return [String, nil] the agency name (`Crossref`, `DataCite`,
19
+ # `mEDRA`, …), or nil when doi.org has no agency for the DOI
20
+ # (`DOI does not exist`, `Invalid DOI`)
21
+ #
22
+ # @raise [Relaton::RequestError] when doi.org does not answer
23
+ #
24
+ def lookup(doi)
25
+ answer = Http.get_json format(URL, doi: CGI.escape(doi)), "doi.org"
26
+ Array(answer).first&.dig("RA")
27
+ end
28
+ end
29
+ end
30
+ end
data/lib/relaton/doi.rb CHANGED
@@ -8,11 +8,56 @@ require_relative "version"
8
8
  require_relative "doi/util"
9
9
  require_relative "doi/parser"
10
10
  require_relative "doi/crossref"
11
+ require_relative "doi/http"
12
+ require_relative "doi/registration_agency"
13
+ require_relative "doi/datacite_parser"
14
+ require_relative "doi/datacite"
11
15
 
12
16
  module Relaton
13
17
  module Doi
14
18
  extend self
15
19
 
20
+ #
21
+ # Fetch a DOI from the registration agency that holds its metadata
22
+ # (relaton#577): doi.org names the agency, and the DOI goes to that
23
+ # agency's API. A transport failure of the lookup raises
24
+ # Relaton::RequestError, so a DOI is never cached as not found because
25
+ # doi.org did not answer.
26
+ #
27
+ # @param [String, Pubid::Doi::Identifier] doi
28
+ #
29
+ # @return [Bib::ItemData, nil]
30
+ #
31
+ def get(doi)
32
+ case agency = RegistrationAgency.lookup(doi_of(doi))
33
+ when "Crossref" then Crossref.get(doi)
34
+ when "DataCite" then Datacite.get(doi)
35
+ when nil
36
+ Util.info "Not found: doi.org has no registration agency for the DOI.", key: doi.to_s
37
+ nil
38
+ else
39
+ Util.info "Not found: the registration agency #{agency} is not supported.", key: doi.to_s
40
+ nil
41
+ end
42
+ end
43
+
44
+ #
45
+ # The DOI itself (`<prefix>/<suffix>`), as the agency APIs take it.
46
+ #
47
+ # A parsed pubid gives it from its components. A String may carry a
48
+ # `doi:` scheme (in any case) or a `doi.org` URL: Relaton::Db keys every
49
+ # form Pubid::Doi reads as one DOI, so all of them must reach the same
50
+ # DOI here, or a miss on one form is cached for the others.
51
+ #
52
+ # @param [String, Pubid::Doi::Identifier] doi
53
+ # @return [String]
54
+ #
55
+ def doi_of(doi)
56
+ return "#{doi.prefix}/#{doi.suffix}" unless doi.is_a?(String)
57
+
58
+ doi.sub(%r{\A(?:doi:|https?://(?:dx\.)?doi\.org/)}i, "")
59
+ end
60
+
16
61
  def grammar_hash
17
62
  Digest::MD5.hexdigest Relaton::VERSION
18
63
  end
@@ -69,6 +69,20 @@ Relaton::Iho::Bibliography.get "IHO B-11"
69
69
  ...
70
70
  ----
71
71
 
72
+ === Get an S-4 section
73
+
74
+ S-4 sections are records of their own, one per content state. A section
75
+ reference without an edition returns the current state. Add the edition to
76
+ get an earlier state.
77
+
78
+ [source,ruby]
79
+ ----
80
+ Relaton::Iho::Bibliography.get "S-4 Part B, Section 400" # current state
81
+ Relaton::Iho::Bibliography.get "IHO S-4 B-400" # same record
82
+ Relaton::Iho::Bibliography.get "S-4 Part B, Section 400, Edition 4.9.0"
83
+ Relaton::Iho::Bibliography.get "IHO S-4 4.9.0 B-400" # same record
84
+ ----
85
+
72
86
  === Typed links
73
87
 
74
88
  Each IHO document has `html`, `website`, or `pdf` type link.
@@ -14,7 +14,7 @@ module Relaton
14
14
  # @return [RelatonIho::IhoBibliographicItem, nil] the IHO standard or nil if not found
15
15
  #
16
16
  def search(text, _year = nil, _opts = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
17
- pubid = text.is_a?(String) ? ::Pubid::Iho::Identifier.parse(text) : text
17
+ pubid = text.is_a?(String) ? parse_reference(text) : text
18
18
  Util.info "Fetching from Relaton repository ...", key: pubid.to_s
19
19
  # Pass the pubid so Relaton::Index narrows candidates by number via
20
20
  # binary search before applying the block. Every row's `:id` is a
@@ -44,6 +44,48 @@ module Relaton
44
44
  raise Relaton::RequestError, "Could not access #{uri}: #{e.message}"
45
45
  end
46
46
 
47
+ # The long-form edition qualifier of an S-4 section citation
48
+ # (relaton#531): "S-4 Part B, Section 400, Edition 4.9.0".
49
+ EDITION_QUALIFIER = /,?\s+Edition\s+(?<edition>\d+(?:\.\d+)*)\s*\z/
50
+
51
+ # Parse a reference string into a Pubid::Iho identifier. Pubid's
52
+ # grammar reads every IHO face except the trailing ", Edition N"
53
+ # qualifier of the long section form, so that qualifier is split
54
+ # off here and set on the edition slot the record is indexed under
55
+ # (see #edition_slot).
56
+ def parse_reference(text)
57
+ ::Pubid::Iho::Identifier.parse(text)
58
+ rescue ::Pubid::Errors::ParseError
59
+ match = EDITION_QUALIFIER.match(text)
60
+ raise unless match
61
+
62
+ pubid = ::Pubid::Iho::Identifier.parse(match.pre_match)
63
+ pubid.public_send(:"#{edition_slot(pubid)}=", match[:edition])
64
+ pubid
65
+ end
66
+
67
+ # The independently-publishable components whose records carry
68
+ # their own edition (mirrors the relaton-data-iho crawler).
69
+ COMPONENT_EDITION_SLOTS = {
70
+ part: :part_edition,
71
+ supplement: :supplement_edition,
72
+ annex: :annex_edition,
73
+ appendix: :appendix_edition,
74
+ volume: :volume_edition,
75
+ }.freeze
76
+
77
+ # The attribute an edition lands on. An S-4 section or spec
78
+ # citation rides its publication's edition ("IHO S-4 4.10.0
79
+ # B-400", pubid#691), so it takes the base edition even though it
80
+ # names a part; any other component published separately carries
81
+ # its own edition (relaton#440).
82
+ def edition_slot(pubid)
83
+ return :edition if pubid.respond_to?(:spec) && pubid.spec
84
+
85
+ COMPONENT_EDITION_SLOTS.find { |comp, _| pubid.public_send(comp) }&.last ||
86
+ :edition
87
+ end
88
+
47
89
  # Populate ext.structuredidentifier from the parsed Pubid when the
48
90
  # fetched record doesn't already provide one. Maps `pubid.number`
49
91
  # (with type prefix, e.g. `S-100`) -> docnumber, `pubid.part` -> part,
@@ -25,6 +25,17 @@ module Relaton
25
25
  Bibliography.get(code, date, opts)
26
26
  end
27
27
 
28
+ # The long section form with an edition qualifier ("S-4 Part B,
29
+ # Section 400, Edition 4.9.0", relaton#531) is read by the flavor,
30
+ # not by pubid's grammar, so the Db cache key comes from the same
31
+ # parse `get` uses.
32
+ def cache_pubid(ref)
33
+ require_relative "../iho"
34
+ Bibliography.parse_reference(ref)
35
+ rescue ArgumentError, ::Pubid::Errors::Error
36
+ super
37
+ end
38
+
28
39
  # An IHO record prints `B-12` and keeps its edition in `edition`, so
29
40
  # the edition goes on the key (relaton#408). The component model
30
41
  # (relaton#440) renamed the pubid attribute `version` -> `edition`.
@@ -57,6 +57,7 @@ module Relaton
57
57
  else
58
58
  ret = hits.fetch_doc(date_filter ? opts : {})
59
59
  end
60
+ ret = check_tc_document_year(ret, query_pubid, missed_year_ids)
60
61
  return fetch_ref_err(query_pubid, missed_year_ids, tip_ids) unless ret
61
62
 
62
63
  response_pubid = ret.docidentifier.find(&:primary) # .sub(" (all parts)", "")
@@ -115,7 +116,7 @@ module Relaton
115
116
  if hit.pubid.date&.year.nil? && hit.hit[:year]
116
117
  hit.pubid.date = ::Pubid::Components::Date.new(year: hit.hit[:year].to_s)
117
118
  end
118
- next true if check_year(year, hit)
119
+ next true if check_year(year, hit) || undated_tc_document?(hit)
119
120
 
120
121
  missed_year_ids << hit.pubid.to_s if hit.pubid.date&.year
121
122
  false
@@ -126,6 +127,45 @@ module Relaton
126
127
 
127
128
  private
128
129
 
130
+ # An ISO/TC committee document ("ISO/TC 184/SC 4 N3500") is identified
131
+ # by its N-number alone: relaton-data-iso's curated `static/` rows carry
132
+ # no year, and the year a citation gives ("…N3500:2020") is the
133
+ # document's publication date (relaton-iso#139). Such a hit passes the
134
+ # year filter, and `check_tc_document_year` checks the year against the
135
+ # fetched document.
136
+ # @param hit [Relaton::Iso::Hit]
137
+ def undated_tc_document?(hit)
138
+ hit.pubid.is_a?(::Pubid::Iso::Identifiers::TcDocument) &&
139
+ hit.pubid.date&.year.nil? && hit.hit[:year].nil?
140
+ end
141
+
142
+ # @param item [Relaton::Iso::ItemData, nil] the fetched document
143
+ # @param query_pubid [Pubid::Iso::Identifier]
144
+ # @param missed_year_ids [Set<String>] gets the dated id that exists, for the tip
145
+ # @return [Relaton::Iso::ItemData, nil] nil when the document's year is
146
+ # not the query's, or the document gives no year to check
147
+ def check_tc_document_year(item, query_pubid, missed_year_ids)
148
+ year = query_pubid.root.date&.year
149
+ return item unless item && year && query_pubid.is_a?(::Pubid::Iso::Identifiers::TcDocument)
150
+
151
+ doc_year = tc_document_year(item)
152
+ return item if doc_year == year.to_s
153
+
154
+ docid = item.docidentifier.find(&:primary) || item.docidentifier.first
155
+ missed_year_ids << "#{docid.to_s.sub(/:\d{4}\z/, '')}:#{doc_year}" if doc_year
156
+ nil
157
+ end
158
+
159
+ # The year of a committee document: from its docidentifier when that is
160
+ # dated, else from its published date.
161
+ # @param item [Relaton::Iso::ItemData]
162
+ # @return [String, nil]
163
+ def tc_document_year(item)
164
+ docid = item.docidentifier.find(&:primary) || item.docidentifier.first
165
+ docid.to_s[/:(\d{4})\z/, 1] ||
166
+ item.date&.find { |d| d.type == "published" }&.at.to_s[/\d{4}/]
167
+ end
168
+
129
169
  # Filter out relations and dates that fall outside the date range.
130
170
  # @param item [Relaton::Iso::ItemData]
131
171
  # @param opts [Hash]
@@ -225,6 +265,8 @@ module Relaton
225
265
  # @return [Integer]
226
266
  def hit_year(hit)
227
267
  yr = hit.pubid&.date&.year || hit.hit[:year] || hit.pubid&.root&.date&.year
268
+ # An undated committee-document row: the year is on the document.
269
+ yr ||= hit.item && tc_document_year(hit.item) if hit.pubid && undated_tc_document?(hit)
228
270
  yr.to_i
229
271
  end
230
272
 
@@ -310,7 +352,12 @@ module Relaton
310
352
  # still found nothing): its own `part` is always nil (never delegated),
311
353
  # so read the underlying document via `#root`, and skip suggesting
312
354
  # "(all parts)" to a caller who already asked for it.
313
- if pubid.all_parts || pubid.root.part
355
+ if pubid.is_a?(::Pubid::Iso::Identifiers::TcDocument)
356
+ # relaton-iso#139: a committee document has no parts.
357
+ Util.info "TIP: ISO/TC committee documents are not public; relaton finds " \
358
+ "only those in the curated `static/` set of relaton-data-iso. " \
359
+ "Encode the reference in the bibliography by hand.", key: pubid.to_s
360
+ elsif pubid.all_parts || pubid.root.part
314
361
  Util.info "TIP: If it cannot be found, the document may no longer be published in parts.", key: pubid.to_s
315
362
  else
316
363
  Util.info "TIP: If you wish to cite all document parts for the reference, " \
@@ -44,6 +44,18 @@ module Relaton
44
44
  fold_year_on_root pubid, year
45
45
  end
46
46
 
47
+ # A committee document (`ISO/TC 184/SC 4 N3500`) prints no year: a
48
+ # dated fetch keeps its year only in the published date, so the year
49
+ # goes on the key. Otherwise the dated item overwrites the undated
50
+ # reference's cache file (relaton-iso#139, relaton#408). The undated
51
+ # form (`to_most_recent_reference`) has no date and keeps its own key.
52
+ def item_cache_key(pubid, item)
53
+ return pubid unless pubid.is_a?(::Pubid::Iso::Identifiers::TcDocument) && pubid.date.nil?
54
+
55
+ year = item.date&.find { |d| d.type == "published" }&.at.to_s[/\d{4}/]
56
+ year ? fold_year(pubid, year) : pubid
57
+ end
58
+
47
59
  # @param xml [String]
48
60
  # @return [RelatonIsoBib::IsoBibliographicItem]
49
61
  def from_xml(xml)
@@ -1,3 +1,3 @@
1
1
  module Relaton
2
- VERSION = "3.1.5".freeze
2
+ VERSION = "3.1.6".freeze
3
3
  end
data/lib/relaton-cli.rb CHANGED
@@ -12,6 +12,5 @@ end
12
12
 
13
13
  require_relative "relaton/bibcollection"
14
14
  require_relative "relaton/bibdata"
15
- require_relative "relaton/element_finder"
16
15
  require_relative "relaton/cli/yaml_convertor"
17
16
  require_relative "relaton/cli"
data/lib/relaton.rb CHANGED
@@ -27,6 +27,7 @@ module Relaton
27
27
  autoload :Bib, "relaton/bib"
28
28
  autoload :ThreeGpp, "relaton/3gpp"
29
29
  autoload :Adobe, "relaton/adobe"
30
+ autoload :Arxiv, "relaton/arxiv"
30
31
  autoload :Bipm, "relaton/bipm"
31
32
  autoload :Calconnect, "relaton/calconnect"
32
33
  autoload :Ccsds, "relaton/ccsds"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.5
4
+ version: 3.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-16 00:00:00.000000000 Z
11
+ date: 2026-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -520,14 +520,14 @@ dependencies:
520
520
  requirements:
521
521
  - - "~>"
522
522
  - !ruby/object:Gem::Version
523
- version: 2.0.25
523
+ version: 2.0.44
524
524
  type: :runtime
525
525
  prerelease: false
526
526
  version_requirements: !ruby/object:Gem::Requirement
527
527
  requirements:
528
528
  - - "~>"
529
529
  - !ruby/object:Gem::Version
530
- version: 2.0.25
530
+ version: 2.0.44
531
531
  - !ruby/object:Gem::Dependency
532
532
  name: yeptris
533
533
  requirement: !ruby/object:Gem::Requirement
@@ -639,6 +639,13 @@ files:
639
639
  - lib/relaton/adobe/item_data.rb
640
640
  - lib/relaton/adobe/processor.rb
641
641
  - lib/relaton/adobe/util.rb
642
+ - lib/relaton/arxiv.rb
643
+ - lib/relaton/arxiv/README.adoc
644
+ - lib/relaton/arxiv/api.rb
645
+ - lib/relaton/arxiv/bibliography.rb
646
+ - lib/relaton/arxiv/parser.rb
647
+ - lib/relaton/arxiv/processor.rb
648
+ - lib/relaton/arxiv/util.rb
642
649
  - lib/relaton/bib.rb
643
650
  - lib/relaton/bib/README.adoc
644
651
  - lib/relaton/bib/converter/asciibib.rb
@@ -879,6 +886,7 @@ files:
879
886
  - lib/relaton/cli/xml_to_html_renderer.rb
880
887
  - lib/relaton/cli/yaml_convertor.rb
881
888
  - lib/relaton/cloud.rb
889
+ - lib/relaton/collection_xml.rb
882
890
  - lib/relaton/core.rb
883
891
  - lib/relaton/core/README.adoc
884
892
  - lib/relaton/core/array_wrapper.rb
@@ -903,8 +911,12 @@ files:
903
911
  - lib/relaton/doi.rb
904
912
  - lib/relaton/doi/README.adoc
905
913
  - lib/relaton/doi/crossref.rb
914
+ - lib/relaton/doi/datacite.rb
915
+ - lib/relaton/doi/datacite_parser.rb
916
+ - lib/relaton/doi/http.rb
906
917
  - lib/relaton/doi/parser.rb
907
918
  - lib/relaton/doi/processor.rb
919
+ - lib/relaton/doi/registration_agency.rb
908
920
  - lib/relaton/doi/util.rb
909
921
  - lib/relaton/easc.rb
910
922
  - lib/relaton/easc/bibdata.rb
@@ -936,7 +948,6 @@ files:
936
948
  - lib/relaton/ecma/processor.rb
937
949
  - lib/relaton/ecma/standard_parser.rb
938
950
  - lib/relaton/ecma/util.rb
939
- - lib/relaton/element_finder.rb
940
951
  - lib/relaton/equivalence.rb
941
952
  - lib/relaton/etsi.rb
942
953
  - lib/relaton/etsi/README.adoc
@@ -1,29 +0,0 @@
1
- module Relaton
2
- module ElementFinder
3
- attr_reader :document
4
-
5
- def find_text(xpath, element = nil)
6
- find(xpath, element)&.text
7
- end
8
-
9
- def find_html(xpath, element = nil)
10
- find(xpath, element)&.inner_html
11
- end
12
-
13
- def find(xpath, element = nil)
14
- (element || document).at(apply_namespace(xpath))
15
- end
16
-
17
- def find_xpath(xpath, element = nil)
18
- element&.xpath(apply_namespace(xpath))
19
- end
20
-
21
- def apply_namespace(xpath)
22
- xpath
23
- .gsub(%r{/([a-zA-Z])}, "/xmlns:\\1")
24
- .gsub(%r{::([a-zA-Z])}, "::xmlns:\\1")
25
- .gsub(%r{\[([a-zA-Z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1")
26
- .gsub(%r{\[([a-zA-Z][a-z0-9A-Z@/]*\])}, "[xmlns:\\1")
27
- end
28
- end
29
- end