user_78361084
user_78361084

Reputation: 3888

How do I json encode all the info available in the parser object?

I am very new to ruby and rails. I am trying to output all the parsed whois info to json output. I have the following:

class WhoisController < ApplicationController
  def index
    c = Whois::Client.new
    record = c.lookup("google.com")
    parser = record.parser
    created = parser.created_on
    msg = {:created => created}
    render :json => msg
  end
end

Output:

{"created":"1997-09-15T00:00:00.000-07:00"}

However, the parser has a LOT more info available....without knowing all the fields available, how do I dump all the keys/values to json?

I've tried:

class WhoisController < ApplicationController
  def index
    c = Whois::Client.new
    record = c.lookup("google.com")
    parser = record.parser
    msg = {:whois => parser}
    render :json => msg
  end
end

But end up getting:

SystemStackError in WhoisController#index

EDIT:

I've also tried:

parser.attributes.each do |attr_name, attr_value|
  puts attr_name
end

But end up getting another error:

undefined method `attributes' for #<Whois::Parser:0x00007fc030d74018>

Both Python and Go (through reflection) can do this. What is the Ruby way to achieve this?

EDIT:

class WhoisController < ApplicationController
    def index
        c = Whois::Client.new
        record = c.lookup("google.com")
        parser = record.parser
        msg = {}
        for x_prop in Whois::Parser::PROPERTIES
            msg[x_prop] = parser.send(x_prop)
        end
        render :json => msg
    end
end

This works ONLY if all the properties exist on parser. However, some domain names don't have all the properties and will result in:

Unable to find a parser for property `registrant_contacts'

I then try to set it only if that property exists:

msg = {}
for x_prop in Whois::Parser::PROPERTIES
  parser.has_attribute?(:x_prop) 
    msg[x_prop] = parser.send(x_prop) 
end
render :json => msg

I get another error:

undefined method `has_attribute?'

EDIT #3:

I've also tried:

msg = {}
for prop in Whois::Parser::PROPERTIES
  msg[prop] = parser.send(prop) if parser.respond_to?(prop)
end
render :json => msg

This still fails if the property is missing in parser. ;(

Upvotes: 2

Views: 355

Answers (3)

thiaguerd
thiaguerd

Reputation: 847

class WhoisController < ApplicationController
    def index
        c = Whois::Client.new
        record = c.lookup("google.com")
        parser = record.parser
        msg = {}
        for x_prop in Whois::Parser::PROPERTIES
            msg[x_prop] = parser.send(x_prop)
        end
        render :json => msg
    end
end

in few cases something properties can be empty and cause an error, to escape this:

begin
    msg[x_prop] = parser.send(x_prop)
rescue
    # do nothing
end

Upvotes: 2

mogbee
mogbee

Reputation: 208

You can use methods or inspect.

    c = Whois::Client.new
    record = c.lookup("google.com")
    parser = record.parser
    render json: parser.methods.to_json
    render json: parser.inspect.to_json

Upvotes: -2

Aaditya Maheshwari
Aaditya Maheshwari

Reputation: 724

For SystemStackError in WhoisController#index:

I think it is because you are calling whois with Whois again at record = Whois.whois("google.com"). Try record = whois("google.com").

For undefined method `attributes' for #<Whois::Parser:0x00007fc030d74018>: attributes method does not exit for the whois parser. See https://whoisrb.org/docs/v3/parser-properties/ .

Upvotes: 0

Related Questions