Contact info empty/not parsed for domains from some registrars - ruby

I'm starting to use whoisrb and I'm noticing domains from some registrars return nil contact information.
For example:
domain_name = ARGV[0]
r = Whois.whois(domain_name)
t=r.registrant_contact
if t == nil
puts 'Registrant Contact is empty.'
end
Will return "Registrant Contact is empty." Trying to access the contact attributes results in an error, like undefined method 'id' for nil:NilClass (NoMethodError).
If I check the raw record that's being returned, puts r, I can see it's getting the thick record, so the contact information is there in the unparsed raw record.
The two registrars I've noticed this for, so far, are onlinenic.com and namesilo.com. If you try to run whois for those two domains, you'll see what I mean.
I'm checking the ICANN compliant sample here:
https://www.icann.org/resources/pages/approved-with-specs-2013-09-17-en#whois
against onlinenic.com and namesilo.com, and I don't see any substantial differences (maybe I'm missing something, though).
Any ideas why it's having trouble parsing these, or pointers on what I could check to fix it? Thanks.

It happens when the registrar has no parser associated, or the parser doesn't have the definition required to parse the contacts.
In other words, unless a parser exists, it's possible that the registrar details are in the response but the library can't find them.
In that case, the solution is to either add/update the parser corresponding to the specific registrar/registry.
Since this behavior is confusing to whoever is not familiar with the internals of the library, also note that the new release 4 will raise an error in this case (instead of silently returning nil). In this way it will be clear when the value is nil vs the value is unknown.

r = Whois.whois(domain_name)
The r here is a Whois::Record object and you can find the available methods here. registrant_contact is not one of them. You probably have to parse it out yourself.

Related

how to parse previously fetch whois data with Ruby Whois?

