Class: RelatonIho::IhoBibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_iho/iho_bibliography.rb

Constant Summary collapse

ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-iho/" \
"master/".freeze

Class Method Summary collapse

Class Method Details

.get(ref, year = nil, opts = {}) ⇒ RelatonIho::IhoBibliographicItem

Parameters:

  • ref (String)

    the IHO standard Code to look up (e..g “IHO B-11”)

  • year (String) (defaults to: nil)

    the year the standard was published (optional)

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :all_parts (TrueClass, FalseClass)

    restricted to all parts if all-parts reference is required

  • :bibdata (TrueClass, FalseClass)

Returns:



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

Parameters:

  • text (String)

    the IHO standard Code to look up (e..g “IHO B-11”)

Returns:



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.warn "(#{text}) Fetching from Relaton repository ..."
  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.warn "(#{text}) Not found."
    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.warn "(#{text}) Found: `#{item.docidentifier.first.id}`"
  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.message}"
end