warning: URI.escape is obsolete - ruby-on-rails

Hi I'm trying to get Oauth2 access from quickbooks but I'm getting this warning on command promt from rails server and nothing happens server just keeps loading the same warning;
Here's the line that gives error (located in the gem);
def escape(value)
URI::escape(value.to_s, OAuth::RESERVED_CHARACTERS)
rescue ArgumentError
URI::escape(value.to_s.force_encoding(Encoding::UTF_8), OAuth::RESERVED_CHARACTERS)
end
I looked at other complains about this error and I tried to replace URI::escape() commands with CGI::escape() but then I got this error;
Can anyone tell me how to get rid of this error and find an alternative that works?
My Ruby version: ruby 2.7.2p137
My Rails Version: Rails 6.1.4
Thank you.

If all you need is a working implementation of URI.escape, you could re-implement it yourself:
# config/initializers/uri.rb
module URI
def self.escape(*args)
DEFAULT_PARSER.escape(*args)
end
end
This will effectively remove the warning and delegate the escape call to the default parser, which also works in Ruby 3.
Note that this is just a quick patch to silence the warning. In the long run, you should find a replacement for the outdated gem(s).

Related

Carriage return character missing (Style/EndOfLine) in Ruby on Rails

I'm writing an very simple Rails application. It was very simple but I always get an warning message "Carriage return character missing" even if in two examples blow...
Can anyone tell me what is missing and what should I do to resolve this warning message?
I'm using Ruby 2.3.3, Rails 5.1.2 and Rubocop 0.49.1...
Is this warning being raised by RuboCop when you run rubocop in the terminal?
You might want to try adding this to your .rubocop.yml
Style/EndOfLine:
EnforcedStyle: lf
If that doesn't work swap try swapping crlf for lf
via: https://github.com/bbatsov/rubocop/issues/4293
My fix was to:
cut the contents
delete the file
re-create the file
paste the contents back in
Rubocop was not happy with the way rails created the file.
In my case, I generated a project with rails new sample-project --api
After adding # frozen_string_literal: true to the top of config/seeds.rb, I got this error:
db/seeds.rb:1:1: C: Layout/EndOfLine: Carriage return character missing.
# frozen_string_literal: true ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

No implicit conversion of nil into String on MessageVerifier