According to README on github, Ruby Whois can be used "as a standalone library to parse WHOIS records fetched previously and/or from different WHOIS clients."
I know how to use the library to directly perform whois query and parse the returning result. But I cannot find anywhere(stackoverflow included) how I can use this library to parse whois data previously fetched ?
I think it's not important but this is how I get my data, anyway: they are fetched through linux whois command and stored in separate files, each file containing one whois query result.
The manual pages on https://whoisrb.org/ are 404. Even the code on the homepage is outdated thus wrong, and the doc pages provide little information.
I tried to scan the source code on github( https://github.com/weppos/whois-parser and https://github.com/weppos/whois). I tried to find the answer on rubydoc ( https://www.rubydoc.info/gems/whois-parser/Whois/Parser, https://www.rubydoc.info/gems/whois/Whois/Record and some related pages). Both failed, partly because this task is the first time and the reason that I use Ruby.
So could anyone help me? I'm really desperate and I'll definitely appreciate any help.
Try it like this,
require 'whois-parser'
domain = 'google.com'
data = 'WHOIS DATA THAT YOU ALREADY HAVE'
whois_server = Whois::Server.guess domain
whois_data = [Whois::Record::Part.new(body: data, host: whois_server.host)]
record = Whois::Record.new(whois_server, whois_data)
parser = record.parser
parser.available? #=> false
parser.registered? #=> true

How do I call the evernote struct object to report all notes inside a notebook?

I am working through the ruby evernote-thrift API and sandbox.
I am experiencing some issues interpreting the docs; I am trying to retrieve the subject line from all the notes inside a particular notebook.
To get the name of the notebook I call
notebooks = noteStore.listNotebooks(authToken) and then run .each on notebooks. According to the docs there is a struct object called noteList but I can’t figure out how to use it.
this is the link to the docs area I am trying to leverage http://www.rubydoc.info/gems/evernote-thrift/Evernote/EDAM/NoteStore/NoteList#struct_fields-instance_method
my attempt, is as follows, but its not returning anything. unfortunately im not familiar with structs at all.
notebooks = noteStore.listNotebooks(authToken)
notebooks.each do |notebook|
next if notebook.name != 'First Notebook'
notes = notebook.noteList
noteList.each do |note|
puts note
end
end
i am getting a no method error... which makese sense because its a struct I just dont know how to leverage it...
undefined method `noteList' for <Evernote::EDAM::Type::Notebook:0x007fb2041683f8> (NoMethodError)
The generated docs for our Ruby SDK are confusing (sorry!), but I find the general docs to be much clearer: https://dev.evernote.com/doc/reference/.
As you can see in https://dev.evernote.com/doc/reference/Types.html#Struct_Notebook, the Notebook object does not have an attribute called noteList. There is a struct called NoteList, but that was what the removed NoteStore.findNotes returned.
The procedure for getting the titles/subjects of the notes in a notebook is to get the Notebook (which you have done), then pass the notebook's guid into NoteStore.findNotesMetadata (https://dev.evernote.com/doc/reference/NoteStore.html#Fn_NoteStore_findNotesMetadata). This returns a NotesMetadataList which has a notes attribute which is a list of NoteMetadata. This struct has metadata like title and GUID but not the body. If you want the full information, you would pass the GUID into NoteStore.getNote (https://dev.evernote.com/doc/reference/NoteStore.html#Fn_NoteStore_getNote).
That API is one of the least Ruby things I've ever seen. You have my condolences for trying to trudge through that :)
From the API docs, all I'm seeing that hangs off of that Evernote::EDAM::Type::Notebook class are #struct_fields and #validate, as far as instance methods go. Perhaps that struct_fields has what you're looking for?
If that doesn't lead you anywhere, I'd suggest doing using something like Pry to help you troubleshoot the error. I'd put a binding.pry statement on the second line and then explore the notebooks objects from there.

Ruby on Rails Exceptions

I am a junior rails developer and was advised to use Class.find(id) to query the database instead of Class.find_by_id(id) which I previously had. The reason I was told is because the former would raise an exception while the latter would return nil. I realize this happens but I am wondering what the high level conceptual logic is for doing it this way. Why do I want the exception? Is this a rails standard where I would always prefer a method that returns an exception as opposed to nil?
You typically want the exception because you're typically doing Foo.find(id) based on data input coming from the user, such as clicking on a link.
For example, you show the user a list of items. There are links like this:
http://example.com/items/100
http://example.com/items/101
http://example.com/items/102
The user clicks the first link, and expects to see item 100.
Your code does this:
Item.find(100)
You expect to find the item, because app created the item link. You'd be surprised if the item didn't exist.
(Corner case surprises are possible: perhaps the item was deleted, or perhaps a hacker is sending in missing ids, etc. Using exceptions helps you handle this as an exceptional circumstance.)
Exceptions are preferred to nil for this, because you want the code to fail immediately so you don't accidentally send the nil on to some other method.
Ruby nil objects can be confusing because they evaluate to falsey and also because nil.id == 4 because of how Ruby uses C. Error messages show up like "Warning: Object#id will be deprecated" or "undefined method for 4:Fixnum".
Nils are problematic as a return type in Ruby in general. There's a great (paid) screencast by Gary Bernhardt that explains why you want to avoid returning nil from methods, but in a nutshell: when a method returns nil, and that nil gets passed up through a chain of method calls and something goes wrong somewhere, it can be extremely difficult to figure out where the actual problem occurred.
Say, for example, you have something like this:
foo_model = MyModel.find_by_name('foo')
# some more lines of code
do_something(foo_model)
and a method:
def do_something(model)
# some stuff stuff
some_other_method(model)
end
Now, if MyModel.find_by_name('foo') returns nil, that nil will be carried along without any errors until it actually has to do something. Say, in some_other_method, you actually try to call something on model, say model.save, you will get an error:
undefined method 'save' for nil:NilClass (NoMethodError)
The trace will carry you back up the method calls, but it will not mention the line that was actually problematic, where you assign MyModel.find_by_name('foo') (which evaluates to nil) to foo_model.
You can imagine that in a real application, the code can be much more complex, and returning nil can make it much more difficult to figure out the source of an error.
An exception, in contrast, tells you immediately where the problem is, and the trace will go back to the line where it occurred. That's one reason (there are others, I imagine) why in general, returning nil is not a good idea.
Hope that helps.

Rails: ParameterFilter::compiled_filter tries to dup symbol

I'm running rails3 with rails exception-notifier gem. When an exception occurs, and an email should be sent, I'm getting an exception from the ParameterFilter class. I've found the problem in the rails source, and am not sure the best way to proceed.
The problem occurs in ActionDispatch::Http::ParameterFilter. In the compiled_filter method, an error occurs on line 38: key = key.dup when key is a symbol, because symbols are not duplicable. Here is the source:
def compiled_filter
...
elsif blocks.present?
key = key.dup
value = value.dup if value.duplicable?
blocks.each { |b| b.call(key, value) }
end
I see that they only call dup on value when it is duplicable. If I patch the source to only call dup on key when key is duplicable, then my problem goes away. I'm assuming there is a reason why the author put that condition on value and not key, so I'm curious if someone out there has a better understanding of this code.
This error only occurs when you add a block to your filter params in application.rb. So, maybe there is a workaround for my original issue that does not require using a block here. If you're interested see my coworker's question Rails: Filter sensitive data in JSON parameter from logs
The key for which this is a problem is :action. This comes from rails and I don't know if there is any way to force it to be a string instead.
I filed a rails bug https://rails.lighthouseapp.com/projects/8994/tickets/6557-symbol-duplication-error-in-parameterfilter-compiled_filter and I have a patch ready that adds if key.duplicable? to the key.dup line, I'm looking for input on whether or not that is the right solution.
This looks like a bug in Rails. Either the key should be a string rather than a symbol, or the dup should be protected by duplicable?.
You should file a bug at https://rails.lighthouseapp.com/, including a minimal test case if possible.

SOAP::RPC::Driver formatting problems. How can I change it?

I'm dealing with a SOAP webservice call from a server that is expecting to receive method calls with the paramaters in the format of:
<urn:offeringId> 354 </urn:offeringId>
But SOAP::RPC::Driver is generating messages in the form of:
<offeringId xsi:type = "xsd:int">354</offeringId>
The server keeps erroring when it gets these messages (especially since it's expecting offeringId to be a custom type internal to itself, not an int).
Is there anyway to configure the driver to format things the way the server is expecting it. Is the server even doing SOAP? I'm having trouble finding reference to that style of formating for SOAP (I know it DOES work though, because SOAPUI works just fine with that type of message).
-Jenny
Edit: I've got at least part of it solved. the RPC::Driver (obviously) uses the RPC standard, whereas apparently the server I'm trying to talk to is doing "document". Now, when I look at RPC::Driver's API, I'm seeing a method named "add_document_method". That SOUNDS to me like it might be what I want, but I can't figure out what paramaters to give it. The examples I've seen around the net don't make much sense to me, things like:
def GetNamePair(response)
response.account.each do |x|
class << x
attr :configuration, true
end
x.configuration = Hash[*x.a.map do |y|
[y.__xmlattr[XSD::QName.new(nil, 'n')], String.new(y)]
end.flatten]
end
end
mNS = 'urn:zimbraAdmin'
drv.add_document_method('GetAllAdminAccountsRequest', mNS, [XSD::QName.new(mNS, 'GetAllAdminAccountsRequest')],
[XSD::QName.new(mNS, 'GetAllAdminAccountsResponse')] )
puts YAML.dump(GetNamePair(drv.GetAllAdminAccountsRequest([]))
All I really know is that I have a method that takes in certain parameters.... I really don't get why, if this method does what I think it does, it has to be more complicated. Isn't this just a matter of taking the exact same data and formating it differently? I'm so confused....
Okay, what I ended up doing was using SOAP:RPC:Drivers add_document_method, which requires me to give it the wsdl, namespace, etc, and then give it the attributes later as a single input hash thingy (and gives me the output in a similar format). It worked, it just wasn't as clean as add_rpc_method (which is waht add_method defaults to)
-Jenny

Resources