kdonovan-duns-lookup 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/lib/duns-lookup.rb +99 -0
- data/spec/duns-lookup_spec.rb +31 -0
- data/spec/spec_helper.rb +9 -0
- metadata +83 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kali Donovan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= duns-lookup
|
2
|
+
|
3
|
+
This provides a small wrapper around the Dun & Bradstreet website to retrieve business information from DUNS numbers.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Kali Donovan. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "duns-lookup"
|
8
|
+
gem.summary = "DSL for looking up Dun & Bradstreet numbers (DUNS)"
|
9
|
+
gem.description = %Q{Provides a small wrapper around the Dun & Bradstreet website to retrieve business information from DUNS numbers.}
|
10
|
+
gem.email = "kali.donovan@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/kdonovan/duns-lookup"
|
12
|
+
gem.authors = ["Kali Donovan"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
gem.add_development_dependency "mechanize"
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
task :spec => :check_dependencies
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
require 'rake/rdoctask'
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
if File.exist?('VERSION')
|
39
|
+
version = File.read('VERSION')
|
40
|
+
else
|
41
|
+
version = ""
|
42
|
+
end
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "duns-lookup #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/duns-lookup.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
class DunsError < Exception
|
2
|
+
end
|
3
|
+
|
4
|
+
class Duns
|
5
|
+
# = Synopsis
|
6
|
+
# The Duns library provides a small wrapper around the Dun & Bradstreet
|
7
|
+
# website's advanced search functionality. Currently it only implements
|
8
|
+
# searching for company by DUNS number.
|
9
|
+
#
|
10
|
+
# == Example
|
11
|
+
# require 'rubygems'
|
12
|
+
# require 'kdonovan-duns-lookup'
|
13
|
+
#
|
14
|
+
# Duns.lookup_duns('123456789')
|
15
|
+
# # => nil
|
16
|
+
# Duns.lookup_duns( *some_valid_number* )
|
17
|
+
# # => {:name => *a_name*, :address => *an_address*}
|
18
|
+
|
19
|
+
require 'rubygems'
|
20
|
+
require 'open-uri'
|
21
|
+
require 'mechanize'
|
22
|
+
|
23
|
+
# D&B homepage URL
|
24
|
+
@@dnb_homepage = 'http://smallbusiness.dnb.com/'
|
25
|
+
|
26
|
+
# The URI for D&B advanced search
|
27
|
+
@@dnb_advanced_search = 'https://smallbusiness.dnb.com/ePlatform/servlet/AdvancedCompanySearch?storeId=10001&catalogId=70001?storeId=10001&catalogId=70001'
|
28
|
+
|
29
|
+
# Our Mechanize agent
|
30
|
+
@@agent = WWW::Mechanize.new
|
31
|
+
@@agent.user_agent_alias = 'Windows IE 7'
|
32
|
+
|
33
|
+
####
|
34
|
+
# Look up a given DUNS number in the D&B database.
|
35
|
+
# If the number is found, returns a hash with :name and :address keys.
|
36
|
+
# Otherwise, returns nil.
|
37
|
+
#
|
38
|
+
def self.lookup_duns(number)
|
39
|
+
form = @@agent.get( @@dnb_advanced_search ).form('DunsSearchForm')
|
40
|
+
form.dunsNumber = enforce_duns_formatting(number)
|
41
|
+
page = @@agent.submit(form)
|
42
|
+
|
43
|
+
extract_search_results(page)
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
####
|
48
|
+
# Updates the internal URL used as the base for advanced searches.
|
49
|
+
#
|
50
|
+
# The D&B website uses lots of extraneous (to us) URL params, and I have no idea what they all mean.
|
51
|
+
# If the base URL stops working, this method will try to set a new one by visiting the main page and
|
52
|
+
# finding & clicking an Advanced Search link.
|
53
|
+
#
|
54
|
+
# This is mostly a precaution, but if searches stop working a good first bet would be to try running this
|
55
|
+
# method. If search is still broken, something actually changed in the D&B HTML and we'll need to retool
|
56
|
+
# the gem.
|
57
|
+
#
|
58
|
+
def self.update_advanced_search_url
|
59
|
+
page = @@agent.get( @@dnb_homepage )
|
60
|
+
advanced_link = page.links.find{|l| l.text.match(/search/i) && l.uri.to_s.match(/AdvancedCompanySearch/)}
|
61
|
+
raise(DunsError, "Unable to find an advanced search link at: #{@@dnb_homepage}") if advanced_link.nil?
|
62
|
+
@@dnb_advanced_search = advanced_link.uri.to_s
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
# Given a search page, extract the results
|
69
|
+
def self.extract_search_results(page)
|
70
|
+
return nil if page.search('div.text-red').size > 0
|
71
|
+
|
72
|
+
# Given a DUNS number search, we only expect one result. For other search types (when/if implemented), loop over all TRs returned in this table
|
73
|
+
company_name = extract_name( page.search("//table[@id='SearchResultsTable']//tr[1]/td[2]") )
|
74
|
+
company_address = extract_address( page.search("//table[@id='SearchResultsTable']//tr[1]/td[3]") )
|
75
|
+
|
76
|
+
{:name => company_name, :address => company_address}
|
77
|
+
end
|
78
|
+
|
79
|
+
# Retrieve an address from the proper D&B HTML for the td element
|
80
|
+
def self.extract_address(td)
|
81
|
+
td.text.gsub(/\s{2,}/, ' ')
|
82
|
+
end
|
83
|
+
|
84
|
+
# Retrieve a name from the proper D&B HTML for the td element
|
85
|
+
def self.extract_name(td)
|
86
|
+
raw_text = td.text.gsub(/\s{2,}/, ' ')
|
87
|
+
with_js = raw_text.match(/var companyName=escape(.+?)var companyAddr/)[1] # First get the surrounding js, to ensure we don't get confused by e.g. a ) in the company name
|
88
|
+
|
89
|
+
# Now we have e.g. >> ('NORTH TEXAS CIRCUIT BOARD CO., INC.'); << and we need to strip off the extraneous stuff
|
90
|
+
with_js[2..-5]
|
91
|
+
end
|
92
|
+
|
93
|
+
# Strip out non-numeric characters, and raise error if remaining number is still invalid
|
94
|
+
def self.enforce_duns_formatting(orig_number)
|
95
|
+
number = orig_number.gsub(/\D/, '')
|
96
|
+
raise(DunsError, "Received invalid DUNS number (must be 9 digits): #{orig_number}") unless number.length == 9
|
97
|
+
return number
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "DunsLookup" do
|
4
|
+
context "Searching by DUNS number" do
|
5
|
+
it "should raise an exception when invalid DUNS numbers are supplied" do
|
6
|
+
lambda { Duns.lookup_duns('123') }.should raise_error(DunsError)
|
7
|
+
lambda { Duns.lookup_duns('abcdefghi') }.should raise_error(DunsError)
|
8
|
+
lambda { Duns.lookup_duns('123abc---') }.should raise_error(DunsError)
|
9
|
+
lambda { Duns.lookup_duns('') }.should raise_error(DunsError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should not raise an exception when a valid DUNS number is supplied" do
|
13
|
+
lambda { Duns.lookup_duns('123456789') }.should_not raise_error(DunsError)
|
14
|
+
lambda { Duns.lookup_duns('123 456 789') }.should_not raise_error(DunsError)
|
15
|
+
lambda { Duns.lookup_duns('123-456-789') }.should_not raise_error(DunsError)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "Returns no results where there shouldn't be any" do
|
19
|
+
Duns.lookup_duns('123456789').should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "Should properly extract company name and address when a valid result is found" do
|
23
|
+
results = Duns.lookup_duns('095444246')
|
24
|
+
results.should_not be_nil
|
25
|
+
results.should be_a_kind_of Hash
|
26
|
+
|
27
|
+
results[:name].should == "NORTH TEXAS CIRCUIT BOARD CO., INC."
|
28
|
+
results[:address].should == "1501 W SHADY GROVE RD, GRAND PRAIRIE, TX"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kdonovan-duns-lookup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kali Donovan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-12 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: nokogiri
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Provides a small wrapper around nokogiri processing of the Dun & Bradstreet website to retrieve business information from DUN numbers.
|
36
|
+
email: kali.donovan@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/duns-lookup.rb
|
52
|
+
- spec/duns-lookup_spec.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/kdonovan/duns-lookup
|
56
|
+
licenses:
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --charset=UTF-8
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.5
|
78
|
+
signing_key:
|
79
|
+
specification_version: 2
|
80
|
+
summary: DSL for looking up Dun & Bradstreet numbers (DUNs)
|
81
|
+
test_files:
|
82
|
+
- spec/duns-lookup_spec.rb
|
83
|
+
- spec/spec_helper.rb
|