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:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/relaton_iso/iso_bibliography.rb', line 114

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.base.nil? && hit.pubid.year.to_s == year.to_s) ||
      (!hit.pubid.base.nil? && hit.pubid.base.year.to_s == year.to_s)
      true
    elsif (!hit.pubid.base.nil? && hit.pubid.year.to_s == year.to_s)
      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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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)

  # Try with ISO/IEC prefix if ISO not found
  if resp[:hits].empty? && query_pubid.copublisher.nil? &&
      query_pubid.publisher == "ISO"
    resp_isoiec = retry_isoiec_prefix(query_pubid, opts)
    resp = resp_isoiec unless resp_isoiec.nil?
  end

  # 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

  return fetch_ref_err(query_pubid) unless ret

  # puts "xxxxx #{ret.docidentifier.first.id.inspect}"
  response_docid = ret.docidentifier.first.id.sub(" (all parts)", "")
  response_pubid = Pubid::Iso::Identifier.parse(response_docid)

  puts "xxxxx query_pubid(#{query_pubid}) response_pubid(#{response_pubid})"

  if query_pubid.to_s == response_pubid.to_s
    warn "[relaton-iso] (\"#{query_pubid}\") Found exact match."
  elsif matches_base?(query_pubid, response_pubid)
    warn "[relaton-iso] (\"#{query_pubid}\") " \
      "Found (\"#{response_pubid}\")."
  elsif matches_base?(query_pubid, response_pubid, any_types_stages: true)
    warn "[relaton-iso] (\"#{query_pubid}\") TIP: " \
      "Found with different type/stage, " \
      "please amend to (\"#{response_pubid}\")."
    else
    # when there are all parts
    warn "[relaton-iso] (\"#{query_pubid}\") Found (\"#{response_pubid}\")."
  end

  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
  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)


103
104
105
106
107
108
109
# File 'lib/relaton_iso/iso_bibliography.rb', line 103

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)


96
97
98
99
100
101
# File 'lib/relaton_iso/iso_bibliography.rb', line 96

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/, "-")
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