Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Is there a way to find out the CNAME value of a certain domain?

Here is the context:

I am providing a service via the following URL: http://api.example.com/ ...... rest of the url ......

Assume there is a service consumer who wants to use this service but they want to have a custom domain http://api.consumer.com/ ...... rest of the url ......

The consumer will be asked to set the CNAME value of http://api.consumer.com/ to http://api.example.com/ so my service can handle all the requests originally sent to http://api.consumer.com/.

I am wondering how to find out whether the consumer has properly set the CNAME value, preferably through a Ruby package. Thanks.

1 Answer

You can always use the UNIX dig command and parse the results. For example you would use dig example.com any and look for instances of "CNAME". But that's clunky and forces system dependencies in your code.

I found a library that will get you what you want, but it's not the most user-friendly API. Check out dnsruby (rdoc). Here's a rough example of what i think you're looking for:

require 'dnsruby'

resolver = Dnsruby::Resolver.new
response = resolver.query('www.youtube.com', Dnsruby::Types.CNAME)
response.answer.any? { |answer| answer.name.to_s == 'www.youtube.com' }    # => true
response.answer.any? { |answer| answer.name.to_s == 'www.yahoo.com' }        # => false

Hope that gets you on the right track.