Class: Leadspend::Client
- Inherits:
-
Object
- Object
- Leadspend::Client
- Defined in:
- lib/leadspend/client.rb
Overview
with convenience methods like unreachable? and illegitimate?.
Instance Method Summary collapse
-
#fetch_result(address) ⇒ Object
fetch a result from Leadspend, doing failover if necessary.
-
#initialize(opts = {}) ⇒ Client
constructor
Instantiate a client.
-
#validate(address) ⇒ Object
Return a boolean based on whether an address is valid or not.
Constructor Details
#initialize(opts = {}) ⇒ Client
Instantiate a client. Recommended options: :ca_file is the full path to a CA Cert file. :timeout is the server-side timeout in seconds, between 3 and 15. If a result is not available within the timeout, the response will be an “unknown” result.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/leadspend/client.rb', line 19 def initialize(opts={}) = opts if defined?(HashWithIndifferentAccess) = HashWithIndifferentAccess.new(opts) end @username = [:username] @password = [:password] if @username.nil? or @password.nil? raise Leadspend::Exceptions::LeadspendException, "No username or password specified!" end @servers = [:servers] || Leadspend::DEFAULT_SERVERS @api_version = [:version] || Leadspend::DEFAULT_VERSION # path to the CA file: download http://curl.haxx.se/ca/cacert.pem and put # it someplace on your server. @ca_file = [:ca_file] # set the JSON parser to use. Use the one specified, or choose a reasonable default. case [:json_parser] when 'yajl' require "#{File.dirname(__FILE__)}/parser/yajl_parser" Leadspend::Result.json_parser=Leadspend::Parser::YajlParser when 'rails' require "#{File.dirname(__FILE__)}/parser/rails_parser" Leadspend::Result.json_parser=Leadspend::Parser::RailsParser when 'json' require "#{File.dirname(__FILE__)}/parser/json_parser" Leadspend::Result.json_parser=Leadspend::Parser::JSONParser else if defined? Rails require "#{File.dirname(__FILE__)}/parser/rails_parser" Leadspend::Result.json_parser=Leadspend::Parser::RailsParser else require "#{File.dirname(__FILE__)}/parser/json_parser" Leadspend::Result.json_parser=Leadspend::Parser::JSONParser end end @server_index = 0 @request_options = {} # optional: allow a server timeout. Minimum is 3, max is 15 if [:timeout] @request_options[:timeout] = [:timeout].to_i if @request_options[:timeout] < Leadspend::MIN_TIMEOUT @request_options[:timeout] = Leadspend::MIN_TIMEOUT elsif @request_options[:timeout] > Leadspend::MAX_TIMEOUT @request_options[:timeout] = Leadspend::MAX_TIMEOUT end end end |
Instance Method Details
#fetch_result(address) ⇒ Object
fetch a result from Leadspend, doing failover if necessary. R Returns a Leadspend::Result object if successful, or an exception if there was a failure.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/leadspend/client.rb', line 80 def fetch_result(address) retry_once = true begin result = query("/#{@api_version}/validity", address, @request_options) rescue Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, IOError, Leadspend::Exceptions::ServerException, Leadspend::Exceptions::ServerBusyException if retry_once retry_once = false @server_index = (@server_index + 1) % @servers.length retry else raise $! end end end |
#validate(address) ⇒ Object
Return a boolean based on whether an address is valid or not.
Valid statuses: unknown, validated.
Invalid statuses: anything else.
73 74 75 76 |
# File 'lib/leadspend/client.rb', line 73 def validate(address) result = fetch_result(address) return result.verified? || result.unknown? end |