lib/demandbase/record.rb in demandbase-0.1.0 vs lib/demandbase/record.rb in demandbase-0.1.1

- old
+ new

@@ -32,11 +32,12 @@ attr_accessor :fortune_1000 attr_accessor :forbes_2000 # Instantiate a new Demandbase Record from a domain name. def initialize(domain) - url = Demandbase::API_URLS[:domain] + "&query=#{domain}" + query = cleanse_domain(domain) + url = Demandbase::API_URLS[:domain] + "&query=#{query}" begin response = JSON.parse(RestClient.get(url)) @company_name = response["domain"]["company_name"] @demandbase_sid = response["domain"]["demandbase_sid"] @@ -65,8 +66,23 @@ @fortune_1000 = response["domain"]["fortune_1000"] @forbes_2000 = response["domain"]["forbes_2000"] rescue => e puts "Problem querying the server: #{e.inspect}" end + end + + # Clean the domain of things like 'http(s)://', 'www', + # '?foo=bar', etc. + # + # Return the domain string. + def cleanse_domain(domain) + domain.downcase! + domain = domain.sub(/^https?\:\/\//, '').sub(/^www./,'') + domain = domain.split("/").first + domain = domain.split("@").last + + domain = PublicSuffix.parse(domain) + domain = "#{domain.sld}.#{domain.tld}" + domain end end end \ No newline at end of file