Skip to content

Commit f2ab313

Browse files
jamespriorbarmintor
jamesprior
authored andcommitted
From/Until may be Time or Date depending on granularity
1 parent 91b9e63 commit f2ab313

File tree

7 files changed

+68
-13
lines changed

7 files changed

+68
-13
lines changed

examples/models/file_model.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def find(selector, opts={})
4545
case selector
4646
when :all
4747
records = Dir["#{@directory}/*.xml"].sort.collect do |file|
48-
File.new(file) unless File.stat(file).mtime.utc < opts[:from] or
49-
File.stat(file).mtime.utc > opts[:until]
48+
File.new(file) unless File.stat(file).mtime.utc < opts[:from].to_time or
49+
File.stat(file).mtime.utc > opts[:until].to_time
5050
end
5151
records
5252
else

lib/oai/provider/model/activerecord_wrapper.rb

+11-6
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def sql_conditions(opts)
177177
sql << "#{timestamp_field} < :until"
178178
esc_values[:until] = parse_to_local(opts[:until]) { |t| t + 1 }
179179
end
180+
180181
return [sql.join(" AND "), esc_values]
181182
end
182183

@@ -190,22 +191,26 @@ def parse_to_local(time)
190191
if time[-1] == "Z"
191192
time_obj = Time.strptime(time, "%Y-%m-%dT%H:%M:%S%Z")
192193
else
193-
time_obj = Time.strptime(time, "%Y-%m-%d")
194+
time_obj = Date.strptime(time, "%Y-%m-%d")
194195
end
195196
rescue
196197
raise OAI::ArgumentException.new, "unparsable date: '#{time}'"
197198
end
198199
end
199200

200201
time_obj = yield(time_obj) if block_given?
201-
# Convert to same as DB - :local => :getlocal, :utc => :getutc
202202

203-
if ActiveRecord::VERSION::MAJOR >= 7
204-
tzconv = "get#{ActiveRecord.default_timezone.to_s}".to_sym
203+
if time_obj.kind_of?(Date)
204+
time_obj.strftime("%Y-%m-%d")
205205
else
206-
tzconv = "get#{model.default_timezone.to_s}".to_sym
206+
# Convert to same as DB - :local => :getlocal, :utc => :getutc
207+
if ActiveRecord::VERSION::MAJOR >= 7
208+
tzconv = "get#{ActiveRecord.default_timezone.to_s}".to_sym
209+
else
210+
tzconv = "get#{model.default_timezone.to_s}".to_sym
211+
end
212+
time_obj.send(tzconv).strftime("%Y-%m-%d %H:%M:%S")
207213
end
208-
time_obj.send(tzconv).strftime("%Y-%m-%d %H:%M:%S")
209214
end
210215

211216
end

lib/oai/provider/response.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def parse_date(value)
9494
if value[-1] == "Z"
9595
Time.strptime(value, "%Y-%m-%dT%H:%M:%S%Z").utc
9696
else
97-
Time.strptime(value, "%Y-%m-%d").utc
97+
Date.strptime(value, "%Y-%m-%d")
9898
end
9999
rescue ArgumentError => e
100100
raise OAI::ArgumentException.new, "unparsable date: '#{value}'"

lib/oai/provider/response/list_records.rb

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ module OAI::Provider::Response
22

33
class ListRecords < RecordResponse
44
required_parameters :metadata_prefix
5+
6+
def valid?
7+
super && matching_granularity?
8+
end
9+
10+
def matching_granularity?
11+
if options[:from].nil? == false && options[:until].nil? == false && options[:from].class.name != options[:until].class.name
12+
raise OAI::ArgumentException.new, "The 'from' and 'until' options specified must have the same granularity"
13+
else
14+
true
15+
end
16+
end
517

618
def to_xml
719
result = provider.model.find(:all, options)

lib/oai/provider/resumption_token.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,20 @@ def encode_conditions
126126

127127
encoded_token = @prefix.to_s.dup
128128
encoded_token << ".s(#{set})" if set
129-
encoded_token << ".f(#{self.from.utc.xmlschema})" if self.from
130-
encoded_token << ".u(#{self.until.utc.xmlschema})" if self.until
129+
if self.from
130+
if self.from.respond_to?(:utc)
131+
encoded_token << ".f(#{self.from.utc.xmlschema})"
132+
else
133+
encoded_token << ".f(#{self.from.xmlschema})"
134+
end
135+
end
136+
if self.until
137+
if self.until.respond_to?(:utc)
138+
encoded_token << ".u(#{self.until.utc.xmlschema})"
139+
else
140+
encoded_token << ".u(#{self.until.xmlschema})"
141+
end
142+
end
131143
encoded_token << ":#{last_str}"
132144
end
133145

test/provider/models.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def find(selector, opts={})
7676
else
7777
records = @records.select do |rec|
7878
((opts[:set].nil? || rec.in_set(opts[:set])) &&
79-
(opts[:from].nil? || rec.updated_at >= opts[:from]) &&
80-
(opts[:until].nil? || rec.updated_at <= opts[:until]))
79+
(opts[:from].nil? || rec.updated_at >= opts[:from].to_time) &&
80+
(opts[:until].nil? || rec.updated_at <= opts[:until].to_time))
8181
#else
8282
# ((opts[:set].nil? || rec.in_set(opts[:set])) &&
8383
# (opts[:from].nil? || rec.updated_at >= opts[:from]) &&

test/provider/tc_provider.rb

+26
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ def test_until
7878
)
7979
assert_equal 100, doc.elements['OAI-PMH/ListRecords'].to_a.size
8080
end
81+
82+
def test_from_and_until_match
83+
assert_nothing_raised do
84+
@big_provider.list_records(
85+
:metadata_prefix => 'oai_dc',
86+
:from => "2000-11-01T05:00:00Z",
87+
:until => "2000-11-30T05:00:00Z"
88+
)
89+
end
90+
91+
assert_nothing_raised do
92+
@big_provider.list_records(
93+
:metadata_prefix => 'oai_dc',
94+
:from => "2000-11-01",
95+
:until => "2000-11-30"
96+
)
97+
end
98+
99+
assert_raise(OAI::ArgumentException) do
100+
@big_provider.list_records(
101+
:metadata_prefix => 'oai_dc',
102+
:from => "2000-11-01T05:00:00Z",
103+
:until => "2000-11-30"
104+
)
105+
end
106+
end
81107

82108
def test_from_and_until
83109
assert_nothing_raised do

0 commit comments

Comments
 (0)