Skip to content

URI.encode is obsoleted and not available in the latest Ruby versions #57

@tannakartikey

Description

@tannakartikey

The obsolete method URI.encode is used in the code which is throwing the error while using the gem with the latest Ruby versions e.g. ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x86_64-linux].

endpoint + URI.encode(flatten_hash(data).join("&"))

The method was marked as obsolete many years back in 2009 with ruby/ruby@238b979

It can be replaced with the CGI.escape method.

More details: https://bugs.ruby-lang.org/issues/17309

There are many more discussions around the topic on https://bugs.ruby-lang.org/

I have monkeypatched the method in my Rails app until the patch is available on this gem.

# config/initalizers/woo_commerce.rb

module WooCommerce
  class API
    # URI.encode (alias of URI.escape) method used in the gem's implementation
    # is obsolete since 10 years and removed in Ruby 3.
    # https://github.com/ruby/ruby/commit/238b979f1789f95262a267d8df6239806f2859cc
    def add_query_params endpoint, data
      return endpoint if data.nil? || data.empty?

      endpoint += "?" unless endpoint.include? "?"
      endpoint += "&" unless endpoint.end_with? "?"
      endpoint + CGI.escape(flatten_hash(data).join("&"))
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions