Class: RelatonOasis::OasisBibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_oasis/oasis_bibliography.rb

Overview

Class methods for search Cenelec standards.

Constant Summary collapse

ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-oasis/main/"
INDEX_FILE =
"index-v1.yaml"

Class Method Summary collapse

Class Method Details

.get(code, year = nil, _opts = {}) ⇒ RelatonOasis::OasisBibliographicItem?

Parameters:

  • code (String)

    the CEN standard Code to look up

  • year (String) (defaults to: nil)

    the year the standard was published (optional)

  • opts (Hash)

    options; restricted to :all_parts if all-parts reference is required

Returns:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/relaton_oasis/oasis_bibliography.rb', line 38

def get(code, year = nil, _opts = {}) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  Util.info "Fetching from Relaton repository ...", key: code
  bibitem = search code, year
  if bibitem
    docid = bibitem.docidentifier.detect(&:primary).id
    Util.info "Found: `#{docid}`", key: code
  else
    Util.info "Not found.", key: code
  end
  bibitem
end

.search(text, _year = nil) ⇒ RelatonOasis::HitCollection

Parameters:

  • text (String)

Returns:

  • (RelatonOasis::HitCollection)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/relaton_oasis/oasis_bibliography.rb', line 12

def search(text, _year = nil) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  /^(?:OASIS\s)?(?<code>.+)/ =~ text
  index = Relaton::Index.find_or_create(
    :oasis, url: "#{ENDPOINT}index-v1.zip", file: INDEX_FILE
  )
  row = index.search(code).min_by { |i| i[:id] }
  return unless row

  agent = Mechanize.new
  resp = agent.get "#{ENDPOINT}#{row[:file]}"
  return unless resp.code == "200"

  hash = YAML.safe_load resp.body
  hash["fetched"] = Date.today.to_s
  OasisBibliographicItem.from_hash hash
rescue Mechanize::ResponseCodeError, OpenURI::HTTPError => e
  return if e.response_code == "404"

  raise RelatonBib::RequestError, e.message
end