Class: Duns
- Inherits:
-
Object
- Object
- Duns
- Defined in:
- lib/duns-lookup.rb
Overview
Synopsis
The Duns library provides a small wrapper around the Dun & Bradstreet website’s advanced search functionality. Currently it only implements searching for company by DUNS number.
Example
require 'rubygems'
require 'kdonovan-duns-lookup'
Duns.lookup_duns( *invalid_number* )
# => nil
Duns.lookup_duns( *some_valid_number* )
# => {:name => *a_name*, :address => *an_address*}
Constant Summary collapse
- @@dnb_homepage =
D&B homepage URL
'http://smallbusiness.dnb.com/'
- @@dnb_advanced_search =
The URI for D&B advanced search
'https://smallbusiness.dnb.com/ePlatform/servlet/AdvancedCompanySearch?storeId=10001&catalogId=70001?storeId=10001&catalogId=70001'
- @@agent =
Our Mechanize agent
WWW::Mechanize.new
Class Method Summary collapse
-
.lookup_duns(number) ⇒ Object
Look up a given DUNS number in the D&B database.
-
.update_advanced_search_url ⇒ Object
Updates the internal URL used as the base for advanced searches.
Class Method Details
.lookup_duns(number) ⇒ Object
Look up a given DUNS number in the D&B database. If the number is found, returns a hash with :name and :address keys. Otherwise, returns nil.
38 39 40 41 42 43 44 |
# File 'lib/duns-lookup.rb', line 38 def self.lookup_duns(number) form = @@agent.get( @@dnb_advanced_search ).form('DunsSearchForm') form.dunsNumber = enforce_duns_formatting(number) page = @@agent.submit(form) extract_search_results(page) end |
.update_advanced_search_url ⇒ Object
Updates the internal URL used as the base for advanced searches.
The D&B website uses lots of extraneous (to us) URL params, and I have no idea what they all mean. If the base URL stops working, this method will try to set a new one by visiting the main page and finding & clicking an Advanced Search link.
This is mostly a precaution, but if searches stop working a good first bet would be to try running this method. If search is still broken, something actually changed in the D&B HTML and we’ll need to retool the gem.
58 59 60 61 62 63 |
# File 'lib/duns-lookup.rb', line 58 def self.update_advanced_search_url page = @@agent.get( @@dnb_homepage ) advanced_link = page.links.find{|l| l.text.match(/search/i) && l.uri.to_s.match(/AdvancedCompanySearch/)} raise(DunsError, "Unable to find an advanced search link at: #{@@dnb_homepage}") if advanced_link.nil? @@dnb_advanced_search = advanced_link.uri.to_s end |