Class: RelatonIso::IsoBibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_iso/iso_bibliography.rb

Overview

Class methods for search ISO standards.

Class Method Summary collapse

Class Method Details

.filter_hits_by_year(hit_collection, year) ⇒ RelatonIso::HitCollection

Parameters:

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/relaton_iso/iso_bibliography.rb', line 89

def filter_hits_by_year(hit_collection, year) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  missed_years = []
  return { hits: hit_collection, missed_years: missed_years } if year.nil?

  # filter by year
  hits = hit_collection.select do |hit|
    if hit.pubid.year == year
      true
    elsif hit.pubid.year.nil? && hit.hit[:year].to_s == year
      hit.pubid.year = year
      true
    else
      missed_year = hit.pubid.year || hit.hit[:year].to_s
      if missed_year && !missed_year.empty? && !missed_years.include?(missed_year)
        missed_years << missed_year
      end
      false
    end
  end

  { hits: hits, missed_years: missed_years }
end

.get(ref, year = nil, opts = {}) ⇒ RelatonIsoBib::IsoBibliographicItem

Returns Relaton XML serialisation of reference.

Parameters:

  • ref (String)

    the ISO standard Code to look up (e..g “ISO 9000”)

  • year (String, NilClass) (defaults to: nil)

    the year the standard was published

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

    options; restricted to :all_parts if all-parts

Options Hash (opts):

  • :all_parts (Boolean)

    if all-parts reference is required

  • :keep_year (Boolean)

    if undated reference should return actual reference with year

Returns:

  • (RelatonIsoBib::IsoBibliographicItem)

    Relaton XML serialisation of reference



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/relaton_iso/iso_bibliography.rb', line 31

def get(ref, year = nil, opts = {}) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity,Metrics/AbcSize
  code = ref.gsub(/\u2013/, "-")

  # parse "all parts" request
  code.sub! " (all parts)", ""
  opts[:all_parts] ||= $~ && opts[:all_parts].nil?

  query_pubid = Pubid::Iso::Identifier.parse(code)
  query_pubid.year = year if year

  resp = isobib_search_filter(query_pubid, opts)

  # return only first one if not all_parts
  ret = if !opts[:all_parts] || resp[:hits].size == 1
          resp[:hits].any? && resp[:hits].first.fetch(opts[:lang])
        else
          resp[:hits].to_all_parts(opts[:lang])
        end

  if ret
    warn "[relaton-iso] (\"#{query_pubid}\") found #{ret.docidentifier.first.id}"
  else
    return fetch_ref_err(query_pubid)
  end

  if (query_pubid.year && opts[:keep_year].nil?) || opts[:keep_year] || opts[:all_parts]
    ret
  else
    ret.to_most_recent_reference
  end
rescue Pubid::Core::Errors::ParseError
  warn "[relaton-iso] (\"#{code}\") is not recognized as a standards identifier."
end

.matches_base?(query_pubid, pubid, any_types_stages: false) ⇒ Boolean

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics?PerceivedComplexity

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/relaton_iso/iso_bibliography.rb', line 78

def matches_base?(query_pubid, pubid, any_types_stages: false) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics?PerceivedComplexity
  query_pubid.publisher == pubid.publisher &&
    query_pubid.number == pubid.number &&
    query_pubid.copublisher == pubid.copublisher &&
    ((any_types_stages && query_pubid.stage.nil?) || query_pubid.stage == pubid.stage) &&
    ((any_types_stages && query_pubid.type.nil?) || query_pubid.type == pubid.type)
end

.matches_parts?(query_pubid, pubid, all_parts: false) ⇒ Boolean

Parameters:

  • query_pubid (Pubid::Iso::Identifier)
  • pubid (Pubid::Iso::Identifier)
  • all_parts (Boolean) (defaults to: false)

    match with any parts when true

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
# File 'lib/relaton_iso/iso_bibliography.rb', line 69

def matches_parts?(query_pubid, pubid, all_parts: false)
  if all_parts
    # match only with documents with part number
    !pubid.part.nil?
  else
    query_pubid.part == pubid.part
  end
end

.search(text) ⇒ RelatonIso::HitCollection

Parameters:

  • text (String)

Returns:



14
15
16
17
18
19
20
21
# File 'lib/relaton_iso/iso_bibliography.rb', line 14

def search(text)
  HitCollection.new text.gsub(/\u2013/, "-")
rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
       EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
       Net::ProtocolError, OpenSSL::SSL::SSLError, Errno::ETIMEDOUT,
       Algolia::AlgoliaUnreachableHostError => e
  raise RelatonBib::RequestError, e.message
end