11# External dependencies
22require 'uri'
3- require 'net/http '
3+ require 'faraday '
44require 'cgi'
55require 'iconv'
66
@@ -67,6 +67,11 @@ class Client
6767 # back XML::Node objects
6868 #
6969 # client = OAI::Client.new 'http://example.com', :parser => 'libxml'
70+ #
71+ # You can configure the Faraday HTTP client by providing an alternate
72+ # Faraday instance:
73+ #
74+ # client = OAI::Client.new 'http://example.com', :http => Faraday.new { |c| }
7075 #
7176 # === HIGH PERFORMANCE
7277 #
@@ -77,7 +82,18 @@ def initialize(base_url, options={})
7782 @base = URI . parse base_url
7883 @debug = options . fetch ( :debug , false )
7984 @parser = options . fetch ( :parser , 'rexml' )
85+
8086 @follow_redirects = options . fetch ( :redirects , true )
87+ @http_client = options . fetch ( :http , Faraday . new ( @base ) )
88+
89+ if !options . key? ( :http ) and @follow_redirects
90+
91+ count = @folow_redirects if @folow_redirects . is_a? Fixnum
92+ count ||= 5
93+
94+ require 'faraday_middleware'
95+ @http_client . use FaradayMiddleware ::FollowRedirects , :limit => count
96+ end
8197
8298 # load appropriate parser
8399 case @parser
@@ -207,21 +223,8 @@ def load_document(xml)
207223
208224 # Do the actual HTTP get, following any temporary redirects
209225 def get ( uri )
210- response = Net ::HTTP . get_response ( uri )
211- case response
212- when Net ::HTTPSuccess
213- return response . body
214- when Net ::HTTPMovedPermanently
215- if @follow_redirects
216- response = get ( URI . parse ( response [ 'location' ] ) )
217- else
218- raise ArgumentError , "Permanently Redirected to [#{ response [ 'location' ] } ]"
219- end
220- when Net ::HTTPTemporaryRedirect
221- response = get ( URI . parse ( response [ 'location' ] ) )
222- else
223- raise ArgumentError , "#{ response . code_type } [#{ response . code } ]"
224- end
226+ response = @http_client . get uri
227+ response . body
225228 end
226229
227230 def debug ( msg )
0 commit comments