22module OAI
33 module Provider
44 module Response
5-
5+
66 class Base
77 attr_reader :provider , :options
8-
8+
99 class << self
1010 attr_reader :valid_options , :default_options , :required_options
1111 def valid_parameters ( *args )
1212 @valid_options ||= [ ]
1313 @valid_options = ( @valid_options + args . dup ) . uniq
1414 end
15-
15+
1616 def default_parameters ( options = { } )
1717 @default_options ||= { }
1818 @default_options . merge! options . dup
1919 end
20-
20+
2121 def required_parameters ( *args )
2222 valid_parameters ( *args )
2323 @required_options ||= [ ]
2424 @required_options = ( @required_options + args . dup ) . uniq
2525 end
26-
27- end
26+
27+ end
2828 def initialize ( provider , options = { } )
2929 @provider = provider
3030 @options = internalize ( options )
@@ -33,88 +33,88 @@ def initialize(provider, options = {})
3333 def response
3434 @builder = Builder ::XmlMarkup . new
3535 @builder . instruct! :xml , :version => "1.0" , :encoding => "UTF-8"
36- @builder . tag! ( 'OAI-PMH' , header ) do
36+ @builder . tag! ( 'OAI-PMH' , header ) do
3737 @builder . responseDate Time . now . utc . xmlschema
3838 #options parameter has been removed here because with it
39- #the data won't validate against oai validators. Without, it
40- #validates.
41- @builder . request ( provider . url ) #-- OAI 2.0 Hack - removed request options
39+ #the data won't validate against oai validators. Without, it
40+ #validates.
41+ @builder . request ( provider . url ) #-- OAI 2.0 Hack - removed request options
4242 yield @builder
4343 end
4444 end
4545 private
46-
46+
4747 def header
48- {
48+ {
4949 'xmlns' => "http://www.openarchives.org/OAI/2.0/" ,
5050 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance" ,
5151 'xsi:schemaLocation' => %{http://www.openarchives.org/OAI/2.0/
52- http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd}
52+ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd} . gsub ( / \s +/ , ' ' )
5353 }
5454 end
5555 def extract_identifier ( id )
5656 id . sub ( "#{ provider . prefix } /" , '' )
5757 end
58-
58+
5959 def valid?
6060 return true if resumption?
61-
61+
6262 return true if self . class . valid_options . nil? and options . empty?
63-
64- # check if the request includes an argument and there are no valid
63+
64+ # check if the request includes an argument and there are no valid
6565 # arguments for that verb (Identify, for example).
6666 raise OAI ::ArgumentException . new if self . class . valid_options . nil? && !options . empty?
67-
67+
6868 if self . class . required_options
6969 return false unless ( self . class . required_options - @options . keys ) . empty?
7070 end
7171 return false unless ( @options . keys - self . class . valid_options ) . empty?
7272 populate_defaults
7373 end
74-
74+
7575 def populate_defaults
7676 self . class . default_options . each do |k , v |
7777 @options [ k ] = v . respond_to? ( :call ) ? v . call ( self ) : v if not @options [ k ]
7878 end
7979 end
80-
80+
8181 def resumption?
82- if @options . keys . include? ( :resumption_token )
82+ if @options . keys . include? ( :resumption_token )
8383 return true if 1 == @options . keys . size
8484 raise OAI ::ArgumentException . new
8585 end
8686 end
87-
87+
8888 # Convert our internal representations back into standard OAI options
8989 def externalize ( value )
9090 value . to_s . gsub ( /_[a-z]/ ) { |m | m . sub ( "_" , '' ) . capitalize }
9191 end
92-
92+
9393 def parse_date ( value )
9494 return value if value . respond_to? ( :strftime )
95-
95+
9696 Date . parse ( value ) # This will raise an exception for badly formatted dates
9797 Time . parse ( value ) . utc # -- UTC Bug fix hack 8/08 not in core
9898 rescue
99- raise OAI ::ArgumentError . new
99+ raise OAI ::ArgumentError . new
100100 end
101-
101+
102102 def internalize ( hash = { } )
103103 internal = { }
104104 hash . keys . each do |key |
105105 internal [ key . to_s . gsub ( /([A-Z])/ , '_\1' ) . downcase . intern ] = hash [ key ] . dup
106106 end
107-
107+
108108 # Convert date formated strings into internal time values
109109 # Convert date formated strings in dates.
110110 internal [ :from ] = parse_date ( internal [ :from ] ) if internal [ :from ]
111111 internal [ :until ] = parse_date ( internal [ :until ] ) if internal [ :until ]
112-
112+
113113 internal
114114 end
115-
115+
116116 end
117-
117+
118118end
119119end
120120end
0 commit comments