Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `encode' for URI:Module with gem Rspotify

I am using gem rspotify to access spotify's api in my rails app. The rails version is 6.0.4 but most importantly the Ruby version is 3.0.0

I keep getting this error "undefined method `encode' for URI:Module" when I try to call the api methods. I have to stress that I do not get this error when I downgrade to Ruby 2.6.3. It seems like Ruby 3.0.0 has not support for URI encode. My users controller is the code sample below. I get the error with spotify_user.country and other api methods.

class UsersController < ApplicationController
  skip_before_action :authenticate_user!, only: [ :spotify]

  def spotify
    spotify_user = RSpotify::User.new(request.env['omniauth.auth'])
    spotify_user.country
  end
end

In config/application.rb

RSpotify::authenticate(ENV['SPOTIFY_CLIENT_ID'], ENV['SPOTIFY_CLIENT_SECRET'])

In devise.rb, I have

require 'rspotify/oauth'

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :spotify, ENV['SPOTIFY_CLIENT_ID'], ENV['SPOTIFY_CLIENT_SECRET'], scope: 
  'user-read-email playlist-modify-public user-library-read user-library-modify'
end

I will like to know if there is a solution for the error that does not involve downgrading the ruby version

like image 993
Hakeem Baba Avatar asked Apr 15 '26 15:04

Hakeem Baba


2 Answers

I upgraded from Ruby 2.4.5 to Ruby 3.0.2 and the solution for me was to replace

URI.encode

with

CGI.escape
like image 56
Dave Avatar answered Apr 17 '26 05:04

Dave


In pure Ruby, for Ruby 3.X:

URL-encode for whole URL:

URI::Parser.new.escape

URL-encode for URL component:

CGI.escape

For examples, see yard doc here and tests here.

like image 37
noraj Avatar answered Apr 17 '26 04:04

noraj