Sha256: 68c6e2f48097e06ba0cef6cd8e31717d69a6a160f699f42cc5ae8eee9997ec68
Contents?: true
Size: 1.49 KB
Versions: 1
Compression:
Stored size: 1.49 KB
Contents
module Petrarca class InvalidEANPrefixError < StandardError; end class InvalidRangeError < StandardError; end module Helpers extend self def split(isbn) isbn = Petrarca.dehyphenate(isbn) ean_prefix = isbn[0, 3] unless ean_prefix == "978" || ean_prefix == "979" raise InvalidEANPrefixError.new("Invalid EAN prefix: #{ean_prefix}") end body = isbn[3, 9] check_digit = isbn[12, 1] begin registration_group, body = Helpers.split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix]) rescue InvalidRangeError raise InvalidRangeError.new("Registration group is not defined: #{body} (under #{ean_prefix})") end prefix = "#{ean_prefix}-#{registration_group}" begin registrant, publication = Helpers.split_to_parts(body, REGISTRANT_RANGES[prefix]) rescue InvalidRangeError raise InvalidRangeError.new("Registrant is not defined: #{body} (under #{prefix})") end [ean_prefix, registration_group, registrant, publication, check_digit] end def split_to_parts(body, ranges) parts = ranges.map do |range| s, e = range prefix = body[0, s.size] if Range.new(s, e).cover?(prefix) [prefix, body[(prefix.size)..]] else nil end end.compact unless parts.empty? parts.first else raise InvalidRangeError.new(body) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
petrarca-1.0.0 | lib/petrarca/helpers.rb |