class TowerDataApi::Api

Public Class Methods

new(api_key=nil, options = {}) click to toggle source
   # File lib/towerdata_api.rb
34 def initialize(api_key=nil, options = {})
35   @api_key = api_key.nil? ? Configuration.api_key : api_key
36   options.each do |key, value|
37     Configuration.send("#{key}=", value)
38   end 
39 end

Public Instance Methods

bulk_query(set) click to toggle source

Get bulk request set - is array of hashes in format

[{email: 'first_email'}, {email: 'second_email'}]
    # File lib/towerdata_api.rb
128 def bulk_query(set)
129   get_bulk_response(bulk_path, JSON.generate(set))
130 end
email_validation(email) click to toggle source

For given email returns EmailValidation object This method will rise TowerDataApi::Error::Unsupported if yours api key doesn't have email validation field

    # File lib/towerdata_api.rb
100 def email_validation email
101   begin
102     result = query_by_email email
103   rescue TowerDataApi::Error::BadRequest 
104     result = {'email_validation' => {'ok' => false}}
105   end
106 
107   if result.has_key? 'email_validation'
108     EmailValidation.new result['email_validation']
109   else
110     raise TowerDataApi::Error::Unsupported, 'Email validation is not supported with yours api key.' 
111   end
112 end
query_by_email(email, options = {}) click to toggle source

Takes an e-mail and returns a hash which maps attribute fields onto attributes Options:

:hash_email     - the email will be hashed before it's sent to Rapleaf
   # File lib/towerdata_api.rb
44 def query_by_email(email, options = {})
45   if options[:hash_email]
46     query_by_sha1(Digest::SHA1.hexdigest(email.downcase))
47   else
48     get_json_response("#{base_path}&email=#{url_encode(email)}")
49   end
50 end
query_by_md5(md5_email, options = {}) click to toggle source

Takes an e-mail that has already been hashed by md5 and returns a hash which maps attribute fields onto attributes, optionally showing available data in the response

   # File lib/towerdata_api.rb
55 def query_by_md5(md5_email, options = {})
56   get_json_response("#{base_path}&md5_email=#{url_encode(md5_email)}")
57 end
query_by_nap(first, last, street, city, state, options = {}) click to toggle source

Takes first name, last name, and postal (street, city, and state acronym), and returns a hash which maps attribute fields onto attributes Options:

:email          - query with an email to increase the hit rate
   # File lib/towerdata_api.rb
70 def query_by_nap(first, last, street, city, state, options = {})
71   if options[:email]
72     url = "#{base_path}&email=#{url_encode(options[:email])}&first=#{url_encode(first)}&last=#{url_encode(last)}" +
73       "&street=#{url_encode(street)}&city=#{url_encode(city)}&state=#{url_encode(state)}"
74   else
75     url = "#{base_path}&first=#{url_encode(first)}&last=#{url_encode(last)}" +
76       "&street=#{url_encode(street)}&city=#{url_encode(city)}&state=#{url_encode(state)}"
77   end
78   get_json_response(url)
79 end
query_by_naz(first, last, zip4, options = {}) click to toggle source

Takes first name, last name, and zip4 code (5-digit zip and 4-digit extension separated by a dash as a string), and returns a hash which maps attribute fields onto attributes Options:

:email          - query with an email to increase the hit rate
   # File lib/towerdata_api.rb
86 def query_by_naz(first, last, zip4, options = {})
87   if options[:email]
88     url = "#{base_path}&email=#{url_encode(options[:email])}&first=#{url_encode(first)}&last=#{url_encode(last)}&zip4=#{zip4}"
89   else
90     url = "#{base_path}&first=#{url_encode(first)}&last=#{url_encode(last)}&zip4=#{zip4}"
91   end
92   get_json_response(url)
93 end
query_by_sha1(sha1_email, options = {}) click to toggle source

Takes an e-mail that has already been hashed by sha1 and returns a hash which maps attribute fields onto attributes, optionally showing available data in the response

   # File lib/towerdata_api.rb
62 def query_by_sha1(sha1_email, options = {})
63   get_json_response("#{base_path}&sha1_email=#{url_encode(sha1_email)}")
64 end
valid_email?(email) click to toggle source

Check is email valid This method will rise TowerDataApi::Error::Api if yours api key doesn't have email validation field Value can be true, false and nil

    # File lib/towerdata_api.rb
118 def valid_email? email
119   email_validation(email).valid?
120 end

Private Instance Methods

api_path() click to toggle source
    # File lib/towerdata_api.rb
196 def api_path
197   "/v#{API_VERSION}"
198 end
base_path() click to toggle source
    # File lib/towerdata_api.rb
192 def base_path
193   "#{api_path}#{BASE_PATH}?api_key=#{@api_key}"
194 end
bulk_path() click to toggle source
    # File lib/towerdata_api.rb
188 def bulk_path
189   "#{api_path}#{BULK_PATH}?api_key=#{@api_key}"
190 end
get_bulk_response(path, data) click to toggle source
    # File lib/towerdata_api.rb
138 def get_bulk_response(path, data)
139   response = Timeout::timeout(@BULK_TIMEOUT) do
140     begin
141       http_client.post(path, data, HEADERS.merge('Content-Type' => 'application/json'))
142     rescue EOFError # Connection cut out. Just try a second time.
143       http_client.post(path, data, HEADERS.merge('Content-Type' => 'application/json'))
144     end
145   end
146 
147   if response.code =~ /^2\d\d/
148     (response.body && response.body != "") ? JSON.parse(response.body) : []
149   else
150     raise TowerDataApi::Error::Api, "Error Code #{response.code}: \"#{response.body}\""
151   end
152 end
get_json_response(path) click to toggle source

Takes a url and returns a hash mapping attribute fields onto attributes Note that an exception is raised in the case that an HTTP response code other than 200 is sent back The error code and error body are put in the exception's message

    # File lib/towerdata_api.rb
158 def get_json_response(path)
159   response = Timeout::timeout(Configuration.timeout) do
160     begin
161       http_client.get(path, HEADERS)
162     rescue EOFError # Connection cut out. Just try a second time.
163       http_client.get(path, HEADERS)
164     end
165   end
166 
167   if response.code =~ /^2\d\d/
168     (response.body && response.body != "") ? JSON.parse(response.body) : {}
169   elsif response.code == '400' 
170     raise TowerDataApi::Error::BadRequest, "Bad request#{response.code}: \"#{response.body}\""
171   else
172     raise TowerDataApi::Error::Api, "Error Code #{response.code}: \"#{response.body}\""
173   end
174 end
http_client() click to toggle source

Returns http connection to HOST on PORT

    # File lib/towerdata_api.rb
177 def http_client
178   unless defined?(@http_client)
179     @http_client = Net::HTTP.new(HOST, PORT)
180     @http_client.use_ssl = true
181     @http_client.ca_file = Configuration.ca_file if Configuration.ca_file
182     #@http_client.verify_mode = OpenSSL::SSL::VERIFY_PEER
183     @http_client.start
184   end
185   @http_client
186 end
url_encode(value) click to toggle source
    # File lib/towerdata_api.rb
134 def url_encode value 
135    URI::encode value
136 end