library_stdnums 1.4.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e0b3b13d325d0819bac0d053fc76b394e7d5086
4
+ data.tar.gz: a858dc5935fd3ac806b5dd76d0f6539af1942af7
5
+ SHA512:
6
+ metadata.gz: bf93f42ec0da3a9cd1d055e6fe3c734811631b0624c92598bed9ea63203a0e9ecce14de18a97e2dc40a5435b6d9a72391114c23a6646e9e36f55647e37ebeb2f
7
+ data.tar.gz: 7accbad483c800551e25a2d40be93c7926151aa5dbb72d5568bc0fda2898ae8c9d08cc9ae1de8630629c26fe4be9939153476e1e858451280b75a51b23aff290
@@ -1,10 +1,16 @@
1
1
  language: ruby
2
- bundler_args: --without documentation
2
+ cache: bundler
3
+ sudo: false
3
4
  rvm:
4
- - 1.8.7
5
- - 1.9.2
6
- - 1.9.3
7
5
  - jruby-19mode
8
- - rbx-19mode
9
- - jruby-18mode
10
- - rbx-18mode
6
+ - jruby-9.0.4.0
7
+ - 1.9
8
+ - 2.2
9
+ - 2.3.3
10
+ - 2.4.0
11
+ before_install:
12
+ - gem update --system
13
+ - gem uninstall bundler
14
+ - gem update bundler
15
+ jdk:
16
+ - oraclejdk8
@@ -1,3 +1,5 @@
1
+ * 1.5.0
2
+ * Added method StdNum::Helpers. extract_multiple_numbers (Michael Harrison)
1
3
  * 1.4.1 (2013.12.02)
2
4
  * Fixed bug in issn at_least_trying?
3
5
  * 1.4.0 (2013.10.23)
data/Rakefile CHANGED
@@ -1,25 +1,4 @@
1
1
  # encoding: utf-8
2
-
3
- require 'rubygems'
4
-
5
- begin
6
- require 'bundler'
7
- rescue LoadError => e
8
- warn e.message
9
- warn "Run `gem install bundler` to install Bundler."
10
- exit e.status_code
11
- end
12
-
13
- begin
14
- Bundler.setup(:development)
15
- rescue Bundler::BundlerError => e
16
- warn e.message
17
- warn "Run `bundle install` to install missing gems."
18
- exit e.status_code
19
- end
20
-
21
- require 'rake'
22
-
23
2
  require "bundler/gem_tasks"
24
3
 
25
4
  require 'yard'
@@ -34,4 +13,4 @@ Rake::TestTask.new do |test|
34
13
  end
35
14
 
36
15
  task :default => :test
37
- task :spec => :test
16
+ task :spec => :test
@@ -21,6 +21,18 @@ module StdNum
21
21
  return (match[1].gsub(/\-/, '')).upcase
22
22
  end
23
23
 
24
+ # Same as STDNUMPAT but allowing for all numbers in the provided string
25
+ STDNUMPAT_MULTIPLE = /.*?(\d[\d\-]{6,}[xX]?)/
26
+
27
+ # Extract the most likely looking numbers from the string. This will be each
28
+ # string with digits-and-hyphens-and-maybe-a-trailing-X, with the hypens removed
29
+ # @param [String] str The string from which to extract the ISBN/ISSNs
30
+ # @return [Array] An array of extracted identifiers
31
+ def extract_multiple_numbers(str)
32
+ return [] if str == '' || str.nil?
33
+ str.scan(STDNUMPAT_MULTIPLE).flatten.map{ |i| i.gsub(/\-/, '').upcase }
34
+ end
35
+
24
36
  # Given any string, extract what looks like the most likely ISBN/ISSN
25
37
  # of the given size(s), or nil if nothing matches at the correct size.
26
38
  # @param [String] rawnum The raw string containing (hopefully) an ISSN/ISBN
@@ -51,12 +63,15 @@ module StdNum
51
63
  # Validate, convert, and normalize ISBNs (10-digit or 13-digit)
52
64
  module ISBN
53
65
  extend Helpers
54
-
66
+
67
+ TEN_TO_THIRTEEN_PREFIX = '978'.freeze
68
+ VALID_THIRTEEN_PREFIX = /\A97[89]/
69
+
55
70
  # Does it even look like an ISBN?
