Class: RelatonIho::IhoBibliography
- Inherits:
-
Object
- Object
- RelatonIho::IhoBibliography
- Defined in:
- lib/relaton_iho/iho_bibliography.rb
Constant Summary collapse
- ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-iho/" \ "main/".freeze
Class Method Summary collapse
- .get(ref, year = nil, opts = {}) ⇒ RelatonIho::IhoBibliographicItem
-
.search(text, _year = nil, _opts = {}) ⇒ RelatonIho::IhoBibliographicItem?
Search for IHO standard by IHO standard Code.
Class Method Details
.get(ref, year = nil, opts = {}) ⇒ RelatonIho::IhoBibliographicItem
54 55 56 |
# File 'lib/relaton_iho/iho_bibliography.rb', line 54 def get(ref, year = nil, opts = {}) search(ref, year, opts) end |
.search(text, _year = nil, _opts = {}) ⇒ RelatonIho::IhoBibliographicItem?
Search for IHO standard by IHO standard Code
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/relaton_iho/iho_bibliography.rb', line 16 def search(text, _year = nil, _opts = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize Util.info "Fetching from Relaton repository ...", key: text ref = text.sub(/^IHO\s/, "").sub(/^([[:alpha:]]+)(\d+)/, '\1-\2') index = Relaton::Index.find_or_create :iho, url: "#{ENDPOINT}index.zip" row = index.search(ref).max_by { |r| r[:id] } unless row Util.info "Not found.", key: text return end uri = URI("#{ENDPOINT}#{row[:file]}") resp = Net::HTTP.get_response uri unless resp.code == "200" raise RelatonBib::RequestError, "Could not access #{uri}: HTTP #{resp.code}" end yaml = RelatonBib.parse_yaml resp.body, [Date] hash = HashConverter.hash_to_bib yaml hash[:fetched] = Date.today.to_s item = IhoBibliographicItem.new(**hash) Util.info "Found: `#{item.docidentifier.first.id}`", key: text item rescue SocketError, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, Net::ReadTimeout, OpenSSL::SSL::SSLError, Errno::ETIMEDOUT => e raise RelatonBib::RequestError, "Could not access #{uri}: #{e.}" end |