Class: Pubid::Un::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/pubid/un/builder.rb

Constant Summary collapse

YEAR_REGEX =
/\A\d{4}\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(parsed_data) ⇒ Object



8
9
10
# File 'lib/pubid/un/builder.rb', line 8

def self.build(parsed_data)
  new.build(parsed_data)
end

Instance Method Details

#build(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pubid/un/builder.rb', line 12

def build(data)
  tokens = extract_tokens(data)
  raise "UN identifier has no tokens" if tokens.empty?

  number = tokens.last
  path = tokens[0...-1]
  year_token = path.reverse.find { |t| t.match?(YEAR_REGEX) }

  Identifiers::Document.new(
    path: path,
    number: number,
    date: year_token ? ::Pubid::Components::Date.new(year: year_token) : nil,
  )
end