I'm trying to use Restclient to communicate with an external API, but keep getting the following error when trying to test a call in the console:
rest_resource = RestClient::Resource.new(uri, USERNAME, PASSWORD)
#=> #<RestClient::Resource:0x007fd86992e228 @url="http://localhost:3000/api/users.json", @block=nil, @options={:user=>"myfinance", :password=>"credit123"}>
users = rest_resource.get
#=> NoMethodError: undefined method `parse' for #<String:0x007fd865eb75b8>
uri
, USERNAME
, and PASSWORD
are all defined constants.
I can't figure out what the issue is, as the syntax appears correct.
Did you redefine URI
by mistake?
URI
is a Ruby module that handles URL parsing. If you redefined that constant in your code, like this:
URI = "some string"
Then you will break everything that depends on the URI module, including RestClient.
For example:
require "rest_client"
USERNAME = "foo"
PASSWORD = "bar"
URI = "http://localhost:3000/api/users.json"
rest_resource = RestClient::Resource.new(URI, USERNAME, PASSWORD)
rest_resource.get
# => NoMethodError: undefined method `parse' for "http://localhost:3000/api/users.json":String
Redefining a constant is bad, and Ruby will warn you about it. For example, the above code produces this warning:
warning: already initialized constant URI