56
71
  def self.at_least_trying? isbn
57
72
  reduce_to_basics(isbn, [10,13]) ? true : false
58
73
  end
59
-
74
+
60
75
 
61
76
  # Compute check digits for 10 or 13-digit ISBNs. See algorithm at
62
77
  # http://en.wikipedia.org/wiki/International_Standard_Book_Number
@@ -92,7 +107,7 @@ module StdNum
92
107
  # Check to see if the checkdigit is correct
93
108
  # @param [String] isbn The ISBN (we'll try to clean it up if possible)
94
109
  # @param [Boolean] preprocessed Set to true if the ISBN has already been through reduce_to_basics
95
- # @return [Boolean] Whether or not the checkdigit is correct. Sneakily, return 'nil' for
110
+ # @return [Boolean] Whether or not the checkdigit is correct. Sneakily, return 'nil' for
96
111
  # values that don't even look like ISBNs, and 'false' for those that look possible but
97
112
  # don't normalize / have bad checkdigits
98
113
  def self.valid? isbn, preprocessed = false
@@ -100,6 +115,7 @@ module StdNum
100
115
  isbn = reduce_to_basics(isbn, [10,13]) unless preprocessed
101
116
  return nil unless isbn
102
117
  return false unless isbn[-1..-1] == self.checkdigit(isbn, true)
118
+ return false unless isbn.size == 10 || valid_isbn13_prefix?(isbn)
103
119
  return true
104
120
  end
105
121
 
@@ -129,7 +145,7 @@ module StdNum
129
145
  return nil unless isbn
130
146
  return nil unless valid?(isbn, true)
131
147
  return isbn if isbn.size == 13
132
- prefix = '978' + isbn[0..8]
148
+ prefix = TEN_TO_THIRTEEN_PREFIX + isbn[0..8]
133
149
  return prefix + self.checkdigit(prefix + '0', true)
134
150
  end
135
151
 
@@ -145,7 +161,7 @@ module StdNum
145
161
  return isbn if isbn.size == 10
146
162
 
147
163
  # Can't be converted to ISBN-10? Bail
148
- return nil unless isbn[0..2] == '978'
164
+ return nil unless isbn[0..2] == TEN_TO_THIRTEEN_PREFIX
149
165
 
150
166
  prefix = isbn[3..11]
151
167
  return prefix + self.checkdigit(prefix + '0')
@@ -172,7 +188,14 @@ module StdNum
172
188
  end
173
189
  end
174
190
 
175
-
191
+ # Checks for a valid ISBN13 prefix
192
+ # ISBN13 always starts with 978 or 979. For example: 1000000000012 has a valid check digit, but
193
+ # is not a valid ISBN13.
194
+ # @param [String] isbn13 The ISBN13 to be checked.
195
+ # @return [Boolean] If true then the prefix is valid
196
+ def self.valid_isbn13_prefix?(isbn13)
197
+ isbn13.size == 13 and !!VALID_THIRTEEN_PREFIX.match(isbn13)
198
+ end
176
199
  end
177
200
 
178
201
  # Validate and and normalize ISSNs
@@ -210,7 +233,7 @@ module StdNum
210
233
  # Check to see if the checkdigit is correct
211
234
  # @param [String] issn The ISSN (we'll try to clean it up if possible)
212
235
  # @param [Boolean] preprocessed Set to true if the number has already been through reduce_to_basic
213
- # @return [Boolean] Whether or not the checkdigit is correct. Sneakily, return 'nil' for
236
+ # @return [Boolean] Whether or not the checkdigit is correct. Sneakily, return 'nil' for
214
237
  # values that don't even look like ISBNs, and 'false' for those that look possible but
215
238
  # don't normalize / have bad checkdigits
216
239
 
@@ -1,4 +1,4 @@
1
1
  module StdNum
2
2
  # library_stdnums version
3
- VERSION = "1.4.1"
3
+ VERSION = "1.6.0"
4
4
  end
