2014-09-30 85 views
1

我是Rails的新手,想试试Ruby Whois gem。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 

我在一个虚拟机(Ubuntu的precise64)运行的Rails 4.0和Ruby 2.1.2版。 Web服务器是默认的Webrick。

我想要做的就是获取对象<Whois::Record>,并从中提取值,如域名,域名ID,注册商等等......任何提示?

+0

试图使代码可读,但只有到目前为止 – 2014-09-30 12:22:46

+0

请花时间使用正确的语法和格式。它使您的问题更容易阅读,这有助于我们帮助您解决问题。 *不*这样做只会阻碍潜在的回答者,他们认为你不够重视写一个可读的问题,所以他们为什么要照顾足够的帮助。 – 2014-09-30 16:14:19

回答

0

#whois方法返回一个Whois::Record对象,它包装whois响应字符串并提供解析功能。您在控制台中看到的输出只是对象表示。

record = Whois.whois("google.it") 
record.class 
# => Whois::Record 

Record文档解释了如何使用对象。例如,要打印出其字符串表示形式,请明确地调用puts或使用.to_s

puts record.to_s 

如果您需要访问特定属性,则不需要调用解析器。只需访问该属性。

record.expires_on 
# => 2015-04-21 00:00:00 +0200 

请务必阅读library documentation

+0

不能相信我因为Ruby非Rails基本的东西而陷入困境。非常感谢 !是的,我一遍又一遍地检查文件,但我想有一些微妙之处,我会错过任何... ... - – Exos 2014-09-30 14:16:23

0
  1. 控制台中的输出显示使呼叫返回预期输出(设置为@record)。

  2. property_supported?没有工作,因为该方法不适用于此类型的对象。

另外我从来没有听说过byebug。
您是否可以从应用程序的根目录执行rails console并尝试在其中使用对象?

+0

从我所了解的情况来看,rails调试器没有被维护为rails版本> 3.0,所以我不得不寻找别的东西。 Byebug其实很容易使用,以及我不会真的知道,因为我还没有尝试任何其他调试器。 – Exos 2014-09-30 14:14:10

+0

[Byebug](https://github.com/deivid-rodriguez/byebug)是一款非常不错的Ruby调试器v2.0 + – 2014-09-30 16:20:00