Skip to content

Allow Faraday 2.x #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2022
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
18 changes: 16 additions & 2 deletions lib/oai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def initialize(base_url, options={})
follow_redirects = 5 if follow_redirects == true

if follow_redirects
require 'faraday_middleware'
require 'faraday/follow_redirects'
builder.use Faraday::FollowRedirects::Middleware
builder.response :follow_redirects, :limit => follow_redirects.to_i
end
builder.adapter :net_http
Expand Down Expand Up @@ -330,14 +331,27 @@ def parse_date(value)
# Regex is from WebCollab:
# http://webcollab.sourceforge.net/unicode.html
def strip_invalid_utf_8_chars(xml)
xml && xml.gsub(/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]
return nil unless xml

# If it's in a specific encoding other than BINARY, it may trigger
# an exception to try to gsub these illegal bytes. Temporarily
# put it in BINARY. NOTE: We're not totally sure what's going on
# with encodings in this gem in general, it might not be totally reasonable.
orig_encoding = xml.encoding
xml.force_encoding("BINARY")

xml = xml.gsub(/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]
| [\x00-\x7F][\x80-\xBF]+
| ([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*
| [\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})
| [\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))
| (?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/x, '?')\
.gsub(/\xE0[\x80-\x9F][\x80-\xBF]
| \xED[\xA0-\xBF][\x80-\xBF]/,'?')

xml.force_encoding(orig_encoding)

xml
end

end
Expand Down
6 changes: 4 additions & 2 deletions ruby-oai.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ Gem::Specification.new do |s|
s.executables = 'oai'

s.add_dependency('builder', '>=3.1.0')
s.add_dependency('faraday')
s.add_dependency('faraday_middleware')
s.add_dependency('faraday', "< 3")
#s.add_dependency('faraday_middleware')
s.add_dependency("faraday-follow_redirects", ">= 0.3.0", "< 2")


s.add_development_dependency "activerecord", ">= 5.2.0", "< 6.1"
s.add_development_dependency "appraisal"
Expand Down
4 changes: 2 additions & 2 deletions test/client/tc_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def test_http_client_handles_trailing_slash_redirects
end

faraday_stub = Faraday.new do |builder|
require 'faraday_middleware'
builder.use FaradayMiddleware::FollowRedirects
require 'faraday/follow_redirects'
builder.use Faraday::FollowRedirects::Middleware
builder.adapter :test, stubs
end

Expand Down