@@ -21,9 +21,9 @@ Gem::Specification.new do |spec|
21
21
  spec.extra_rdoc_files = spec.files.grep(%r{^doc/})
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
- spec.add_development_dependency "rake"
25
- spec.add_development_dependency "minitest"
26
- spec.add_development_dependency "yard"
24
+ spec.add_development_dependency "rake", "~>11.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "yard", ">= 0.9.5"
27
27
  end
28
28
 
29
29
 
@@ -14,7 +14,7 @@ describe "Extract" do
14
14
  end
15
15
 
16
16
  it "should return nil on a non-match" do
17
- StdNum::ISBN.extractNumber('bill dueber').must_equal nil
17
+ StdNum::ISBN.extractNumber('bill dueber').must_be_nil
18
18
  end
19
19
 
20
20
  it "should allow a trailing X" do
@@ -30,9 +30,25 @@ describe "Extract" do
30
30
  end
31
31
 
32
32
  it "doesn't allow numbers that are too short" do
33
- StdNum::ISBN.extractNumber('12345').must_equal nil
33
+ StdNum::ISBN.extractNumber('12345').must_be_nil
34
34
  end
35
35
 
36
+ let(:identifiers_string) { '9780987115423 (print ed) 9780987115430 (web ed)' }
37
+ it "will extract multiple identifiers" do
38
+ StdNum::ISBN.extract_multiple_numbers(identifiers_string).must_be_kind_of Array
39
+ StdNum::ISBN.extract_multiple_numbers(identifiers_string).count.must_equal 2
40
+ StdNum::ISBN.extract_multiple_numbers(identifiers_string)[0].must_equal '9780987115423'
41
+ StdNum::ISBN.extract_multiple_numbers(identifiers_string)[1].must_equal '9780987115430'
42
+ end
43
+
44
+ let(:string_with_no_identifiers) { 'This has no identifiers' }
45
+ it "will return an empty array when no identifiers are in the supplied string " do
46
+ StdNum::ISBN.extract_multiple_numbers(string_with_no_identifiers).must_be_kind_of Array
47
+ StdNum::ISBN.extract_multiple_numbers(string_with_no_identifiers).count.must_equal 0
48
+
49
+ StdNum::ISBN.extract_multiple_numbers('').must_be_kind_of Array
50
+ StdNum::ISBN.extract_multiple_numbers('').count.must_equal 0
51
+ end
36
52
  it "skips over short prefixing numbers" do
37
53
  StdNum::ISBN.extractNumber('ISBN13: 1234567890123').must_equal '1234567890123'
38
54
  end
@@ -64,25 +80,25 @@ describe "ISBN" do
64
80
  it "finds a good number valid" do
65
81
  StdNum::ISBN.valid?('9780306406157').must_equal true
66
82
  end
67
-
83
+
68
84
  it "says a good number is trying" do
69
85
  StdNum::ISBN.at_least_trying?('9780306406157').must_equal true
70
86
  end
71
-
87
+
72
88
  it "says bad data is not trying" do
73
89
  StdNum::ISBN.at_least_trying?('978006406157').must_equal false
74
90
  StdNum::ISBN.at_least_trying?('406157').must_equal false
75
91
  StdNum::ISBN.at_least_trying?('$22').must_equal false
76
92
  StdNum::ISBN.at_least_trying?('hello').must_equal false
77
93
  end
78
-
94
+
79
95
 
80
96
  it "finds a bad number invalid" do
81
97
  StdNum::ISBN.valid?('9780306406154').must_equal false
82
98
  end
83
99
 
84
100
  it "returns nil when computing checksum for bad ISBN" do
85
- StdNum::ISBN.checkdigit('12345').must_equal nil
101
+ StdNum::ISBN.checkdigit('12345').must_be_nil
86
102
  end
87
103
 
88
104
  it "converts 10 to 13" do
@@ -99,9 +115,9 @@ describe "ISBN" do
99
115
 
100
116
  it "normalizes" do
101
117
  StdNum::ISBN.normalize('0-306-40615-2').must_equal '9780306406157'
102
- StdNum::ISBN.normalize('0-306-40615-X').must_equal nil
118
+ StdNum::ISBN.normalize('0-306-40615-X').must_be_nil
103
119
  StdNum::ISBN.normalize('ISBN: 978-0-306-40615-7').must_equal '9780306406157'
