Question

I am trying to make a Ruby console script to make namecheap API calls. I found the namecheap gem and installed it using sudo gem install namecheap.

I wrote my test script, hiding the key, username and clientip:

#!/usr/bin/env ruby

require 'namecheap'

Namecheap.configure do |config|
  config.key = 'mykey'
  config.username = 'myusername'
  config.client_ip = 'myip'
end
Namecheap.domains.get_list

My Ruby version:

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]

I get this error when I run the script:

/var/lib/gems/1.9.1/gems/namecheap-0.1.2/lib/monkey_patch.rb:14:in `block in to_param': undefined method `to_query' for "myusername":String (NoMethodError)
    from /var/lib/gems/1.9.1/gems/namecheap-0.1.2/lib/monkey_patch.rb:13:in `each'
    from /var/lib/gems/1.9.1/gems/namecheap-0.1.2/lib/monkey_patch.rb:13:in `collect'
    from /var/lib/gems/1.9.1/gems/namecheap-0.1.2/lib/monkey_patch.rb:13:in `to_param'
    from /var/lib/gems/1.9.1/gems/namecheap-0.1.2/lib/namecheap/api.rb:15:in `api_call'
    from /var/lib/gems/1.9.1/gems/namecheap-0.1.2/lib/namecheap/domains.rb:5:in `get_list'
    from ./chkdn.rb:15:in `<main>'

Any idea what is wrong?

Was it helpful?

Solution

Spoiler: this gem works fine only in Rails projects

As far as I see, it is a bug in the namecheap gem. Gem author copied the part of core extension from rails activesupport in order to transform hashes into URL parameters. However he or she forgot that this particular function relies on functions defined for many possible classes, that could possibly be used as the values in hashes and converted to URL parameters (see all these definitions).

That is exactly why you're getting this error:

undefined method `to_query' for "myusername":String

It is really was not defined for String. However, it will work out of the box if you use Rails (because it defined there, obviously). If you're not going to use Rails, your options are:

  • to copy contents of this file somewhere in your project (you might want to skip the last class, Hash, as it was already patched in namecheap gem)
  • install activesupport gem and require either 'active_support/core_ext/object/to_param' or 'active_support/core_ext' (latter will include all core extensions)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top