Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ gemspec
gem 'jruby-openssl', :platform => :jruby

group :test do
gem 'activerecord'
gem 'activerecord', '~> 3.2.8'
gem 'activerecord-jdbcsqlite3-adapter', :platform => [:jruby]
gem 'libxml-ruby', :platform => [:ruby, :mswin]
gem 'rake'
Expand Down
11 changes: 5 additions & 6 deletions lib/oai/provider/metadata_format/oai_dc.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module OAI::Provider::Metadata
# = OAI::Metadata::DublinCore
#

# Simple implementation of the Dublin Core metadata format.
class DublinCore < Format

def initialize
@prefix = 'oai_dc'
@schema = 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd'
Expand All @@ -19,9 +18,9 @@ def header_specification
'xmlns:oai_dc' => "http://www.openarchives.org/OAI/2.0/oai_dc/",
'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' =>
%{http://www.openarchives.org/OAI/2.0/oai_dc/
http://www.openarchives.org/OAI/2.0/oai_dc.xsd}
'xsi:schemaLocation' =>
%{http://www.openarchives.org/OAI/2.0/oai_dc/
http://www.openarchives.org/OAI/2.0/oai_dc.xsd}.gsub(/\s+/, ' ')
}
end

Expand Down
60 changes: 30 additions & 30 deletions lib/oai/provider/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
module OAI
module Provider
module Response

class Base
attr_reader :provider, :options

class << self
attr_reader :valid_options, :default_options, :required_options
def valid_parameters(*args)
@valid_options ||= []
@valid_options = (@valid_options + args.dup).uniq
end

def default_parameters(options = {})
@default_options ||= {}
@default_options.merge! options.dup
end

def required_parameters(*args)
valid_parameters(*args)
@required_options ||= []
@required_options = (@required_options + args.dup).uniq
end
end

end
def initialize(provider, options = {})
@provider = provider
@options = internalize(options)
Expand All @@ -33,88 +33,88 @@ def initialize(provider, options = {})
def response
@builder = Builder::XmlMarkup.new
@builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
@builder.tag!('OAI-PMH', header) do
@builder.tag!('OAI-PMH', header) do
@builder.responseDate Time.now.utc.xmlschema
#options parameter has been removed here because with it
#the data won't validate against oai validators. Without, it
#validates.
@builder.request(provider.url) #-- OAI 2.0 Hack - removed request options
#the data won't validate against oai validators. Without, it
#validates.
@builder.request(provider.url) #-- OAI 2.0 Hack - removed request options
yield @builder
end
end
private

def header
{
{
'xmlns' => "http://www.openarchives.org/OAI/2.0/",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' => %{http://www.openarchives.org/OAI/2.0/
http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd}
http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd}.gsub(/\s+/, ' ')
}
end
def extract_identifier(id)
id.sub("#{provider.prefix}/", '')
end

def valid?
return true if resumption?

return true if self.class.valid_options.nil? and options.empty?
# check if the request includes an argument and there are no valid

# check if the request includes an argument and there are no valid
# arguments for that verb (Identify, for example).
raise OAI::ArgumentException.new if self.class.valid_options.nil? && !options.empty?

if self.class.required_options
return false unless (self.class.required_options - @options.keys).empty?
end
return false unless (@options.keys - self.class.valid_options).empty?
populate_defaults
end

def populate_defaults
self.class.default_options.each do |k,v|
@options[k] = v.respond_to?(:call) ? v.call(self) : v if not @options[k]
end
end

def resumption?
if @options.keys.include?(:resumption_token)
if @options.keys.include?(:resumption_token)
return true if 1 == @options.keys.size
raise OAI::ArgumentException.new
end
end

# Convert our internal representations back into standard OAI options
def externalize(value)
value.to_s.gsub(/_[a-z]/) { |m| m.sub("_", '').capitalize }
end

def parse_date(value)
return value if value.respond_to?(:strftime)

Date.parse(value) # This will raise an exception for badly formatted dates
Time.parse(value).utc # -- UTC Bug fix hack 8/08 not in core
rescue
raise OAI::ArgumentError.new
raise OAI::ArgumentError.new
end

def internalize(hash = {})
internal = {}
hash.keys.each do |key|
internal[key.to_s.gsub(/([A-Z])/, '_\1').downcase.intern] = hash[key].dup
end

# Convert date formated strings into internal time values
# Convert date formated strings in dates.
internal[:from] = parse_date(internal[:from]) if internal[:from]
internal[:until] = parse_date(internal[:until]) if internal[:until]

internal
end

end

end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/oai/provider/response/get_record.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module OAI::Provider::Response

class GetRecord < RecordResponse
required_parameters :identifier
required_parameters :identifier, :metadata_prefix

def to_xml
id = extract_identifier(options.delete(:identifier))
unless record = provider.model.find(id, options)
Expand All @@ -11,17 +11,17 @@ def to_xml

response do |r|
r.GetRecord do
r.record do
r.record do
header_for record
data_for record unless deleted?(record)
about_for record unless deleted?(record)
end
end
end
end

end

end


9 changes: 5 additions & 4 deletions lib/oai/provider/response/list_records.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module OAI::Provider::Response

class ListRecords < RecordResponse

required_parameters :metadata_prefix

def to_xml
result = provider.model.find(:all, options)
# result may be an array of records, or a partial result
Expand All @@ -27,8 +28,8 @@ def to_xml
end
end
end

end

end

28 changes: 14 additions & 14 deletions lib/oai/provider/response/record_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module OAI::Provider::Response
class RecordResponse < Base
def self.inherited(klass)
klass.valid_parameters :metadata_prefix, :from, :until, :set
klass.default_parameters :metadata_prefix => "oai_dc",
klass.default_parameters :metadata_prefix => "oai_dc",
:from => Proc.new {|x| Time.parse(x.provider.model.earliest.to_s) }, #-- OAI 2.0 hack - UTC
:until => Proc.new {|x| Time.parse(x.provider.model.latest.to_s) } #-- OAI 2.0 hack - UTC
end

# emit record header
def header_for(record)
param = Hash.new
param[:status] = 'deleted' if deleted?(record)
@builder.header param do
@builder.header param do
@builder.identifier identifier_for(record)
@builder.datestamp timestamp_for(record)
sets_for(record).each do |set|
Expand All @@ -33,7 +33,7 @@ def about_for(record)
return unless provider.model.respond_to? :about

about = provider.model.about(record)
return if about.nil?
return if about.nil?

unless about.is_a? Array
about = [about]
Expand All @@ -42,43 +42,43 @@ def about_for(record)
about.each do |a|
@builder.about do
@builder.target! << a
end
end
end
end

private

def identifier_for(record)
"#{provider.prefix}/#{record.id}"
end

def timestamp_for(record)
record.send(provider.model.timestamp_field).utc.xmlschema
end

def sets_for(record)
return [] unless record.respond_to?(:sets) and record.sets
record.sets.respond_to?(:each) ? record.sets : [record.sets]
end

def requested_format
format =
format =
if options[:metadata_prefix]
options[:metadata_prefix]
elsif options[:resumption_token]
OAI::Provider::ResumptionToken.extract_format(options[:resumption_token])
end
raise OAI::FormatException.new unless provider.format_supported?(format)

format
end

def deleted?(record)
return record.deleted? if record.respond_to?(:deleted?)
return record.deleted if record.respond_to?(:deleted)
return record.deleted_at if record.respond_to?(:deleted_at)
false
end

end
end
Loading