Skip to content

Commit 402c14f

Browse files
jamespriorbarmintor
authored andcommitted
Stricter ISO 8601 date parsing
1 parent 5de71a9 commit 402c14f

File tree

6 files changed

+61
-17
lines changed

6 files changed

+61
-17
lines changed

lib/oai/client/response.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ def initialize(doc, &resumption_block)
3737
message = error.content
3838
code = ""
3939
if defined?(error.property) == nil
40-
code = error.attributes['code']
41-
else
42-
begin
43-
code = error["code"]
44-
rescue
45-
code = error.property('code')
46-
end
40+
code = error.attributes['code']
41+
else
42+
begin
43+
code = error["code"]
44+
rescue
45+
code = error.property('code')
4746
end
47+
end
4848
end
4949
raise OAI::Exception.new(message, code)
5050
end

lib/oai/provider/model/activerecord_wrapper.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,20 @@ def sql_conditions(opts)
183183
private
184184

185185
def parse_to_local(time)
186-
time_obj = Time.parse(time.to_s)
186+
if time.respond_to?(:strftime)
187+
time_obj = time
188+
else
189+
begin
190+
if time.ends_with?("Z")
191+
time_obj = Time.strptime(time, "%Y-%m-%dT%H:%M:%SZ")
192+
else
193+
time_obj = Time.strptime(time, "%Y-%m-%d")
194+
end
195+
rescue
196+
raise OAI::ArgumentException.new, "unparsable date: '#{time}'"
197+
end
198+
end
199+
187200
time_obj = yield(time_obj) if block_given?
188201
# Convert to same as DB - :local => :getlocal, :utc => :getutc
189202

lib/oai/provider/response.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ def externalize(value)
9090

9191
def parse_date(value)
9292
return value if value.respond_to?(:strftime)
93-
94-
Date.parse(value) # This will raise an exception for badly formatted dates
95-
Time.parse(value).utc # -- UTC Bug fix hack 8/08 not in core
96-
rescue
93+
94+
if provider.granularity == OAI::Const::Granularity::HIGH
95+
Time.strptime(value, "%Y-%m-%dT%H:%M:%SZ").utc
96+
else
97+
Time.strptime(value, "%Y-%m-%d").utc
98+
end
99+
rescue ArgumentError => e
97100
raise OAI::ArgumentException.new, "unparsable date: '#{value}'"
98101
end
99102

test/activerecord_provider/tc_ar_provider.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_from
8686
DCField.where("id < #{first_id + 10}").update_all(updated_at: Time.parse("June 1 2005"))
8787

8888

89-
from_param = Time.parse("January 1 2006")
89+
from_param = Time.parse("January 1 2006").getutc.iso8601
9090

9191
doc = REXML::Document.new(
9292
@provider.list_records(
@@ -97,7 +97,7 @@ def test_from
9797

9898
doc = REXML::Document.new(
9999
@provider.list_records(
100-
:metadata_prefix => 'oai_dc', :from => Time.parse("May 30 2005"))
100+
:metadata_prefix => 'oai_dc', :from => Time.parse("May 30 2005").getutc.iso8601)
101101
)
102102
assert_equal 20, doc.elements['OAI-PMH/ListRecords'].to_a.size
103103
end
@@ -122,11 +122,38 @@ def test_from_and_until
122122
doc = REXML::Document.new(
123123
@provider.list_records(
124124
:metadata_prefix => 'oai_dc',
125-
:from => Time.parse("June 3 2005"),
126-
:until => Time.parse("June 16 2005"))
125+
:from => Time.parse("June 3 2005").getutc.iso8601,
126+
:until => Time.parse("June 16 2005").getutc.iso8601)
127127
)
128128
assert_equal 40, doc.elements['OAI-PMH/ListRecords'].to_a.size
129129
end
130+
131+
def test_bad_until_raises_exception
132+
DCField.order('id asc').limit(10).update_all(updated_at: 1.year.ago)
133+
DCField.order('id desc').limit(10).update_all(updated_at: 1.year.from_now)
134+
badTimes = [
135+
'junk',
136+
'February 92nd, 2015']
137+
badTimes.each do |time|
138+
assert_raise(OAI::ArgumentException) do
139+
@provider.list_records(:metadata_prefix => 'oai_dc', :until => time)
140+
end
141+
end
142+
end
143+
144+
def test_bad_from_raises_exception
145+
DCField.order('id asc').limit(10).update_all(updated_at: 1.year.ago)
146+
DCField.order('id desc').limit(10).update_all(updated_at: 1.year.from_now)
147+
148+
badTimes = [
149+
'junk',
150+
'February 92nd, 2015']
151+
badTimes.each do |time|
152+
assert_raise(OAI::ArgumentException) do
153+
@provider.list_records(:metadata_prefix => 'oai_dc', :from => time)
154+
end
155+
end
156+
end
130157

131158
def test_handles_empty_collections
132159
DCField.delete_all

test/client/helpers/provider.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ComplexClientProvider < OAI::Provider::Base
1010
source_model ComplexModel.new(100)
1111
end
1212

13-
attr_reader :consumed, :server
13+
attr_reader :consumed, :server, :provider
1414

1515
def initialize(port, mount_point)
1616
@consumed = []

test/client/tc_list_identifiers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def test_list_full
4747
end
4848

4949
def test_list_with_date_range
50+
$provider_server.provider.class.update_granularity OAI::Const::Granularity::LOW
5051
client = OAI::Client.new 'http://localhost:3333/oai'
5152
from_date = Date.new(1998,1,1)
5253
until_date = Date.new(2002,1,1)

0 commit comments

Comments
 (0)