Skip to content

Commit 1b417f3

Browse files
committed
ensure default from/until constraint values have valid granularity
- inspect opposite constraint and match type if present - make private OAI::Provider::Response::Base#parse_date available as protected class method - fixes #104
1 parent e2ea4f0 commit 1b417f3

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

lib/oai/provider/response.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ def response
4040
yield @builder
4141
end
4242
end
43+
44+
protected
45+
46+
def self.parse_date(value)
47+
return value if value.respond_to?(:strftime)
48+
49+
if value[-1] == "Z"
50+
Time.strptime(value, "%Y-%m-%dT%H:%M:%S%Z").utc
51+
else
52+
Date.strptime(value, "%Y-%m-%d")
53+
end
54+
rescue ArgumentError => e
55+
raise OAI::ArgumentException.new, "unparsable date: '#{value}'"
56+
end
57+
4358
private
4459

4560
def header
@@ -89,15 +104,7 @@ def externalize(value)
89104
end
90105

91106
def parse_date(value)
92-
return value if value.respond_to?(:strftime)
93-
94-
if value[-1] == "Z"
95-
Time.strptime(value, "%Y-%m-%dT%H:%M:%S%Z").utc
96-
else
97-
Date.strptime(value, "%Y-%m-%d")
98-
end
99-
rescue ArgumentError => e
100-
raise OAI::ArgumentException.new, "unparsable date: '#{value}'"
107+
self.class.parse_date(value)
101108
end
102109

103110
def internalize(hash = {})

lib/oai/provider/response/record_response.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@ class RecordResponse < Base
33
def self.inherited(klass)
44
klass.valid_parameters :metadata_prefix, :from, :until, :set
55
klass.default_parameters :metadata_prefix => "oai_dc",
6-
:from => Proc.new {|x| Time.parse(x.provider.model.earliest.to_s) }, #-- OAI 2.0 hack - UTC
7-
:until => Proc.new {|x| Time.parse(x.provider.model.latest.to_s) } #-- OAI 2.0 hack - UTC
6+
:from => method(:default_from).to_proc,
7+
:until => method(:default_until).to_proc
8+
end
9+
10+
def self.default_from(response)
11+
value = Time.parse(response.provider.model.earliest.to_s).utc
12+
if response.options[:until]
13+
u = parse_date(response.options[:until])
14+
value = value.to_date if u.is_a? Date
15+
end
16+
value
17+
end
18+
19+
def self.default_until(response)
20+
value = Time.parse(response.provider.model.latest.to_s).utc
21+
if response.options[:from]
22+
f = parse_date(response.options[:from])
23+
value = value.to_date if f.is_a? Date
24+
end
25+
value
826
end
927

1028
# emit record header

test/provider/tc_simple_provider.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ def test_list_records_without_constraints
4949
assert_equal total, doc.elements['OAI-PMH/ListRecords'].size
5050
end
5151

52+
def test_list_records_with_from_constraints
53+
assert_nothing_raised { REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc')) }
54+
55+
total = @model.find(:all).size
56+
doc = REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc', from: "2002-10-05"))
57+
assert_equal total, doc.elements['OAI-PMH/ListRecords'].size
58+
end
59+
60+
def test_list_records_with_until_constraints
61+
assert_nothing_raised { REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc')) }
62+
63+
total = @model.find(:all).size
64+
doc = REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc', until: "2002-11-05"))
65+
assert_equal total, doc.elements['OAI-PMH/ListRecords'].size
66+
end
67+
5268
def test_list_records_with_set_equal_a
5369
total = @model.find(:all, :set => 'A').size
5470
doc = REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc', :set => 'A'))

0 commit comments

Comments
 (0)