我是Rails的新手,想尝试Ruby Whois宝石。
我定义了一个创建报告的Active Record记录。然后该报告将填充来自whois查询的数据(最好解析)。
当我进行查询时,我得到了一些非常奇怪的东西。我使用ByeBug作为调试器:
$ (byebug) Whois.whois("google.it")
"\n*********************************************************************\n* Please note
that the following result could be a subgroup of *\n* the data contained in the database.
*\n* *\n* Additional
information can be visualized at:
*\n* http://www.nic.it/cgi-bin/Whois/whois.cgi
*\n*********************************************************************\n\nDomain: google.it\nStatus: ok\nCreated: 1999-12-10
00:00:00\nLast Update: 2014-05-07 00:52:45\nExpire Date: 2015-04-21\n
\nRegistrant\n Name: Google Ireland Holdings\n Organization: Google
Ireland Holdings\n ContactID: DUP430692088\n Address: 70 Sir John
Rogersons Quay\n Dublin\n 2\n
IE\n IE\n Created: 2013-04-21 01:05:35\n Last Update:
2013-04-21 01:05:35\n\nAdmin Contact\n Name: Tsao Tu\n Organization: Tu
Tsao\n ContactID: DUP142437129\n Address: 70 Sir John Rogersons
Quay\n Dublin\n 2\n
IE\n IE\n Created: 2013-04-21 01:05:35\n Last Update:
2013-04-21 01:05:35\n\nTechnical Contacts\n Name: Google Ireland Holdings\n
Organization: Google Ireland Holdings\n ContactID: DUP430692088\n
Address: 70 Sir John Rogersons Quay\n
Dublin\n 2\n IE\n IE\n
Created: 2013-04-21 01:05:35\n Last Update: 2013-04-21 01:05:35\n
\nRegistrar\n Organization: MarkMonitor International Limited\n Name:
MARKMONITOR-REG\n Web: https://www.markmonitor.com/\n\nNameservers\n
ns1.google.com\n ns4.google.com\n ns2.google.com\n ns3.google.com\n\n"
$ (byebug) Whois.whois("google.it").parser
#<Whois::Record::Parser:0x000000065bdc30
@record="\n*********************************************************************\n*
Please note that the following result could be a subgroup of *\n* the data contained
in the database.
*\n* *\n* Additional
information can be visualized at: *\n* http://www.nic.it/cgi-bin/Whois
/whois.cgi
*\n*********************************************************************
\n\nDomain: google.it\nStatus: ok\nCreated: 1999-12-10
00:00:00\nLast Update: 2014-05-07 00:52:45\nExpire Date: 2015-04-21\n
\nRegistrant\n Name: Google Ireland Holdings\n Organization: Google
Ireland Holdings\n ContactID: DUP430692088\n Address: 70 Sir John
Rogersons Quay\n Dublin\n 2\n
IE\n IE\n Created: 2013-04-21 01:05:35\n Last Update:
2013-04-21 01:05:35\n\nAdmin Contact\n Name: Tsao Tu\n Organization: Tu
Tsao\n ContactID: DUP142437129\n Address: 70 Sir John Rogersons
Quay\n Dublin\n 2\n
IE\n IE\n Created: 2013-04-21 01:05:35\n Last Update:
2013-04-21 01:05:35\n\nTechnical Contacts\n Name: Google Ireland Holdings\n Organization: Google Ireland Holdings\n ContactID: DUP430692088\n
Address: 70 Sir John Rogersons Quay\n
Dublin\n 2\n IE\n IE\n
Created: 2013-04-21 01:05:35\n Last Update: 2013-04-21 01:05:35\n
\nRegistrar\n Organization: MarkMonitor International Limited\n Name:
MARKMONITOR-REG\n Web: https://www.markmonitor.com/\n\nNameservers\n
ns1.google.com\n ns4.google.com\n ns2.google.com\n ns3.google.com\n\n">
我想知道这一切是什么。它的结构不像在IRB或简单的控制台上那样。
当我尝试查看属性时,它只是告诉我嗡嗡声:
$ (byebug) Whois.whois("google.it").property_supported?(:domain)
NoMethodError Exception: undefined method `property_supported?' for #<Whois::Record:0x000000065aba08>
nil
我在运行Rails 4.0和Ruby版本2.1.2的虚拟机(Ubuntu precise64)上。 Web服务器是默认的Webrick。
我想做的就是获取对象<Whois::Record>
并从中提取值,例如domain,domain_id,registrar等等......任何提示?
答案 0 :(得分:0)
控制台中的输出显示调用会返回预期的输出(设置@record
)。
property_supported?
无法正常工作,因为此类型的对象无法使用该方法。
我也从未听说过byebug
您是否能够从应用程序的根目录执行rails console
并尝试在那里使用对象?
答案 1 :(得分:0)
#whois
方法返回一个Whois::Record
对象,它包装了whois响应字符串并提供了解析功能。您在控制台中看到的输出只是对象表示。
record = Whois.whois("google.it")
record.class
# => Whois::Record
Record文档说明了如何使用该对象。例如,要打印出其String表示,请显式调用puts
或使用.to_s
。
puts record.to_s
如果需要访问特定属性,则无需调用解析器。只需进入酒店。
record.expires_on
# => 2015-04-21 00:00:00 +0200
请务必阅读library documentation。