weppos / whois

An intelligent — pure Ruby — WHOIS client and parser.
https://whoisrb.org
MIT License
1.11k stars 282 forks source link

How to detect if a record has changed? #659

Closed AumCoin closed 7 months ago

AumCoin commented 8 months ago

The doc at https://whoisrb.org/docs/v3/documentation/ says Parser.changed? can be passed a record, but on my system it only takes a parser:

2.7.2 :073 > record = Whois.whois("google.it").parser
 => #<Whois::Parser:0x000055c96b029628 @record="\n*********************************************************************\n* Please not...
2.7.2 :074 > record2 = Whois.whois("google.it").parser
 => #<Whois::Parser:0x000055c96b090210 @record="\n*********************************************************************\n* Please not...
2.7.2 :075 > record.changed?(record2)
 => false
2.7.2 :076 > record.changed?(record2)
 => false
2.7.2 :077 > record.changed?(Whois.whois("google.it"))
Traceback (most recent call last):
        6: from /usr/local/rvm/rubies/ruby-2.7.2/bin/irb:23:in `<main>'
        5: from /usr/local/rvm/rubies/ruby-2.7.2/bin/irb:23:in `load'
        4: from /usr/local/rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
        3: from (irb):77
        2: from /usr/local/rvm/gems/ruby-2.7.2/gems/whois-parser-2.0.0/lib/whois/parser.rb:261:in `changed?'
        1: from /usr/local/rvm/gems/ruby-2.7.2/gems/whois-parser-2.0.0/lib/whois/parser.rb:274:in `unchanged?'
ArgumentError (Can't compare `Whois::Parser' with `Whois::Record')
2.7.2 :078 >

I assume records don't change that often that I would need to check it frequently, so presumably the idea was to save a record and check it later to see if it had changed unexpectedly, but I can't figure out how to get a Whois::Record object from a string.

I need this because I was hoping to monitor the status of a large number of domains I own for unauthorized changes, but most of the ones I tried failed to parse out any contacts (which is weird because I can plainly see the info, in a format I've seen a thousand times, on https://www.whois.com/whois/nadhiya.com; seriously, how sophisticated does something need to be to parse the data when the data is meticulously labeled in a standard format), to where I decided the feature was useless for my needs.

I could just compare one text record to another but I was hoping this library could do something more intelligent, based on the claim that changed? attempts to guess if the current WHOIS response changed compared to an other WHOIS response passed as parameter". What am I missing here?

weppos commented 7 months ago

You can see the record object how it is composed: in its essence, it's an array of string parts.

https://github.com/weppos/whois/blob/d172f896b6be86f1d8cf2f17554ad2ad4a7a8328/lib/whois/record.rb#L27-L35

Once you have a whois record, you can extract the parts and store them where you want.

record = Whois.whois("google.it")
parts = record.parts # => Array<String>

Whois::Record.new(nil, parts)

Also note that your code is trying to compare a parser with a record, which are two completely different objects. The following line in your code returns a parser, not a record.

record = Whois.whois("google.it").parser