Reputation: 1714
At the moment I'm rewriting a whole application from Rails 3 to Rails 6.
In that process I'm cleaning a lot of things as well.
But here is my problem.
I have a module called Reusable
which as it name implies contains a ton of reusable methods across the models.
We use Faraday
to establish connection to the certain server:
Old code looked like this:
module Reusable
def self.establish_connection(url)
url = I18n.transliterate(url)
url = URI.encode(url)
conn = Faraday.new(url: url) do |builder|
builder.use :cookie_jar
builder.use FaradayMiddleware::FollowRedirects
builder.adapter Faraday.default_adapter
end
conn.get
conn
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Errno::ETIMEDOUT => e
message = "(Timeout::Error) Connection to address: #{url} failed. If address is reachable please run the method again."
end
end
However, by moving to Ruby 2.7.0, we've got the dreaded URI.encode
is obsolete warning.
In order to get rid of it, I rewrote a whole part like so:
module Reusable
def self.establish_connection(url)
url = I18n.transliterate(url)
url = URI.encode_www_form_component(url)
conn = Faraday.new(url: url) do |builder|
builder.request :url_encoded
builder.use :cookie_jar
builder.use FaradayMiddleware::FollowRedirects
builder.adapter Faraday.default_adapter
end
conn.get
conn
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Errno::ETIMEDOUT => e
message = "(Timeout::Error) Connection to address: #{url} failed. If address is reachable please run the method again."
end
end
Hoewever, I'm getting this:
I've searched the SO for the solution to this problem, and I found this -> URI::InvalidURIError (bad URI(is not URI?): ):
But this again returns me to using the URI.encode
which I want to avoid.
Any solution to this, or should I just quietly ignore the obsolete warning for now until something better comes out?
Upvotes: 0
Views: 414
Reputation: 13014
Try this:
module Reusable
def self.establish_connection(url)
url = I18n.transliterate(url)
params = Rack::Utils.parse_nested_query(URI(url).query)
conn = Faraday.new(url: url, params: params) do |builder|
builder.request :url_encoded
builder.use :cookie_jar
builder.use FaradayMiddleware::FollowRedirects
builder.adapter Faraday.default_adapter
end
conn.get
conn
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Errno::ETIMEDOUT => e
message = "(Timeout::Error) Connection to address: #{url} failed. If address is reachable please run the method again."
end
end
Upvotes: 1