oai 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/oai/client.rb +18 -4
- data/lib/oai/record.rb +6 -0
- data/test.rb +1 -0
- data/test/tc_exception.rb +21 -0
- metadata +3 -2
data/lib/oai/client.rb
CHANGED
@@ -2,6 +2,7 @@ require 'uri'
|
|
2
2
|
require 'net/http'
|
3
3
|
require 'rexml/document'
|
4
4
|
require 'cgi'
|
5
|
+
require 'date'
|
5
6
|
|
6
7
|
module OAI
|
7
8
|
|
@@ -9,7 +10,7 @@ module OAI
|
|
9
10
|
# a OAI-PMH server. The 6 OAI-PMH verbs translate directly to methods you
|
10
11
|
# can call on a OAI::Client object. Verb arguments are passed as a hash:
|
11
12
|
#
|
12
|
-
# client = OAI::
|
13
|
+
# client = OAI::Client.new ''http://www.pubmedcentral.gov/oai/oai.cgi'
|
13
14
|
# client.list_identifiers :metadata_prefix => 'oai_dc'
|
14
15
|
#
|
15
16
|
# It is worth noting that the api uses methods and parameter names with
|
@@ -32,9 +33,14 @@ module OAI
|
|
32
33
|
# service:
|
33
34
|
#
|
34
35
|
# client = OAI::Harvseter.new 'http://www.pubmedcentral.gov/oai/oai.cgi'
|
36
|
+
#
|
37
|
+
# If you want to see debugging messages on STDERR use:
|
38
|
+
#
|
39
|
+
# client = OAI::Harvester.new 'http://example.com', :debug => true
|
35
40
|
|
36
|
-
def initialize(base_url)
|
41
|
+
def initialize(base_url, options={})
|
37
42
|
@base = URI.parse base_url
|
43
|
+
@debug = options[:debug]
|
38
44
|
end
|
39
45
|
|
40
46
|
# Equivalent to a Identify request. You'll get back a OAI::IdentifyResponse
|
@@ -123,13 +129,17 @@ module OAI
|
|
123
129
|
"#{key}=#{value}"
|
124
130
|
end
|
125
131
|
uri.query = parts.join('&')
|
132
|
+
debug("doing request: #{uri.to_s}")
|
126
133
|
|
127
134
|
# fire off the request and return an REXML::Document object
|
128
135
|
begin
|
129
136
|
xml = Net::HTTP.get(uri)
|
137
|
+
debug("got response: #{xml}")
|
130
138
|
return REXML::Document.new(xml)
|
131
|
-
rescue
|
132
|
-
raise OAI::Exception, '
|
139
|
+
rescue REXML::ParseException => e
|
140
|
+
raise OAI::Exception, 'response not well formed XML: '+e, caller
|
141
|
+
rescue SystemCallError=> e
|
142
|
+
raise OAI::Exception, 'HTTP level error during OAI request: '+e, caller
|
133
143
|
end
|
134
144
|
end
|
135
145
|
|
@@ -159,5 +169,9 @@ module OAI
|
|
159
169
|
end
|
160
170
|
end
|
161
171
|
end
|
172
|
+
|
173
|
+
def debug(msg)
|
174
|
+
$stderr.print("#{msg}\n") if @debug
|
175
|
+
end
|
162
176
|
end
|
163
177
|
end
|
data/lib/oai/record.rb
CHANGED
@@ -1,4 +1,10 @@
|
|
1
1
|
module OAI
|
2
|
+
|
3
|
+
# A class for representing a Record as returned from a GetRecord
|
4
|
+
# or ListRecords request. Each record will have a header and metadata
|
5
|
+
# attribute. The header is a OAI::Header object and the metadata is
|
6
|
+
# a REXML::Element object for that chunk of XML.
|
7
|
+
|
2
8
|
class Record
|
3
9
|
include OAI::XPath
|
4
10
|
attr_accessor :header, :metadata
|
data/test.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
class ExceptionTest < Test::Unit::TestCase
|
2
|
+
|
3
|
+
def test_http_error
|
4
|
+
client = OAI::Client.new 'http://www.example.com'
|
5
|
+
begin
|
6
|
+
client.identify
|
7
|
+
flunk 'did not throw expected exception'
|
8
|
+
rescue OAI::Exception => e
|
9
|
+
assert_match /Connection refused/, e.to_s, 'include error message'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_xml_error
|
14
|
+
client = OAI::Client.new 'http://www.google.com'
|
15
|
+
begin
|
16
|
+
client.identify
|
17
|
+
rescue OAI::Exception => e
|
18
|
+
assert_match /response not well formed XML/, e.to_s, 'xml error'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: oai
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2006-
|
6
|
+
version: 0.0.2
|
7
|
+
date: 2006-05-15 00:00:00 -05:00
|
8
8
|
summary: A ruby library for working with the Open Archive Initiative Protocol for Metadata Harvesting (OAI-PMH)
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- lib/oai/response.rb
|
46
46
|
- lib/oai/set.rb
|
47
47
|
- lib/oai/xpath.rb
|
48
|
+
- test/tc_exception.rb
|
48
49
|
- test/tc_get_record.rb
|
49
50
|
- test/tc_identify.rb
|
50
51
|
- test/tc_list_identifiers.rb
|