Class: Isbn10To13DigitConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/Isbn10To13DigitConverter.rb

Instance Method Summary collapse

Constructor Details

#initialize(isbn) ⇒ Isbn10To13DigitConverter

Returns a new instance of Isbn10To13DigitConverter.



6
7
8
# File 'lib/Isbn10To13DigitConverter.rb', line 6

def initialize(isbn)
	@isbn = isbn
end

Instance Method Details

#convert(isbn10) ⇒ Object



28
29
30
# File 'lib/Isbn10To13DigitConverter.rb', line 28

def convert (isbn10)
 isbn10ToIsbn13(isbn10)
end

#getIsbn13CheckSum(isbn13) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/Isbn10To13DigitConverter.rb', line 46

def  getIsbn13CheckSum(isbn13)
	sum = 0
	isbnFactorPattern = [1,3,1,3,1,3,1,3,1,3,1,3]
	for i in 0..11
	  sum = sum + isbnFactorPattern[i] *  isbn13[i].to_i
	end
	value = sum % 10
	lastDigitIsbn = 10 - value 
	return lastDigitIsbn  
end

#isbn10ToIsbn13(isbn10) ⇒ Object

The method to convert an 10 digit ISBN to 13 digit The method takes a 10 digit isbn and append “978” to the first 9 characters of the isbn and generate the 13 digit



35
36
37
38
39
40
41
42
43
# File 'lib/Isbn10To13DigitConverter.rb', line 35

def isbn10ToIsbn13(isbn10) 
	
	if isbn10.length() !=10
	  raise "The argument length should be 10"
	end
	
	isbn13 = "978" + isbn10.chomp("-");
	return isbn13.slice(0, isbn13.length()-1) + getIsbn13CheckSum(isbn13).to_s;
end

#processObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/Isbn10To13DigitConverter.rb', line 15

def process

 @strippedIsbn =  @isbn.gsub(/[^0-9a-z]/i,'')
 
 if( @strippedIsbn.length() == 10 )
		return convert(@strippedIsbn)
	else (@strippedIsbn.length() == 13 )
	   @isbn13To10Converter.process
  end

end

#set13To10DigitConverter(isbn13To10Converter) ⇒ Object



10
11
12
# File 'lib/Isbn10To13DigitConverter.rb', line 10

def set13To10DigitConverter(isbn13To10Converter)
	@isbn13To10Converter = isbn13To10Converter
end