谁是解析器不工作



我是Rails的新手,想尝试Ruby Whois gem。

我定义了一个用于创建报告的活动记录。然后,该报告将填充来自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*********************************************************************nnDomain:             google.itnStatus:             oknCreated:            1999-12-10
00:00:00nLast Update:        2014-05-07 00:52:45nExpire Date:        2015-04-21n
nRegistrantn  Name:             Google Ireland Holdingsn  Organization:     Google 
Ireland Holdingsn  ContactID:        DUP430692088n  Address:          70 Sir John 
Rogersons Quayn                    Dublinn                    2n                    
IEn                    IEn  Created:          2013-04-21 01:05:35n  Last Update:      
2013-04-21 01:05:35nnAdmin Contactn  Name:             Tsao Tun  Organization:     Tu 
Tsaon  ContactID:        DUP142437129n  Address:          70 Sir John Rogersons 
Quayn                    Dublinn                    2n                    
IEn                    IEn  Created:          2013-04-21 01:05:35n  Last Update:      
2013-04-21 01:05:35nnTechnical Contactsn  Name:             Google Ireland Holdingsn  
Organization:     Google Ireland Holdingsn  ContactID:        DUP430692088n  
Address:          70 Sir John Rogersons Quayn                    
Dublinn                    2n                    IEn                    IEn  
Created:          2013-04-21 01:05:35n  Last Update:      2013-04-21 01:05:35n
nRegistrarn  Organization:     MarkMonitor International Limitedn  Name:             
MARKMONITOR-REGn  Web:              https://www.markmonitor.com/nnNameserversn  
ns1.google.comn  ns4.google.comn  ns2.google.comn  ns3.google.comnn"

$ (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*********************************************************************
nnDomain:             google.itnStatus:             oknCreated:  1999-12-10
00:00:00nLast Update:        2014-05-07 00:52:45nExpire Date:        2015-04-21n
nRegistrantn  Name:             Google Ireland Holdingsn  Organization:     Google 
Ireland Holdingsn  ContactID:        DUP430692088n  Address:          70 Sir John 
Rogersons Quayn                    Dublinn                    2n                    
IEn                    IEn  Created:          2013-04-21 01:05:35n  Last Update:      
2013-04-21 01:05:35nnAdmin Contactn  Name:             Tsao Tun  Organization:     Tu 
Tsaon  ContactID:        DUP142437129n  Address:          70 Sir John Rogersons 
Quayn                    Dublinn                    2n                    
IEn                    IEn  Created:          2013-04-21 01:05:35n  Last Update:      
2013-04-21 01:05:35nnTechnical Contactsn  Name:             Google Ireland Holdingsn  Organization:     Google Ireland Holdingsn  ContactID:        DUP430692088n  
Address:          70 Sir John Rogersons Quayn                    
Dublinn                    2n                    IEn                    IEn  
Created:          2013-04-21 01:05:35n  Last Update:      2013-04-21 01:05:35n
nRegistrarn  Organization:     MarkMonitor International Limitedn  Name:             
MARKMONITOR-REGn  Web:              https://www.markmonitor.com/nnNameserversn  
ns1.google.comn  ns4.google.comn  ns2.google.comn  ns3.google.comnn">

我想知道这一切是怎么回事。它的结构与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、registrator等等…有什么技巧吗?

#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

请务必阅读库文档。

  1. 控制台中的输出显示,进行调用将返回预期的输出(设置@record)。

  2. property_supported?无法工作,因为该方法在此类型的对象上不可用。

此外,我从未听说过夜蛾
您是否能够从应用程序的根目录执行rails console并尝试在那里使用对象?

最新更新