petrarca 0.6.6 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +13 -13
- data/.rspec +1 -1
- data/Gemfile +7 -4
- data/LICENSE.txt +22 -22
- data/README.md +84 -82
- data/Rakefile +2 -9
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/lib/petrarca/helpers.rb +51 -64
- data/lib/petrarca/isbn10.rb +54 -54
- data/lib/petrarca/isbn13.rb +47 -47
- data/lib/petrarca/version.rb +3 -3
- data/lib/petrarca.rb +85 -85
- data/petrarca.gemspec +43 -44
- metadata +9 -54
- data/data/registrant_ranges.txt +0 -551
- data/data/registration_group_ranges.txt +0 -9
- data/scripts/generate_range_files.rb +0 -82
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4132d184b7107b98f372097e6e2273360abd3ced931d5d6bcc0690eceae20e9b
|
4
|
+
data.tar.gz: 519bc872dd45f3bfec7fda6cbd85d3b33128c2e593c6467627a4481894e91800
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c0c7387558e3655a79370bece28b685df72ce5e2334bdcb4b059190886ad66fe44d9b3ac9142237915b9fa123a0582cabcb65e9e61fb3e53212d6ec37215710
|
7
|
+
data.tar.gz: de4ee6524415ccdc74e31a02f236d68077299b3f83cc2fd9e366616b7fdb98eb0a5790d8cc32ff7dfda3558bbc46b24a33abaac2e50855eee7aab2e17333048b
|
data/.gitignore
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
/.bundle/
|
2
|
-
/.yardoc
|
3
|
-
/_yardoc/
|
4
|
-
/coverage/
|
5
|
-
/doc/
|
6
|
-
/pkg/
|
7
|
-
/spec/reports/
|
8
|
-
/tmp/
|
9
|
-
/vendor/
|
10
|
-
/Gemfile.lock
|
11
|
-
*.txt
|
12
|
-
*.xml
|
13
|
-
.ruby-version
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
/vendor/
|
10
|
+
/Gemfile.lock
|
11
|
+
*.txt
|
12
|
+
*.xml
|
13
|
+
.ruby-version
|
data/.rspec
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--require spec_helper
|
1
|
+
--require spec_helper
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
Copyright (c) 2020 takatoh
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright (c) 2020 takatoh
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,82 +1,84 @@
|
|
1
|
-
# Petrarca
|
2
|
-
|
3
|
-
This library Petrarca provides some utility functions to manipulate ISBN numbers:
|
4
|
-
|
5
|
-
- Validation
|
6
|
-
- Calculate check digit
|
7
|
-
- Mutual conversion of ISBN-13 and ISBN-10
|
8
|
-
- Hyphenation / Dehyphenation
|
9
|
-
- Split to
|
10
|
-
|
11
|
-
All functions support both ISBN-13 and ISBN-10.
|
12
|
-
|
13
|
-
All ranges of registration groups and registrants are supported.
|
14
|
-
Those
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
```
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
irb(main):
|
39
|
-
=> true
|
40
|
-
irb(main):
|
41
|
-
=>
|
42
|
-
irb(main):
|
43
|
-
=> "
|
44
|
-
irb(main):
|
45
|
-
=> "
|
46
|
-
irb(main):
|
47
|
-
=> "978-4-8156-0644-2"
|
48
|
-
irb(main):
|
49
|
-
=> "
|
50
|
-
irb(main):
|
51
|
-
=>
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
irb(main):
|
60
|
-
=>
|
61
|
-
irb(main):
|
62
|
-
=> "
|
63
|
-
irb(main):
|
64
|
-
=> "
|
65
|
-
irb(main):
|
66
|
-
=> "978-4-8156-0644-2"
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
irb(main):
|
73
|
-
=>
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
1
|
+
# Petrarca
|
2
|
+
|
3
|
+
This library Petrarca provides some utility functions to manipulate ISBN numbers:
|
4
|
+
|
5
|
+
- Validation
|
6
|
+
- Calculate check digit
|
7
|
+
- Mutual conversion of ISBN-13 and ISBN-10
|
8
|
+
- Hyphenation / Dehyphenation
|
9
|
+
- Split to an array that includes ISBN segments
|
10
|
+
|
11
|
+
All functions support both ISBN-13 and ISBN-10.
|
12
|
+
|
13
|
+
All ranges of registration groups and registrants are supported.
|
14
|
+
Those depend indirectly on data published by the [International ISBN agency](https://www.isbn-international.org/) through 'isbnranges' gem.
|
15
|
+
|
16
|
+
**In version 1.0.0, the range files have been removed. Instead, the data provided by 'isbnranges' gem is used.** See https://github.com/takatoh/ISBNRanges for more information.
|
17
|
+
|
18
|
+
**No longer update just for range files.**
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem "petrarca"
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install petrarca
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
irb(main):001:0> require "petrarca"
|
39
|
+
=> true
|
40
|
+
irb(main):002:0> Petrarca.valid?("978-4-8156-0644-2")
|
41
|
+
=> true
|
42
|
+
irb(main):003:0> Petrarca.calc_check_digit("978-4-8156-0644-2")
|
43
|
+
=> "2"
|
44
|
+
irb(main):004:0> Petrarca.to_10("978-4-8156-0644-2")
|
45
|
+
=> "4-8156-0644-7"
|
46
|
+
irb(main):005:0> Petrarca.to_13("4-8156-0644-7")
|
47
|
+
=> "978-4-8156-0644-2"
|
48
|
+
irb(main):006:0> Petrarca.hyphenate("9784815606442")
|
49
|
+
=> "978-4-8156-0644-2"
|
50
|
+
irb(main):007:0> Petrarca.dehyphenate("978-4-8156-0644-2")
|
51
|
+
=> "9784815605442"
|
52
|
+
irb(main):008:0> Petrarca.split("9784815606442")
|
53
|
+
=> ["978", "4", "8156", "0644", "2"]
|
54
|
+
|
55
|
+
`Petrarca.dehyphenate` and `Petrarca.split` are new in v0.6.0.
|
56
|
+
|
57
|
+
Accept Integer as ISBN, only if no `-` and `X` including. (New in v0.5.0)
|
58
|
+
|
59
|
+
irb(main):001:0> require 'petrarca'
|
60
|
+
=> true
|
61
|
+
irb(main):002:0> Petrarca.calc_check_digit(978481560644)
|
62
|
+
=> "2"
|
63
|
+
irb(main):003:0> Petrarca.to_10(9784815606442)
|
64
|
+
=> "4-8156-0644-7"
|
65
|
+
irb(main):004:0> Petrarca.to_13(4815606447)
|
66
|
+
=> "978-4-8156-0644-2"
|
67
|
+
irb(main):005:0> Petrarca.hyphenate(9784815606442)
|
68
|
+
=> "978-4-8156-0644-2"
|
69
|
+
|
70
|
+
If the ISBN is hyphenated, registration group and registrant are considered in `Petrarca.valid?`. The second example below has the invalid registrant.
|
71
|
+
|
72
|
+
irb(main):006:0> Petrarca.valid?("978-4-8156-0644-2")
|
73
|
+
=> true
|
74
|
+
irb(main):007:0> Petrarca.valid?("978-4-815-60644-2")
|
75
|
+
=> false
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
MIT License
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
Bug reports and pull requests are welcome on GitHub at
|
84
|
+
https://github.com/takatoh/Petrarca.
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "petrarca"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "petrarca"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
set -euo pipefail
|
3
|
-
IFS=$'\n\t'
|
4
|
-
set -vx
|
5
|
-
|
6
|
-
bundle install
|
7
|
-
|
8
|
-
# Do any other automated setup that you need to do here
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
set -euo pipefail
|
3
|
+
IFS=$'\n\t'
|
4
|
+
set -vx
|
5
|
+
|
6
|
+
bundle install
|
7
|
+
|
8
|
+
# Do any other automated setup that you need to do here
|
data/lib/petrarca/helpers.rb
CHANGED
@@ -1,64 +1,51 @@
|
|
1
|
-
module Petrarca
|
2
|
-
|
3
|
-
class InvalidEANPrefixError < StandardError; end
|
4
|
-
class InvalidRangeError < StandardError; end
|
5
|
-
|
6
|
-
module Helpers
|
7
|
-
|
8
|
-
extend self
|
9
|
-
|
10
|
-
def split(isbn)
|
11
|
-
isbn = Petrarca.dehyphenate(isbn)
|
12
|
-
ean_prefix = isbn[0, 3]
|
13
|
-
unless ean_prefix == "978" || ean_prefix == "979"
|
14
|
-
raise InvalidEANPrefixError.new("Invalid EAN prefix: #{ean_prefix}")
|
15
|
-
end
|
16
|
-
body = isbn[3, 9]
|
17
|
-
check_digit = isbn[12, 1]
|
18
|
-
begin
|
19
|
-
registration_group, body = Helpers.split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix])
|
20
|
-
rescue InvalidRangeError
|
21
|
-
raise InvalidRangeError.new("Registration group is not defined: #{body} (under #{ean_prefix})")
|
22
|
-
end
|
23
|
-
prefix = "#{ean_prefix}-#{registration_group}"
|
24
|
-
begin
|
25
|
-
registrant, publication = Helpers.split_to_parts(body, REGISTRANT_RANGES[prefix])
|
26
|
-
rescue InvalidRangeError
|
27
|
-
raise InvalidRangeError.new("Registrant is not defined: #{body} (under #{prefix})")
|
28
|
-
end
|
29
|
-
[ean_prefix, registration_group, registrant, publication, check_digit]
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
def split_to_parts(body, ranges)
|
34
|
-
parts = ranges.map do |
|
35
|
-
s, e =
|
36
|
-
prefix = body[0, s.size]
|
37
|
-
if Range.new(s
|
38
|
-
[prefix, body[(prefix.size)..]]
|
39
|
-
else
|
40
|
-
nil
|
41
|
-
end
|
42
|
-
end.compact
|
43
|
-
unless parts.empty?
|
44
|
-
parts.first
|
45
|
-
else
|
46
|
-
raise InvalidRangeError.new(body)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
ranges = {}
|
53
|
-
File.open(range_file, "r") do |f|
|
54
|
-
f.each_line do |line|
|
55
|
-
next if line.start_with?("#")
|
56
|
-
g, r = line.chomp.split(":")
|
57
|
-
ranges[g] = r.split(",") unless r.nil?
|
58
|
-
end
|
59
|
-
end
|
60
|
-
ranges
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
end
|
1
|
+
module Petrarca
|
2
|
+
|
3
|
+
class InvalidEANPrefixError < StandardError; end
|
4
|
+
class InvalidRangeError < StandardError; end
|
5
|
+
|
6
|
+
module Helpers
|
7
|
+
|
8
|
+
extend self
|
9
|
+
|
10
|
+
def split(isbn)
|
11
|
+
isbn = Petrarca.dehyphenate(isbn)
|
12
|
+
ean_prefix = isbn[0, 3]
|
13
|
+
unless ean_prefix == "978" || ean_prefix == "979"
|
14
|
+
raise InvalidEANPrefixError.new("Invalid EAN prefix: #{ean_prefix}")
|
15
|
+
end
|
16
|
+
body = isbn[3, 9]
|
17
|
+
check_digit = isbn[12, 1]
|
18
|
+
begin
|
19
|
+
registration_group, body = Helpers.split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix])
|
20
|
+
rescue InvalidRangeError
|
21
|
+
raise InvalidRangeError.new("Registration group is not defined: #{body} (under #{ean_prefix})")
|
22
|
+
end
|
23
|
+
prefix = "#{ean_prefix}-#{registration_group}"
|
24
|
+
begin
|
25
|
+
registrant, publication = Helpers.split_to_parts(body, REGISTRANT_RANGES[prefix])
|
26
|
+
rescue InvalidRangeError
|
27
|
+
raise InvalidRangeError.new("Registrant is not defined: #{body} (under #{prefix})")
|
28
|
+
end
|
29
|
+
[ean_prefix, registration_group, registrant, publication, check_digit]
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def split_to_parts(body, ranges)
|
34
|
+
parts = ranges.map do |range|
|
35
|
+
s, e = range
|
36
|
+
prefix = body[0, s.size]
|
37
|
+
if Range.new(s, e).cover?(prefix)
|
38
|
+
[prefix, body[(prefix.size)..]]
|
39
|
+
else
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
end.compact
|
43
|
+
unless parts.empty?
|
44
|
+
parts.first
|
45
|
+
else
|
46
|
+
raise InvalidRangeError.new(body)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/lib/petrarca/isbn10.rb
CHANGED
@@ -1,54 +1,54 @@
|
|
1
|
-
require "petrarca/helpers"
|
2
|
-
|
3
|
-
|
4
|
-
module Petrarca
|
5
|
-
module ISBN10
|
6
|
-
|
7
|
-
extend self
|
8
|
-
|
9
|
-
def valid?(isbn)
|
10
|
-
isbn = isbn.to_s
|
11
|
-
if correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
|
12
|
-
if isbn.include?("-")
|
13
|
-
Helpers.split("978" + isbn).drop(1) == isbn.split("-")
|
14
|
-
else
|
15
|
-
true
|
16
|
-
end
|
17
|
-
else
|
18
|
-
false
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def correct_format?(isbn)
|
23
|
-
isbn = dehyphenate(isbn)
|
24
|
-
!!(/\A\d{9}[0-9X]\z/ =~ isbn)
|
25
|
-
end
|
26
|
-
|
27
|
-
def calc_check_digit(isbn)
|
28
|
-
nums = dehyphenate(isbn).split("")[0, 9].map{|x| x.to_i }
|
29
|
-
sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+)
|
30
|
-
check_digit = 11 - (sum % 11)
|
31
|
-
case check_digit
|
32
|
-
when 10
|
33
|
-
"X"
|
34
|
-
when 11
|
35
|
-
"0"
|
36
|
-
else
|
37
|
-
check_digit.to_s
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def dehyphenate(isbn)
|
42
|
-
Petrarca.dehyphenate(isbn)
|
43
|
-
end
|
44
|
-
|
45
|
-
def hyphenate(isbn)
|
46
|
-
split(isbn).join("-")
|
47
|
-
end
|
48
|
-
|
49
|
-
def split(isbn)
|
50
|
-
Helpers.split("978" + isbn.to_s).drop(1)
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
1
|
+
require "petrarca/helpers"
|
2
|
+
|
3
|
+
|
4
|
+
module Petrarca
|
5
|
+
module ISBN10
|
6
|
+
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def valid?(isbn)
|
10
|
+
isbn = isbn.to_s
|
11
|
+
if correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
|
12
|
+
if isbn.include?("-")
|
13
|
+
Helpers.split("978" + isbn).drop(1) == isbn.split("-")
|
14
|
+
else
|
15
|
+
true
|
16
|
+
end
|
17
|
+
else
|
18
|
+
false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def correct_format?(isbn)
|
23
|
+
isbn = dehyphenate(isbn)
|
24
|
+
!!(/\A\d{9}[0-9X]\z/ =~ isbn)
|
25
|
+
end
|
26
|
+
|
27
|
+
def calc_check_digit(isbn)
|
28
|
+
nums = dehyphenate(isbn).split("")[0, 9].map{|x| x.to_i }
|
29
|
+
sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+)
|
30
|
+
check_digit = 11 - (sum % 11)
|
31
|
+
case check_digit
|
32
|
+
when 10
|
33
|
+
"X"
|
34
|
+
when 11
|
35
|
+
"0"
|
36
|
+
else
|
37
|
+
check_digit.to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def dehyphenate(isbn)
|
42
|
+
Petrarca.dehyphenate(isbn)
|
43
|
+
end
|
44
|
+
|
45
|
+
def hyphenate(isbn)
|
46
|
+
split(isbn).join("-")
|
47
|
+
end
|
48
|
+
|
49
|
+
def split(isbn)
|
50
|
+
Helpers.split("978" + isbn.to_s).drop(1)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
data/lib/petrarca/isbn13.rb
CHANGED
@@ -1,47 +1,47 @@
|
|
1
|
-
require "petrarca/helpers"
|
2
|
-
|
3
|
-
|
4
|
-
module Petrarca
|
5
|
-
module ISBN13
|
6
|
-
|
7
|
-
extend self
|
8
|
-
|
9
|
-
def valid?(isbn)
|
10
|
-
isbn = isbn.to_s
|
11
|
-
if correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
|
12
|
-
if isbn.include?("-")
|
13
|
-
Helpers.split(isbn) == isbn.split("-")
|
14
|
-
else
|
15
|
-
true
|
16
|
-
end
|
17
|
-
else
|
18
|
-
false
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def correct_format?(isbn)
|
23
|
-
isbn = dehyphenate(isbn)
|
24
|
-
!!(/\A97[89]\d{9}\d\z/ =~ isbn)
|
25
|
-
end
|
26
|
-
|
27
|
-
def calc_check_digit(isbn)
|
28
|
-
nums = dehyphenate(isbn).split("")[0, 12].map{|x| x.to_i }
|
29
|
-
sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
|
30
|
-
check_digit = 10 - (sum % 10)
|
31
|
-
check_digit == 10 ? "0" : check_digit.to_s
|
32
|
-
end
|
33
|
-
|
34
|
-
def dehyphenate(isbn)
|
35
|
-
Petrarca.dehyphenate(isbn)
|
36
|
-
end
|
37
|
-
|
38
|
-
def hyphenate(isbn)
|
39
|
-
split(isbn).join("-")
|
40
|
-
end
|
41
|
-
|
42
|
-
def split(isbn)
|
43
|
-
Helpers.split(isbn)
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
1
|
+
require "petrarca/helpers"
|
2
|
+
|
3
|
+
|
4
|
+
module Petrarca
|
5
|
+
module ISBN13
|
6
|
+
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def valid?(isbn)
|
10
|
+
isbn = isbn.to_s
|
11
|
+
if correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
|
12
|
+
if isbn.include?("-")
|
13
|
+
Helpers.split(isbn) == isbn.split("-")
|
14
|
+
else
|
15
|
+
true
|
16
|
+
end
|
17
|
+
else
|
18
|
+
false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def correct_format?(isbn)
|
23
|
+
isbn = dehyphenate(isbn)
|
24
|
+
!!(/\A97[89]\d{9}\d\z/ =~ isbn)
|
25
|
+
end
|
26
|
+
|
27
|
+
def calc_check_digit(isbn)
|
28
|
+
nums = dehyphenate(isbn).split("")[0, 12].map{|x| x.to_i }
|
29
|
+
sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
|
30
|
+
check_digit = 10 - (sum % 10)
|
31
|
+
check_digit == 10 ? "0" : check_digit.to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
def dehyphenate(isbn)
|
35
|
+
Petrarca.dehyphenate(isbn)
|
36
|
+
end
|
37
|
+
|
38
|
+
def hyphenate(isbn)
|
39
|
+
split(isbn).join("-")
|
40
|
+
end
|
41
|
+
|
42
|
+
def split(isbn)
|
43
|
+
Helpers.split(isbn)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/lib/petrarca/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Petrarca
|
2
|
-
VERSION = "0.
|
3
|
-
end
|
1
|
+
module Petrarca
|
2
|
+
VERSION = "1.0.0"
|
3
|
+
end
|