Class: Relaton::Ecma::EditionParser

Inherits:
Object
  • Object
show all
Includes:
ParserCommon
Defined in:
lib/relaton/ecma/edition_parser.rb

Instance Method Summary collapse

Methods included from ParserCommon

#contributor, #default_bib_hash, #fetch_docidentifier, #fetch_doctype, #fetch_ext

Constructor Details

#initialize(doc:, bib:, errors: {}, translation_source: []) ⇒ EditionParser

Returns a new instance of EditionParser.

Parameters:

  • doc (Mechanize::Page)

    document page

  • bib (Hash)

    base bibliographic item attributes

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

    error tracking hash

  • translation_source (Array) (defaults to: [])

    precomputed translation sources



10
11
12
13
14
15
# File 'lib/relaton/ecma/edition_parser.rb', line 10

def initialize(doc:, bib:, errors: {}, translation_source: [])
  @doc = doc
  @bib = bib
  @errors = errors
  @translation_source = translation_source
end

Instance Method Details

#create_extent(vol) ⇒ Object



66
67
68
69
70
71
# File 'lib/relaton/ecma/edition_parser.rb', line 66

def create_extent(vol)
  return unless vol && !vol.empty?

  locality = Bib::Locality.new(type: "volume", reference_from: vol)
  [Bib::Extent.new(locality: [locality])]
end

#edition_id_parts(text) ⇒ Array<String,nil,Array<Relaton::Bib::Date>>

Parse edition and date

Parameters:

  • text (String)

    identifier text

Returns:

  • (Array<String,nil,Array<Relaton::Bib::Date>>)

    edition and date



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/relaton/ecma/edition_parser.rb', line 44

def edition_id_parts(text) # rubocop:disable Metrics/MethodLength
  %r{^
    (?<id>\w+(?:[\d-]+|\sTR/\d+)),?\s
    (?:Volume\s(?<vol>[\d.]+),?\s)?
    (?<ed>[\d.]+)(?:st|nd|rd|th)?\sedition
    (?:[,.]\s(?<dt>\w+\s\d+))?
  }x =~ text
  date = [dt].compact.map do |d|
    on = Date.strptime(d, "%B %Y").strftime("%Y-%m")
    Bib::Date.new(type: "published", at: on)
  end
  [id, ed, date, vol]
end

#edition_source(hit) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/relaton/ecma/edition_parser.rb', line 58

def edition_source(hit)
  es = { "src" => hit.at("./a"), "pdf" => hit.at("./span/a") }.map do |type, a|
    Bib::Uri.new(type: type, content: a[:href]) if a
  end.compact
  @errors[:edition_source] &&= es.empty?
  es
end

#parseArray<Relaton::Ecma::ItemData>

Returns editions.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/relaton/ecma/edition_parser.rb', line 18

def parse # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  return [] unless @doc

  docid = @bib[:docidentifier]
  @doc.xpath('//div[@id="main"]/div[1]/div/main/article/div/div/standard/div/ul/li').map do |hit|
    bib = @bib.dup
    id, ed, bib[:date], vol = edition_id_parts hit.at("./span", "./a").text
    bib[:source] = edition_source(hit) + edition_translation_source(ed)
    next if ed.nil? || ed.empty?

    bib[:docidentifier] = id.nil? || id.empty? ? docid : fetch_docidentifier(id)
    @errors[:edition_docidentifier] &&= bib[:docidentifier].empty?
    bib[:edition] = Bib::Edition.new(content: ed)
    bib[:extent] = create_extent(vol)
    @errors[:edition_extent] &&= bib[:extent].nil?
    ItemData.new(**bib)
  end.compact
end