104
- StdNum::ISBN.normalize('ISBN: 978-0-306-40615-3').must_equal nil
120
+ StdNum::ISBN.normalize('ISBN: 978-0-306-40615-3').must_be_nil
105
121
  end
106
122
 
107
123
  it "gets both normalized values" do
@@ -112,7 +128,10 @@ describe "ISBN" do
112
128
  a.sort.must_equal ['9780306406157', '0306406152' ].sort
113
129
  end
114
130
 
115
-
131
+ it "identifies an invalid ISBN13 due to the prefix" do
132
+ StdNum::ISBN.valid_isbn13_prefix?('9780000000002').must_equal true
133
+ StdNum::ISBN.valid?('1000000000012').must_equal false
134
+ end
116
135
 
117
136
  end
118
137
 
@@ -451,7 +470,6 @@ describe "LCCN tests from Business::LCCN perl module" do
451
470
  :serial => '001234',
452
471
  :prefix => 'a',
453
472
  :year_cataloged => 1989,
454
- :serial => '001234',
455
473
  },
456
474
  { :orig => 'http://lccn.loc.gov/ab89001234',
457
475
  :canonical => 'ab 89001234 ',
@@ -1,5 +1,19 @@
1
- require 'minitest/spec'
2
- require 'minitest/autorun'
1
+ # minitest has a circular reference somewhere
2
+ # drives me crazy
3
+ def silence_warnings(&block)
4
+ warn_level = $VERBOSE
5
+ $VERBOSE = nil
6
+ result = block.call
7
+ $VERBOSE = warn_level
8
+ result
9
+ end
10
+
11
+
12
+ silence_warnings do
13
+ require 'minitest/spec'
14
+ require 'minitest/autorun'
15
+ end
16
+
3
17
 
4
18
  require 'library_stdnums'
5
19
 
metadata CHANGED
@@ -1,80 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: library_stdnums
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
5
- prerelease:
4
+ version: 1.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Bill Dueber
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-12-02 00:00:00.000000000 Z
11
+ date: 2017-05-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
- version: '0'
33
+ version: '11.0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
- version: '0'
40
+ version: '11.0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: minitest
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: '0'
47
+ version: '5.0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
- version: '0'
54
+ version: '5.0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: yard
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
- version: '0'
61
+ version: 0.9.5
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
- version: '0'
68
+ version: 0.9.5
78
69
  description:
79
70
  email:
80
71
  - none@nowhere.org
@@ -82,9 +73,9 @@ executables: []
82
73
  extensions: []
83
74
  extra_rdoc_files: []
84
75
  files:
85
- - .gitignore
86
- - .travis.yml
87
- - .yardopts
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - ".yardopts"
88
79
  - ChangeLog.md
89
80
  - Gemfile
90
81
  - LICENSE.txt
@@ -98,36 +89,28 @@ files:
98
89
  homepage: https://github.com/billdueber/library_stdnums
99
90
  licenses:
100
91
  - MIT
92
+ metadata: {}
101
93
  post_install_message:
102
94
  rdoc_options: []
103
95
  require_paths:
104
96
  - lib
105
97
  required_ruby_version: !ruby/object:Gem::Requirement
106
- none: false
107
98
  requirements:
108
- - - ! '>='
99
+ - - ">="
109
100
  - !ruby/object:Gem::Version
110
101
  version: '0'
111
- segments:
112
- - 0
113
- hash: -3816894525355175923
114
102
  required_rubygems_version: !ruby/object:Gem::Requirement
115
- none: false
116
103
  requirements:
117
- - - ! '>='
104
+ - - ">="
118
105
  - !ruby/object:Gem::Version
119
106
  version: '0'
120
- segments:
121
- - 0
122
- hash: -3816894525355175923
123
107
  requirements: []
124
108
  rubyforge_project:
125
- rubygems_version: 1.8.23
109
+ rubygems_version: 2.6.8
126
110
  signing_key:
127
- specification_version: 3
111
+ specification_version: 4
128
112
  summary: A simple set of module functions to normalize, validate, and convert common
129
113
  library standard numbers
130
114
  test_files:
131
115
  - spec/library_stdnums_spec.rb
132
116
  - spec/spec_helper.rb
133
- has_rdoc: