2014-09-30 72 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