isbn 2.0.7 → 2.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/isbn.rb +9 -1
  2. data/test/isbn_spec.rb +11 -2
  3. metadata +3 -3
@@ -1,7 +1,7 @@
1
1
  module ISBN
2
2
  extend self
3
3
 
4
- VERSION = "2.0.7"
4
+ VERSION = "2.0.8"
5
5
 
6
6
  def ten(isbn)
7
7
  raise InvalidISBNError unless isbn.is_a? String
@@ -65,6 +65,7 @@ module ISBN
65
65
  alias :unused :as_new
66
66
 
67
67
  def valid?(isbn)
68
+ return false unless isbn.is_a?(String)
68
69
  isbn = isbn.delete("-")
69
70
  case isbn.size
70
71
  when 13 then isbn[-1] == thirteen(isbn)[-1]
@@ -88,6 +89,13 @@ module ISBN
88
89
  raise InvalidSourceString unless match
89
90
  match.to_a.first
90
91
  end
92
+
93
+ def with_dashes(isbn)
94
+ s = isbn.gsub("-", "")
95
+ return s.scan(/(...)(.)(....)(....)(.)/).join('-') if s.size == 13
96
+ return s.scan(/(.)(....)(....)(.)/).join('-') if s.size == 10
97
+ raise InvalidSourceString
98
+ end
91
99
 
92
100
  class InvalidISBNError < RuntimeError; end
93
101
  class No10DigitISBNAvailable < RuntimeError; end
@@ -1,5 +1,5 @@
1
1
  require "minitest/spec"
2
- require "isbn"
2
+ require_relative "../lib/isbn"
3
3
 
4
4
  MiniTest::Unit.autorun
5
5
 
@@ -59,9 +59,18 @@ describe ISBN do
59
59
  ISBN.valid?("012781910X").must_equal true
60
60
  ISBN.valid?("9887401392").must_equal false
61
61
  ISBN.valid?("082047267").must_equal false
62
+ ISBN.valid?(nil).must_equal false
62
63
  end
63
64
 
64
65
  it "should get isbn from source string" do
65
66
  ISBN.from_string("ISBN:978-83-7659-303-6\nmore of content").must_equal "978-83-7659-303-6"
66
67
  end
67
- end
68
+
69
+ it "should add dashes to isbn 13 without dashes" do
70
+ ISBN.with_dashes("9780763740382").must_equal "978-0-7637-4038-2"
71
+ end
72
+
73
+ it "should add dashes to isbn 10 without dashes" do
74
+ ISBN.with_dashes("0763740382").must_equal "0-7637-4038-2"
75
+ end
76
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isbn
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.7
4
+ version: 2.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-06-14 00:00:00.000000000Z
13
+ date: 2012-12-26 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: ! ' library to transform ISBN''s from new to used, between 10 and 13,
16
16
  etc...
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 1.8.5
50
+ rubygems_version: 1.8.23
51
51
  signing_key:
52
52
  specification_version: 3
53
53
  summary: a simple library of functions on ISBN's