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) ⇒ Array<RelatonIso::HitCollection, Array<String>>

Returns hits and missed year IDs.

Parameters:

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/relaton_iso/iso_bibliography.rb', line 105

def filter_hits_by_year(hit_collection, year)
  missed_year_ids = Set.new
  return [hit_collection, missed_year_ids] if year.nil?

  # filter by year
  hits = hit_collection.select do |hit|
    hit.pubid.year ||= hit.hit[:year]
    next true if check_year(year, hit)

    missed_year_ids << hit.pubid.to_s if hit.pubid.year
    false
  end

  [hits, missed_year_ids]
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
64
65
66
67
68
69
70
# 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
  query_pubid.part = nil if opts[:all_parts]
  Util.warn "(#{query_pubid}) Fetching from iso.org ..."

  hits, missed_year_ids = isobib_search_filter(query_pubid, opts)
  tip_ids = look_up_with_any_types_stages(hits, ref, opts)

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

  return fetch_ref_err(query_pubid, missed_year_ids, tip_ids) unless ret

  response_docid = ret.docidentifier.first.id.sub(" (all parts)", "")
  response_pubid = Pubid::Iso::Identifier.parse(response_docid)

  Util.warn "(#{query_pubid}) Found: `#{response_pubid}`"

  get_all = (
    (query_pubid.year && opts[:keep_year].nil?) ||
    opts[:keep_year] ||
    opts[:all_parts]
  )
  return ret if get_all

  ret.to_most_recent_reference
rescue Pubid::Core::Errors::ParseError
  Util.warn "(#{code}) Is not recognized as a standards identifier."
  nil
end

.matches_base?(query_pubid, pubid, any_types_stages: false) ⇒ <Type>

Matches base of query_pubid and pubid.

Parameters:

  • query_pubid (Pubid::Iso::Identifier)

    pubid to match

  • pubid (Pubid::Iso::Identifier)

    pubid to match

  • any_types_stages (Boolean) (defaults to: false)

    match with any types and stages

Returns:

  • (<Type>)

    <description>



92
93
94
95
96
97
98
99
100
# File 'lib/relaton_iso/iso_bibliography.rb', line 92

def matches_base?(query_pubid, pubid, any_types_stages: false) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics?PerceivedComplexity
  return false unless pubid.respond_to?(:publisher)

  query_pubid.publisher == pubid.publisher &&
    query_pubid.number == pubid.number &&
    query_pubid.copublisher == pubid.copublisher &&
    (any_types_stages || query_pubid.stage == pubid.stage) &&
    (any_types_stages || query_pubid.is_a?(pubid.class))
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)


76
77
78
79
80
81
# File 'lib/relaton_iso/iso_bibliography.rb', line 76

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

  query_pubid.part == pubid.part
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", "-")).fetch
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