rbook 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/LICENSE +13 -0
- data/README +16 -0
- data/Rakefile +206 -0
- data/examples/titlepage.rb +14 -0
- data/examples/www/find_all.rb +23 -0
- data/examples/www/find_cover_from_amazon.rb +12 -0
- data/examples/www/find_url_from_rainbow.rb +12 -0
- data/examples/www/list.rb +13 -0
- data/lib/rbook/bisac.rb +175 -0
- data/lib/rbook/errors.rb +7 -0
- data/lib/rbook/isbn.rb +249 -0
- data/lib/rbook/onix.rb +68 -0
- data/lib/rbook/onix/contributor.rb +60 -0
- data/lib/rbook/onix/lists.rb +2 -0
- data/lib/rbook/onix/lists/contributor_role.rb +10 -0
- data/lib/rbook/onix/lists/product_form.rb +100 -0
- data/lib/rbook/onix/message.rb +101 -0
- data/lib/rbook/onix/product.rb +188 -0
- data/lib/rbook/onix/sales_restriction.rb +51 -0
- data/lib/rbook/onix/supply_detail.rb +68 -0
- data/lib/rbook/onix/xchar.rb +98 -0
- data/lib/rbook/titlepage.rb +96 -0
- data/lib/rbook/titlepage/TitleQueryClient.rb +62 -0
- data/lib/rbook/titlepage/titlepage_driver.rb +134 -0
- data/lib/rbook/titlepage/titlepage_utils.rb +374 -0
- data/lib/rbook/www.rb +172 -0
- data/lib/rbook/www/aau_scraper.rb +76 -0
- data/lib/rbook/www/amazon_uk_scraper.rb +44 -0
- data/lib/rbook/www/base.rb +87 -0
- data/lib/rbook/www/harper_au_scraper.rb +56 -0
- data/lib/rbook/www/harper_us_scraper.rb +55 -0
- data/lib/rbook/www/hha_scraper.rb +50 -0
- data/lib/rbook/www/macmillan_scraper.rb +62 -0
- data/lib/rbook/www/orbis_scraper.rb +48 -0
- data/lib/rbook/www/oup_scraper.rb +64 -0
- data/lib/rbook/www/paulist_scraper.rb +53 -0
- data/lib/rbook/www/pearson_au_scraper.rb +52 -0
- data/lib/rbook/www/penguin_scraper.rb +45 -0
- data/lib/rbook/www/random_au_scraper.rb +90 -0
- data/lib/rbook/www/random_us_scraper.rb +59 -0
- data/lib/rbook/www/sas_scraper.rb +54 -0
- data/lib/rbook/www/unireps_scraper.rb +58 -0
- data/lib/rbook/www/wiley_us_scraper.rb +54 -0
- data/test/data/abingdon.xml +38931 -0
- data/test/data/augsburg.xml +39009 -0
- data/test/data/chalice.xml +10851 -0
- data/test/data/eerdsman.xml +36942 -0
- data/test/data/invalid_no_product.xml +9 -0
- data/test/data/not_xml.csv +1 -0
- data/test/data/single_product.xml +50 -0
- data/test/data/xml_not_onix.xml +7 -0
- data/test/mocks/titlepage_driver.rb +107 -0
- data/test/unit/bisac_test.rb +57 -0
- data/test/unit/isbn_test.rb +149 -0
- data/test/unit/onix/contributor_test.rb +50 -0
- data/test/unit/onix/message_test.rb +119 -0
- data/test/unit/onix/product_test.rb +101 -0
- data/test/unit/onix/sales_restriction_test.rb +48 -0
- data/test/unit/onix/supply_detail_test.rb +53 -0
- data/test/unit/onix/xchar_test.rb +37 -0
- data/test/unit/titlepage_test.rb +127 -0
- metadata +130 -0
@@ -0,0 +1,9 @@
|
|
1
|
+
<?xml version='1.0'?>
|
2
|
+
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
|
3
|
+
<ONIXMessage>
|
4
|
+
<Header>
|
5
|
+
<FromCompany>Schwartz Publishing Pty Ltd</FromCompany>
|
6
|
+
<FromPerson>James Healy</FromPerson>
|
7
|
+
<SentDate>20060522</SentDate>
|
8
|
+
</Header>
|
9
|
+
</ONIXMessage>
|
@@ -0,0 +1 @@
|
|
1
|
+
"this file is not XML","Remind you of 1990?"
|
@@ -0,0 +1,50 @@
|
|
1
|
+
<?xml version='1.0'?>
|
2
|
+
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
|
3
|
+
<ONIXMessage>
|
4
|
+
<Header>
|
5
|
+
<FromCompany>Chalice</FromCompany>
|
6
|
+
<FromPerson>James Healy</FromPerson>
|
7
|
+
<SentDate>20060521</SentDate>
|
8
|
+
</Header>
|
9
|
+
<Product>
|
10
|
+
<RecordReference>0827200528</RecordReference>
|
11
|
+
<NotificationType>03</NotificationType>
|
12
|
+
<ProductIdentifier>
|
13
|
+
<ProductIDType>02</ProductIDType>
|
14
|
+
<IDValue>0827200528</IDValue>
|
15
|
+
</ProductIdentifier>
|
16
|
+
<ProductForm>BA</ProductForm>
|
17
|
+
<NoSeries/>
|
18
|
+
<Title>
|
19
|
+
<TitleType>01</TitleType>
|
20
|
+
<TitleText>And Their Eyes Are Opened</TitleText>
|
21
|
+
<Subtitle>Story Sermons Embracing the World</Subtitle>
|
22
|
+
</Title>
|
23
|
+
<Contributor>
|
24
|
+
<SequenceNumber>0</SequenceNumber>
|
25
|
+
<ContributorRole>A01</ContributorRole>
|
26
|
+
<PersonNameInverted>Healy, James</PersonNameInverted>
|
27
|
+
</Contributor>
|
28
|
+
<NoEdition/>
|
29
|
+
<BICMainSubject>YFH</BICMainSubject>
|
30
|
+
<NumberOfPages>0</NumberOfPages>
|
31
|
+
<Publisher>
|
32
|
+
<PublishingRole>01</PublishingRole>
|
33
|
+
<PublisherName>Chalice</PublisherName>
|
34
|
+
</Publisher>
|
35
|
+
<PublicationDate>20050303</PublicationDate>
|
36
|
+
<SalesRestriction>
|
37
|
+
<SalesRestrictionType>00</SalesRestrictionType>
|
38
|
+
<SalesRestrictionDetail>Unknown</SalesRestrictionDetail>
|
39
|
+
</SalesRestriction>
|
40
|
+
<SupplyDetail>
|
41
|
+
<SupplierName>Chalice</SupplierName>
|
42
|
+
<AvailabilityCode>CS</AvailabilityCode>
|
43
|
+
<IntermediaryAvailabilityCode>99</IntermediaryAvailabilityCode>
|
44
|
+
<Price>
|
45
|
+
<PriceTypeCode>02</PriceTypeCode>
|
46
|
+
<PriceAmount>29.95</PriceAmount>
|
47
|
+
</Price>
|
48
|
+
</SupplyDetail>
|
49
|
+
</Product>
|
50
|
+
</ONIXMessage>
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'soap/soap'
|
3
|
+
|
4
|
+
# A mock object to assist with testing the titlepage API wrapper
|
5
|
+
class MockTitlePageDriver
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@username = "test"
|
9
|
+
@password = "apa"
|
10
|
+
end
|
11
|
+
|
12
|
+
def login(username, password)
|
13
|
+
if username.eql?(@username) && password.eql?(@password)
|
14
|
+
@token = Digest::MD5.hexdigest(Time.now.to_s)
|
15
|
+
return @token
|
16
|
+
else
|
17
|
+
raise SOAP::FaultError, SOAP::SOAPFault.new
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def logout(token)
|
22
|
+
if token.eql?(@token)
|
23
|
+
@token = nil
|
24
|
+
return true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def SearchByEAN(token, isbn)
|
29
|
+
if !token.eql?(@token)
|
30
|
+
return nil
|
31
|
+
elsif isbn.eql?("9780091835132")
|
32
|
+
return fight_club
|
33
|
+
elsif isbn.eql?("9780672327568")
|
34
|
+
return postgresql
|
35
|
+
else
|
36
|
+
return no_match(isbn)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def fight_club
|
43
|
+
result = SearchResults.new
|
44
|
+
result.EAN = "9780091835132"
|
45
|
+
product = Product.new
|
46
|
+
author = Contributor.new
|
47
|
+
author.PersonNameInverted = "PALAHNIUK, CHUCK"
|
48
|
+
author.ContributorRole = "A01"
|
49
|
+
product.Contributors = [author]
|
50
|
+
title = Title.new
|
51
|
+
title.TitleType = "01"
|
52
|
+
title.TitleText = "Fight Club"
|
53
|
+
product.Title = title
|
54
|
+
supply = SupplyDetail.new
|
55
|
+
supply.ProductAvailability = "20"
|
56
|
+
supply.SupplierName = "Random House Australia"
|
57
|
+
stock = Stock.new
|
58
|
+
stock.OnHand = "In Stock"
|
59
|
+
stock.OnOrder = "0"
|
60
|
+
supply.Stock = stock
|
61
|
+
price = Price.new
|
62
|
+
price.PriceAmount = "23.95"
|
63
|
+
supply.Price = price
|
64
|
+
supply.PackQuantity = "48"
|
65
|
+
id = ProductIdentifier.new
|
66
|
+
id.ProductIDType = "03"
|
67
|
+
id.IDValue = "9780091835132"
|
68
|
+
result.Product = product
|
69
|
+
return result
|
70
|
+
end
|
71
|
+
|
72
|
+
def no_match(isbn)
|
73
|
+
result = SearchResults.new
|
74
|
+
result.EAN = isbn
|
75
|
+
return result
|
76
|
+
end
|
77
|
+
|
78
|
+
def postgresql
|
79
|
+
result = SearchResults.new
|
80
|
+
result.EAN = "9780672327568"
|
81
|
+
product = Product.new
|
82
|
+
author = Contributor.new
|
83
|
+
author.PersonNameInverted = "KORRY, DOUGLASS"
|
84
|
+
author.ContributorRole = "A01"
|
85
|
+
product.Contributors = [author]
|
86
|
+
title = Title.new
|
87
|
+
title.TitleType = "01"
|
88
|
+
title.TitleText = "PostgreSQL"
|
89
|
+
product.Title = title
|
90
|
+
supply = SupplyDetail.new
|
91
|
+
supply.ProductAvailability = "20"
|
92
|
+
supply.SupplierName = "United Book Distributors"
|
93
|
+
stock = Stock.new
|
94
|
+
stock.OnHand = "0"
|
95
|
+
stock.OnOrder = "0"
|
96
|
+
supply.Stock = stock
|
97
|
+
price = Price.new
|
98
|
+
price.PriceAmount = "69.95"
|
99
|
+
supply.Price = price
|
100
|
+
supply.PackQuantity = "48"
|
101
|
+
id = ProductIdentifier.new
|
102
|
+
id.ProductIDType = "03"
|
103
|
+
id.IDValue = "9780672327568"
|
104
|
+
result.Product = product
|
105
|
+
return result
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rbook/bisac'
|
5
|
+
|
6
|
+
class BISACTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def test_line_length
|
9
|
+
|
10
|
+
bisac = RBook::Bisac.new("Rainbow","1234567","test","1")
|
11
|
+
bisac.add_item(valid_item_1)
|
12
|
+
bisac.add_item(valid_item_2)
|
13
|
+
|
14
|
+
bisac.to_s.split("\n").each do |line|
|
15
|
+
assert_equal 259, line.length
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def valid_item_1
|
22
|
+
item = {}
|
23
|
+
item[:isbn] = "0743285689"
|
24
|
+
item[:title] = "Enemy Combatant"
|
25
|
+
item[:author] = "Begg, Moazzam"
|
26
|
+
item[:price] = "2995"
|
27
|
+
item[:pubdate] = "060101"
|
28
|
+
item[:publisher] = "Simon and Schuster"
|
29
|
+
item[:imprint] = "Free Press"
|
30
|
+
item[:volumes] = "1"
|
31
|
+
item[:edition] = "1"
|
32
|
+
item[:binding] = "PB"
|
33
|
+
item[:volume] = "1"
|
34
|
+
item[:status] = "O/O"
|
35
|
+
return item
|
36
|
+
end
|
37
|
+
|
38
|
+
def valid_item_2
|
39
|
+
item = {}
|
40
|
+
item[:isbn] = "0975240277"
|
41
|
+
item[:title] = "HTML Utopia Designing Without Tables Using CSS"
|
42
|
+
item[:author] = "Andrew, Rachel; Shafer, Dan"
|
43
|
+
item[:price] = "5995"
|
44
|
+
item[:pubdate] = "060401"
|
45
|
+
item[:publisher] = "Sitepoint"
|
46
|
+
item[:imprint] = "Sitepoint"
|
47
|
+
item[:volumes] = "1"
|
48
|
+
item[:edition] = "2"
|
49
|
+
item[:binding] = "PB"
|
50
|
+
item[:volume] = "1"
|
51
|
+
item[:status] = "ACT"
|
52
|
+
return item
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
@@ -0,0 +1,149 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rbook/isbn'
|
5
|
+
|
6
|
+
class ISBNTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include RBook
|
9
|
+
|
10
|
+
def test_add_groups
|
11
|
+
assert_equal ISBN::add_groups('020161622X'), '0-20161-622-X'
|
12
|
+
assert_equal ISBN::add_groups('9780201616224'), '978-0-20161-622-4'
|
13
|
+
assert_equal ISBN::add_groups('invalId'), 'invalId'
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_add_groups_destructive
|
17
|
+
isbn = '020161622X'
|
18
|
+
new_isbn = ISBN::add_groups(isbn)
|
19
|
+
assert_equal( isbn, '020161622X')
|
20
|
+
|
21
|
+
isbn = '978201616224'
|
22
|
+
new_isbn = ISBN::add_groups(isbn)
|
23
|
+
assert_equal( isbn, '978201616224')
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_add_boundaries
|
27
|
+
assert_equal ISBN::add_groups(''), ''
|
28
|
+
assert_equal ISBN::add_groups(nil), nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_valid_isbn10
|
32
|
+
assert_equal ISBN::valid_isbn10?('020161622X'), true
|
33
|
+
assert_equal ISBN::valid_isbn10?('020161622'), false
|
34
|
+
assert_equal ISBN::valid_isbn10?('023252565X'), true
|
35
|
+
assert_equal ISBN::valid_isbn10?('9780764812200'), false
|
36
|
+
assert_equal ISBN::valid_isbn10?('12345'), false
|
37
|
+
assert_equal ISBN::valid_isbn10?('12345 7890'), false
|
38
|
+
assert_equal ISBN::valid_isbn10?('CD-DSE'), false
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_valid_isbn10_boundaries
|
42
|
+
assert_equal ISBN::valid_isbn10?(''), false
|
43
|
+
assert_equal ISBN::valid_isbn10?('11278217391827398217391723918723918'), false
|
44
|
+
assert_equal ISBN::valid_isbn10?(nil), false
|
45
|
+
assert_equal ISBN::valid_isbn10?(1234567890), false
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_valid_isbn13
|
49
|
+
assert_equal ISBN::valid_isbn13?('9780201616224'), true
|
50
|
+
assert_equal ISBN::valid_isbn13?('978020161622'), false
|
51
|
+
assert_equal ISBN::valid_isbn13?('9780764812200'), true
|
52
|
+
assert_equal ISBN::valid_isbn13?('978076481220'), false
|
53
|
+
assert_equal ISBN::valid_isbn13?('978 NCR 4611'), false
|
54
|
+
assert_equal ISBN::valid_isbn13?('12345'), false
|
55
|
+
assert_equal ISBN::valid_isbn13?('CD-DSE'), false
|
56
|
+
assert_equal ISBN::valid_isbn13?('020161622X'), false
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_valid_isbn13_boundaries
|
60
|
+
assert_equal ISBN::valid_isbn13?(''), false
|
61
|
+
assert_equal ISBN::valid_isbn13?('11278217391827398217391723918723918'), false
|
62
|
+
assert_equal ISBN::valid_isbn13?(nil), false
|
63
|
+
assert_equal ISBN::valid_isbn13?(9781234567890), false
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_calc_isbn10_check
|
67
|
+
assert_equal ISBN::calc_isbn10_check('020161622'), 'X'
|
68
|
+
assert_equal ISBN::calc_isbn10_check('055299777'), '3'
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_calc_isbn10_check_boundaries
|
72
|
+
assert_equal ISBN::calc_isbn10_check(''), nil
|
73
|
+
assert_equal ISBN::calc_isbn10_check(nil), nil
|
74
|
+
assert_equal ISBN::calc_isbn10_check(978020161622), nil
|
75
|
+
assert_equal ISBN::calc_isbn10_check(' - '), nil
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_calc_isbn13_check
|
79
|
+
assert_equal ISBN::calc_isbn13_check('978020161622'), '4'
|
80
|
+
assert_equal ISBN::calc_isbn13_check('978055299777'), '5'
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_calc_isbn13_boundaries
|
84
|
+
assert_equal ISBN::calc_isbn13_check(''), nil
|
85
|
+
assert_equal ISBN::calc_isbn13_check(nil), nil
|
86
|
+
assert_equal ISBN::calc_isbn13_check(978020161622), nil
|
87
|
+
assert_equal ISBN::calc_isbn13_check(' - '), nil
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_convert_to_isbn10
|
91
|
+
#some random titles
|
92
|
+
assert_equal ISBN::convert_to_isbn10('9780764812200'), '0764812203'
|
93
|
+
assert_equal ISBN::convert_to_isbn10('9781856075336'), '1856075338'
|
94
|
+
assert_equal ISBN::convert_to_isbn10('9780232525656'), '023252565X'
|
95
|
+
assert_equal ISBN::convert_to_isbn10('9781863951340'), '1863951342'
|
96
|
+
assert_equal ISBN::convert_to_isbn10('978 NCR 4611'), nil
|
97
|
+
|
98
|
+
# a 979 ISBN13 that can't be converted to isbn10
|
99
|
+
assert_equal ISBN::convert_to_isbn10('9791863951340'), nil
|
100
|
+
|
101
|
+
# already isbn10
|
102
|
+
assert_equal ISBN::convert_to_isbn10('1856075338'), '1856075338'
|
103
|
+
|
104
|
+
# valid isbn without a checkdigit
|
105
|
+
assert_equal ISBN::convert_to_isbn10('185607533'), nil
|
106
|
+
|
107
|
+
#non valid isbns
|
108
|
+
assert_equal ISBN::convert_to_isbn10('AAAAAAA'), nil
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_convert_to_isbn10_boundaries
|
113
|
+
assert_equal ISBN::convert_to_isbn13(' '), nil
|
114
|
+
assert_equal ISBN::convert_to_isbn13(nil), nil
|
115
|
+
assert_equal ISBN::convert_to_isbn13(120234203), nil
|
116
|
+
assert_equal ISBN::convert_to_isbn13('18273987123987129387192873912820234203'), nil
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_convert_to_isbn13
|
120
|
+
#some random titles
|
121
|
+
assert_equal ISBN::convert_to_isbn13('0764812203'), '9780764812200'
|
122
|
+
assert_equal ISBN::convert_to_isbn13('1856075338'), '9781856075336'
|
123
|
+
assert_equal ISBN::convert_to_isbn13('023252565X'), '9780232525656'
|
124
|
+
assert_equal ISBN::convert_to_isbn13('1863951342'), '9781863951340'
|
125
|
+
assert_equal ISBN::convert_to_isbn13('186-395-134-2'), nil
|
126
|
+
|
127
|
+
#already isbn13
|
128
|
+
assert_equal ISBN::convert_to_isbn13('9781856075336'), '9781856075336'
|
129
|
+
|
130
|
+
# valid isbn without a check digit
|
131
|
+
assert_equal ISBN::convert_to_isbn13('978185607533'), nil
|
132
|
+
|
133
|
+
#non valid isbns
|
134
|
+
assert_equal ISBN::convert_to_isbn13('651887010820'), nil
|
135
|
+
assert_equal ISBN::convert_to_isbn13('AAAAAAA'), nil
|
136
|
+
assert_equal ISBN::convert_to_isbn13('CX-188'), nil
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_convert_to_isbn13_boundaries
|
141
|
+
assert_equal ISBN::convert_to_isbn13(' '), nil
|
142
|
+
assert_equal ISBN::convert_to_isbn13(nil), nil
|
143
|
+
assert_equal ISBN::convert_to_isbn13(120234203), nil
|
144
|
+
assert_equal ISBN::convert_to_isbn13('18273987123987129387192873912820234203'), nil
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rexml/document'
|
5
|
+
require 'rbook/onix'
|
6
|
+
|
7
|
+
class ContributorTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def test_load_from_element
|
10
|
+
|
11
|
+
file = File.open(File.dirname(__FILE__)+"/../../data/single_product.xml", "r")
|
12
|
+
doc = REXML::Document.new(file)
|
13
|
+
contributor = RBook::Onix::Contributor.load_from_element(REXML::XPath.first(doc, '/ONIXMessage/Product/Contributor'))
|
14
|
+
|
15
|
+
file.close
|
16
|
+
|
17
|
+
assert_equal('0', contributor.sequence_number)
|
18
|
+
assert_equal('A01', contributor.role)
|
19
|
+
assert_equal('Healy, James', contributor.name_inverted)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_load_from_element_not_contributor
|
23
|
+
|
24
|
+
element = REXML::Element.new('NonContributor')
|
25
|
+
element.add_element('SequenceNumber').text = 'blah'
|
26
|
+
|
27
|
+
assert_raise(LoadError) {
|
28
|
+
product = RBook::Onix::Contributor.load_from_element(element)
|
29
|
+
}
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
# ensure product information is output to xml correctly
|
34
|
+
def test_to_element
|
35
|
+
|
36
|
+
contributor = RBook::Onix::Contributor.new
|
37
|
+
contributor.name_inverted = 'Healy, James'
|
38
|
+
contributor.sequence_number = '01'
|
39
|
+
contributor.role = 'A01'
|
40
|
+
|
41
|
+
doc = contributor.to_element
|
42
|
+
|
43
|
+
assert_not_equal(nil,REXML::XPath.first(doc, '/PersonNameInverted'), 'Person Name Inverted must not be nil')
|
44
|
+
assert_equal("Healy, James", REXML::XPath.first(doc, '/PersonNameInverted').text)
|
45
|
+
assert_not_equal(nil,REXML::XPath.first(doc, '/ContributorRole'), 'Contributor Role must not be nil')
|
46
|
+
assert_equal("A01", REXML::XPath.first(doc, '/ContributorRole').text)
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rexml/document'
|
5
|
+
require 'date'
|
6
|
+
require 'rbook/onix'
|
7
|
+
|
8
|
+
class MessageTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
# test loading a valid onix file. Make sure the correct number of products
|
11
|
+
# exist, but don't worry about the content. The product unit tests can check them
|
12
|
+
def test_load_from_xmldoc
|
13
|
+
|
14
|
+
file = File.open(File.dirname(__FILE__)+"/../../data/single_product.xml", "r")
|
15
|
+
doc = REXML::Document.new(file)
|
16
|
+
msg = RBook::Onix::Message.load_from_xmldoc(doc)
|
17
|
+
|
18
|
+
file.close
|
19
|
+
|
20
|
+
#check header elements
|
21
|
+
assert_equal('Chalice', msg.from_company)
|
22
|
+
assert_equal('James Healy', msg.from_person)
|
23
|
+
assert Date.civil(2006, 5, 21) === msg.sent_date
|
24
|
+
|
25
|
+
#check product count
|
26
|
+
assert_equal(1, msg.products.size)
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
# try loading an onix file with no products
|
31
|
+
def test_load_from_xmldoc_invalid_no_product
|
32
|
+
file = File.open(File.dirname(__FILE__)+"/../../data/invalid_no_product.xml", "r")
|
33
|
+
doc = REXML::Document.new(file)
|
34
|
+
|
35
|
+
assert_raise(LoadError) {
|
36
|
+
msg = RBook::Onix::Message.load_from_xmldoc(doc)
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
# test loading an xml file that's not an onix file
|
41
|
+
def test_load_from_xmldoc_invalid_not_onix
|
42
|
+
file = File.open(File.dirname(__FILE__)+"/../../data/xml_not_onix.xml", "r")
|
43
|
+
doc = REXML::Document.new(file)
|
44
|
+
|
45
|
+
assert_raise(LoadError) {
|
46
|
+
msg = RBook::Onix::Message.load_from_xmldoc(doc)
|
47
|
+
}
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
# test loading a non XML file
|
52
|
+
def test_load_from_xmldoc_invalid_not_xml
|
53
|
+
file = File.open(File.dirname(__FILE__)+"/../../data/not_xml.csv", "r")
|
54
|
+
doc = REXML::Document.new(file)
|
55
|
+
|
56
|
+
assert_raise(LoadError) {
|
57
|
+
msg = RBook::Onix::Message.load_from_xmldoc(doc)
|
58
|
+
}
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
# ensure header information is output to xml correctly and that a
|
63
|
+
# product is created
|
64
|
+
def test_to_xml
|
65
|
+
msg = RBook::Onix::Message.new
|
66
|
+
msg.from_person = "James Healy"
|
67
|
+
msg.from_company = "Healy Inc."
|
68
|
+
msg.to_person = "Test::Unit"
|
69
|
+
|
70
|
+
product = RBook::Onix::Product.new
|
71
|
+
product.product_identifier = '0977616606'
|
72
|
+
msg.add_product(product)
|
73
|
+
|
74
|
+
doc = msg.to_xml
|
75
|
+
|
76
|
+
assert_not_equal(nil,REXML::XPath.first(doc, '/ONIXMessage/Header/FromPerson'))
|
77
|
+
assert_equal("James Healy", REXML::XPath.first(doc, '/ONIXMessage/Header/FromPerson').text)
|
78
|
+
assert_not_equal(nil,REXML::XPath.first(doc, '/ONIXMessage/Header/FromCompany'))
|
79
|
+
assert_equal("Healy Inc.", REXML::XPath.first(doc, '/ONIXMessage/Header/FromCompany').text)
|
80
|
+
assert_not_equal(nil,REXML::XPath.first(doc, '/ONIXMessage/Header/ToPerson'))
|
81
|
+
assert_equal("Test::Unit", REXML::XPath.first(doc, '/ONIXMessage/Header/ToPerson').text)
|
82
|
+
assert_not_equal(nil,REXML::XPath.first(doc, '/ONIXMessage/Product'))
|
83
|
+
end
|
84
|
+
|
85
|
+
# test for an exception when a onixmessage with no products is converted to an xml document
|
86
|
+
def test_to_xml_no_product
|
87
|
+
msg = RBook::Onix::Message.new
|
88
|
+
msg.from_person = "James Healy"
|
89
|
+
msg.from_company = "Healy Inc."
|
90
|
+
msg.to_person = "Test::Unit"
|
91
|
+
|
92
|
+
assert_raise(RuntimeError) {
|
93
|
+
doc = msg.to_xml
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_to_s_valid_utf8_charset
|
98
|
+
msg = RBook::Onix::Message.new
|
99
|
+
msg.from_person = "James Healy\x11"
|
100
|
+
msg.from_company = "Healy Inc.\x11"
|
101
|
+
msg.from_email = "someone@example.com\x11"
|
102
|
+
msg.to_person = "Test::Unit\x11"
|
103
|
+
msg.to_company = "Sample Company\x11"
|
104
|
+
msg.message_note = "This is a sample ONIX message\x11"
|
105
|
+
|
106
|
+
product = RBook::Onix::Product.new
|
107
|
+
product.product_identifier = '0977616606'
|
108
|
+
product.title = 'Test Title'
|
109
|
+
msg.add_product(product)
|
110
|
+
|
111
|
+
doc = msg.to_s
|
112
|
+
|
113
|
+
doc.each_byte do |c|
|
114
|
+
n = XChar::CP1252[c] || c
|
115
|
+
n = nil unless XChar::VALID.find {|range| range.include? n}
|
116
|
+
assert_not_equal nil, n, "Non valid UTF8 char detected (code: #{n.to_s})"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|