Module: Petrarca::Helpers

Included in:
ISBN10, ISBN13
Defined in:
lib/petrarca/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_ranges(range_file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/petrarca/helpers.rb', line 25

def self.load_ranges(range_file)
  ranges = {}
  File.open(range_file, "r") do |f|
    f.each_line do |line|
      next if line.start_with?("#")
      g, r = line.chomp.split(":")
      ranges[g] = r.split(",")
    end
  end
  ranges
end

Instance Method Details

#hyphenate13(isbn) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/petrarca/helpers.rb', line 14

def hyphenate13(isbn)
  ean_prefix = isbn[0..2]
  body = isbn[3..11]
  check_digit = isbn[12..12]
  registration_group, body = split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix])
  prefix = "#{ean_prefix}-#{registration_group}"
  registrant, publication = split_to_parts(body, REGISTRANT_RANGES[prefix])
  [ean_prefix, registration_group, registrant, publication, check_digit].join("-")
end

#split_to_parts(body, ranges) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/petrarca/helpers.rb', line 4

def split_to_parts(body, ranges)
  g = ""
  ranges.each do |range_str|
    s, e = range_str.split("-")
    g = body[0..(s.size - 1)]
    break if Range.new(s.to_i, e.to_i).cover?(g.to_i)
  end
  [g, body[(g.size)..]]
end