Recently I received new project (backend of iOS app, actually) that works on Ruby (Rails).
I have a part of code in model (user):
155: def self.create_access_token(user)
156: verifier.generate(user.id)
157: end
After some action that indirectly uses that part of code, in "Passenger" output I see following error that terminates everything:
TypeError (no implicit conversion of nil into String):
app/models/user.rb:156:in `create_access_token'
app/models/user.rb:139:in `access_token'
app/controllers/mailing_controller.rb:68:in `send_charts'
verifier is an instance of ActiveSupport::MessageVerifier
I'm totally sure that user.id contains valid value (I've tested it with $stderr.puts)
I'm completely new to this language, it's hard for me to figure out why this error appears. Hope someone can help.
Thanks!
Well, it may the case user_id is nil. Anyway please check out rubyonrails api and verify generate method.
We can actually dig inside generate method. Use pry-rails gem for easy debugging.
Most likely you did not set the access_token_secret in secrets.yml.

Rails- upgrading to ruby 2.2.2 - no implicit conversion of Array into String (TypeError)

very new to ruby and rails.. Ive been working on a project, that simply reads in files and parses them to store into a database. Project was running fine, however running the code after an update to ruby 2.2.2 , I received an error that wasn't previously there:
in `foreach': no implicit conversion of Array into String (TypeError)
Here is the snippet of the foreach thats causing an error: (let me know if more code is necessary).
def parse(file_name)
File.foreach(file_name).with_index do |line, line_num|
puts "\nLine #{line_num}: #{line}"
Does anyone know whats going on?
Thank you
EDIT: Sorry it was a snippet! Im calling this ruby code into my rails Test called "parse_log_file_test"
require File.dirname(__FILE__) + '/../test_helper'
class ParseLogFileTest < ActiveSupport::TestCase
filename = Array.new
Dir.glob('database/oag-logs/*.log').each do |log_file|
filename.push(log_file)
end
parser = ParseLogFile.new
parser.parse(filename)
test 'parse' do
parser = ParseLogFile.new
filename.each do |log_file|
begin
parser.parse(log_file)
rescue
puts"ERROR: Unable to parse line #{log_file}"
end
end
assert true
end
end
I'm guessing you omitted the end to your function, but if you don't have it, you need it.
This error indicates that the argument passed to parse as file_name is an array instead of a string.
However, if that's the case, it fails the same on e.g. Ruby 1.8.4:
File.foreach([]).with_index do |line, line_num|
puts "\nLine #{line_num}: #{line}"
end
Output:
TypeError: can't convert Array into String
from (irb):1:in `foreach'
from (irb):1:in `with_index'
from (irb):1
from :0
Thus my guess is that the code that produces the value you pass to parse returned a string in your previous Ruby version and returns an array in 2.2.2.
In your case, the error is caused by the first invocation of parser.parse(...), right above the test 'parse' do line, not by the invocation inside the test method. I guess you put that invocation there after the migration, probably to debug a problem. But you are passing a different argument to the first invocation than to the invocation inside the test method, so it fails for a different reason than the one in the test method.
To see what Error is caused inside your test, simply remove the lines
rescue
puts"ERROR: Unable to parse line #{log_file}"
(Keep the end or you'll have to remove the begin, too.)
This way, an Error will hit the test runner, which will usually display it including message and stack trace.
Agreed with the poster above that you are most likely missing quotation marks. It should have nothing to do with 2.2.2 though, probably you are copy-pastying your file name differently this time around.
So apparently this is an issue with upgrading to ruby 2.2.2 on windows. After getting past this error, I only encountered more errors.. nokogiri..etc
I have recently got a mac and the errors went away.

Rails Rspec warning: "This dynamic method is deprecated."

My Rails 4/Ruby 2 app is throwing the following warning every time my RSpec tests create a FactoryGirl object: "DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead."
This warning is not thrown when I run my app in development. Is FactoryGirl's code throwing this? I tried to find some information but it doesn't look like other people are getting this.
If you tell Rails to give you a full stack trace for the deprecation warning, you should be able to diagnose it pretty easily. The warnings are coming from a library called ActiveSupport::Deprecation - tell it to run in debug mode.
# config/environments/test.rb
ActiveSupport::Deprecation.debug = true
For me, the warnings were caused by an old version of the Stringex library.
FactoryGirl would make a new model, which would trigger a call to one of the Stringex methods, which would raise the warning, although there was no way to see that until I turned on full stack traces. bundle update stringex solved the issue with no problem.
It looks like it's coming from ActiveRecord.
module DeprecationWarning
def body
"#{deprecation_warning}\n#{super}"
end
def deprecation_warning
%{ActiveSupport::Deprecation.warn("This dynamic method is deprecated. Please use e.g. #{deprecation_alternative} instead.")}
end
end
I'm not sure why you're not getting the warnings in development. Is your environment suppressing the warnings?

Tracing dependency loading in Rails

Our team is working on a new application that we started with Rails 3.1 and Ruby 1.9.2 - and last night I took it to Ruby 1.9.3.
One of the gems we were using in the dependency chain (css_parser) ended up having a require 'iconv' in it, triggering a deprecation warning in 1.9.3 that looked like this:
.../gems/activesupport-3.1.1/lib/active_support/dependencies.rb:240:in `block in require': iconv will be deprecated in the future, use String#encode instead.
At first I naively blamed that on rails without a better trace, until I didn't find a require 'iconv' in it anywhere.
The only way I tracked this down was that I started commenting things out in my Gemfile and then I finally got the bright idea to load irb and start requiring each library in turn. I also could have just done a filesystem grep in the gems directory, but I wasn't exactly sure that "require 'iconv'" was what was triggering the error.
What a PITA. There has to be a better way - just doing a --trace in rake tasks that load rails didn't cut it. Is there some way / any way of triggering a trace on this that would have shown me which line in the relatively long list of library dependencies was triggering the deprecation?
So, it's probably a little moot because I'm not likely to ever run into the problem again (and the css_parser gem was the only iconv-requiring gems in my current Rails 3.1/Ruby 1.9.3 projects).
But it was a puzzle, so I wanted to find some way of solving it.
The problem is very iconv-specific in this case. There are other ruby deprecations, but for the most part they seem to go through Kernel#warn (if ruby) or rb_warn() (if C) - but the warning in iconv.c is a little different than the others - at any rate it's a puts to rb_stderr.
So maybe I can do the following
Override Kernel#require to capture stderr
Check for an iconv message after calling the original Kernel#require
Raise an exception if the message found, thereby getting a trace
Do this before bundler runs if at all possible.
It turns out I can't do #4 - because Bundler calls Kernel.require directly - but I can use Bundler to parse the Gemfile to give me a list of things to require myself.
So this is what I get - thanks to this stack overflow post for a pointer on capturing standard error - and the rubygems source for the idea on aliasing the original Kernel#require
# override Kernel#require to intercept stderr messages on require
# and raise a custom error if we find one mentioning 'iconv'
require "stringio"
class RequireError < StandardError
end
module Kernel
alias :the_original_require require
private :the_original_require
def capture_stderr
# The output stream must be an IO-like object. In this case we capture it in
# an in-memory IO object so we can return the string value. You can assign any
# IO object here.
previous_stderr, $stderr = $stderr, StringIO.new
yield
$stderr.string
ensure
# Restore the previous value of stderr (typically equal to STDERR).
$stderr = previous_stderr
end
def require(name)
captured_output = capture_stderr do
the_original_require(name)
end
if(captured_output =~ %r{iconv})
raise RequireError, 'iconv requirement found'
end
end
end
require 'bundler'
# load the list of Bundler requirements from the Gemfile
required_libraries = Bundler.definition.dependencies.map(&:name)
# loop through and require each, ignoring all errors other than
# our custom error
required_libraries.each do |requirement|
begin
require(requirement)
rescue Exception => e
if(e.class == RequireError)
raise e
end
end
end
And voila! A trace message that helps track down where the iconv requirement was.
In the end, probably just a search for "require 'iconv'" is still best (once it's clear that's the what was causing the error).
But, as in life. Some Yaks Must Be Shaved.
You could take a look at the Gemfile.lock file, which holds all dependencies in a hierarchical tree and indicates the versions required by each gem. This might help to identify the gem that is requiring it.

Resources