unapi 0.0.1
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.
- data/lib/unapi.rb +5 -0
- data/lib/unapi/format.rb +21 -0
- data/lib/unapi/page.rb +59 -0
- data/lib/unapi/service.rb +45 -0
- data/lib/unapi/utils.rb +49 -0
- data/lib/unapi/validator.rb +175 -0
- data/test.rb +33 -0
- data/test/index.html +926 -0
- data/test/unapi_servlet.rb +103 -0
- data/test/unapi_test.rb +81 -0
- data/test/validate_test.rb +20 -0
- metadata +60 -0
@@ -0,0 +1,103 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'webrick'
|
4
|
+
require 'rexml/document'
|
5
|
+
require 'net/http'
|
6
|
+
|
7
|
+
# a barely functional unapi server for testing
|
8
|
+
|
9
|
+
class UnAPIServlet < WEBrick::HTTPServlet::AbstractServlet
|
10
|
+
|
11
|
+
def service(request, response)
|
12
|
+
uri = request.query.fetch('uri', nil)
|
13
|
+
format = request.query.fetch('format', nil)
|
14
|
+
|
15
|
+
if not uri and not format
|
16
|
+
doc = REXML::Document.new
|
17
|
+
doc.add_element get_formats
|
18
|
+
response['Content-Type'] = 'application/xml'
|
19
|
+
response.status = 300
|
20
|
+
response.body = doc.to_s
|
21
|
+
|
22
|
+
elsif uri and not format
|
23
|
+
if not has_record(uri)
|
24
|
+
response.status = 404
|
25
|
+
else
|
26
|
+
doc = REXML::Document.new
|
27
|
+
doc.add_element get_formats(uri)
|
28
|
+
response['Content-Type'] = 'application/xml'
|
29
|
+
response.status = 300
|
30
|
+
response.body = doc.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
elsif uri and format
|
34
|
+
record = get_record(uri, format)
|
35
|
+
if not ok_format? format
|
36
|
+
response.status = 415
|
37
|
+
elsif not record
|
38
|
+
response.status = 404
|
39
|
+
else
|
40
|
+
response.status = 200
|
41
|
+
response['Content-Type'] = 'text/xml'
|
42
|
+
response.body = record.to_s
|
43
|
+
end
|
44
|
+
|
45
|
+
else
|
46
|
+
response.status = 400
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def ok_format?(format)
|
54
|
+
return(['mods','dc'].member? format)
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_formats(uri=nil)
|
58
|
+
formats = REXML::Element.new 'formats'
|
59
|
+
if uri
|
60
|
+
uri_element = REXML::Element.new 'uri', formats
|
61
|
+
uri_element.text = uri
|
62
|
+
end
|
63
|
+
formats.add_element get_format('dc', 'text/xml')
|
64
|
+
formats.add_element get_format('mods', 'text/xml')
|
65
|
+
return formats
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_format(name, type)
|
69
|
+
format = REXML::Element.new 'format'
|
70
|
+
name_element = REXML::Element.new 'name', format
|
71
|
+
name_element.text = name
|
72
|
+
type_element = REXML::Element.new 'type', format
|
73
|
+
type_element.text = type
|
74
|
+
return format
|
75
|
+
end
|
76
|
+
|
77
|
+
def has_record(uri)
|
78
|
+
ids = ['urn:isbn:0553804790', 'urn:isbn:030723827X', 'urn:isbn:0307236579']
|
79
|
+
return(ids.member? uri)
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_record(uri, format)
|
83
|
+
if has_record(uri)
|
84
|
+
case format
|
85
|
+
when 'dc' : return(REXML::Element.new 'dc')
|
86
|
+
when 'mods' : return(REXML::Element.new 'mods')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
return nil
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
# run this file and it will start an http server on port 9999
|
96
|
+
# with the servlet at http://localhost:9999/unapi
|
97
|
+
|
98
|
+
if $0 == __FILE__
|
99
|
+
server = WEBrick::HTTPServer.new(:Port => 9999, :DocumentRoot => 'test')
|
100
|
+
server.mount("/unapi", UnAPIServlet)
|
101
|
+
trap("INT") {server.shutdown}
|
102
|
+
server.start
|
103
|
+
end
|
data/test/unapi_test.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'unapi'
|
2
|
+
|
3
|
+
class UnAPITest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_service_url
|
6
|
+
page = UnAPI::Page.new 'http://localhost:9999/index.html'
|
7
|
+
assert_equal page.service_url, 'http://localhost:9999/unapi'
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_uris
|
11
|
+
page = UnAPI::Page.new 'http://localhost:9999/index.html'
|
12
|
+
assert_equal [ 'urn:isbn:0553804790', 'urn:isbn:030723827X',
|
13
|
+
'urn:isbn:0307236579'], page.uris
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_service
|
17
|
+
page = UnAPI::Page.new 'http://localhost:9999/index.html'
|
18
|
+
service = page.service
|
19
|
+
assert_equal service.class, UnAPI::Service
|
20
|
+
assert_equal service.url, 'http://localhost:9999/unapi'
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_formats
|
24
|
+
service = UnAPI::Service.new 'http://localhost:9999/unapi'
|
25
|
+
formats = service.formats
|
26
|
+
assert_equal 2, formats.length
|
27
|
+
assert_equal formats[0].name, 'dc'
|
28
|
+
assert_equal formats[0].type, 'text/xml'
|
29
|
+
assert_equal formats[1].name, 'mods'
|
30
|
+
assert_equal formats[1].type, 'text/xml'
|
31
|
+
assert_equal 300, service.status_code
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_bad_service_url
|
35
|
+
service = UnAPI::Service.new 'http://foobar'
|
36
|
+
formats = service.formats
|
37
|
+
assert 0, formats.length
|
38
|
+
assert_equal nil, service.status_code
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_uri_formats
|
42
|
+
service = UnAPI::Service.new 'http://localhost:9999/unapi'
|
43
|
+
formats = service.formats_for_uri('urn:isbn:0553804790')
|
44
|
+
assert_equal 2, formats.length
|
45
|
+
assert_equal formats[0].name, 'dc'
|
46
|
+
assert_equal formats[0].type, 'text/xml'
|
47
|
+
assert_equal formats[1].name, 'mods'
|
48
|
+
assert_equal formats[1].type, 'text/xml'
|
49
|
+
assert_equal 300, service.status_code
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_bad_uri_formats
|
53
|
+
service = UnAPI::Service.new 'http://localhost:9999/unapi'
|
54
|
+
formats = service.formats_for_uri('urn:isbn:foobarf')
|
55
|
+
assert_equal 0, formats.length
|
56
|
+
assert_equal 404, service.status_code
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_get_uri_in_format
|
60
|
+
service = UnAPI::Service.new 'http://localhost:9999/unapi'
|
61
|
+
uri = 'urn:isbn:0553804790'
|
62
|
+
assert_equal '<dc/>', service.get_uri_in_format(uri, 'dc')
|
63
|
+
assert_equal 200, service.status_code
|
64
|
+
assert_equal '<mods/>', service.get_uri_in_format(uri, 'mods')
|
65
|
+
assert_equal 200, service.status_code
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_get_bad_uri
|
69
|
+
service = UnAPI::Service.new 'http://localhost:9999/unapi'
|
70
|
+
assert_equal nil, service.get_uri_in_format('foobar', 'dc')
|
71
|
+
assert_equal 404, service.status_code
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_get_bad_uri_in_format
|
75
|
+
uri = 'urn:isbn:0553804790'
|
76
|
+
service = UnAPI::Service.new 'http://localhost:9999/unapi'
|
77
|
+
assert_equal nil, service.get_uri_in_format(uri, 'cheese')
|
78
|
+
assert_equal 415, service.status_code
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test/unit/testcase'
|
2
|
+
require 'stringio'
|
3
|
+
require 'unapi'
|
4
|
+
|
5
|
+
class ValidateTest < Test::Unit::TestCase
|
6
|
+
def test_print_handler
|
7
|
+
buffer = StringIO.new
|
8
|
+
handler = UnAPI::PrintHandler.new(buffer)
|
9
|
+
validator = UnAPI::Validator.new(handler)
|
10
|
+
validator.validate_page('http://localhost:9999/unapi')
|
11
|
+
assert buffer.to_s.length > 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_output_to_array
|
15
|
+
handler = UnAPI::ListHandler.new
|
16
|
+
validator = UnAPI::Validator.new(handler)
|
17
|
+
validator.validate_page('http://localhost:9999/unapi')
|
18
|
+
assert handler.messages.length > 0
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: unapi
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2006-03-13 00:00:00 -06:00
|
8
|
+
summary: A library for working with the unapi protocol
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: ehs@pobox.com
|
12
|
+
homepage: http://www.textualize.com/unapi
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: unapi
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
signing_key:
|
28
|
+
cert_chain:
|
29
|
+
authors:
|
30
|
+
- Ed Summers
|
31
|
+
files:
|
32
|
+
- lib/unapi
|
33
|
+
- lib/unapi.rb
|
34
|
+
- lib/unapi/format.rb
|
35
|
+
- lib/unapi/page.rb
|
36
|
+
- lib/unapi/service.rb
|
37
|
+
- lib/unapi/utils.rb
|
38
|
+
- lib/unapi/validator.rb
|
39
|
+
- test/index.html
|
40
|
+
- test/unapi_servlet.rb
|
41
|
+
- test/unapi_test.rb
|
42
|
+
- test/validate_test.rb
|
43
|
+
test_files:
|
44
|
+
- test.rb
|
45
|
+
rdoc_options: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
executables: []
|
48
|
+
extensions: []
|
49
|
+
requirements: []
|
50
|
+
dependencies:
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rubyful_soup
|
53
|
+
version_requirement:
|
54
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
55
|
+
requirements:
|
56
|
+
-
|
57
|
+
- ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 1.0.4
|
60
|
+
version:
|