class Valabn::Lookup

Constants

WSDL

Public Class Methods

cache_host=(value) click to toggle source
# File lib/valabn/lookup.rb, line 18
def self.cache_host=(value)
  @@cache_host = value
end
cache_port=(value) click to toggle source
# File lib/valabn/lookup.rb, line 22
def self.cache_port=(value)
  @@cache_port = value
end
configure() { |self| ... } click to toggle source
# File lib/valabn/lookup.rb, line 26
def self.configure
  yield self
end
guid=(value) click to toggle source
# File lib/valabn/lookup.rb, line 14
def self.guid=(value)
  @@guid = value
end
validate(number, options = {}) click to toggle source
# File lib/valabn/lookup.rb, line 30
def self.validate(number, options = {})
  number = number.to_s.gsub(/\W/, '')

  if Valabn::Utils.valid_format?(number)
    cache = if @@cache_port && @@cache_host
      redis_client = Redis.new(host: @@cache_host, port: @@cache_port, db: 15)
      namespaced_redis = Redis::Namespace.new(:valabn_cache, redis: redis_client)

      JSON.parse(namespaced_redis.get(number) || '{}', symbolize_names: true)
    else
      {}
    end

    if cache.any? && Time.at(cache[:expires_at]) > Time.now
      cache
    else
      options[:includeHistoricalDetails] ||= 'n'
      options[:searchString] = number
      options[:authenticationGuid] = options[:guid] || @@guid

      client = Savon.client(wsdl: WSDL)

      response = JSON.parse(client.call(:search_by_ab_nv201408, message: options).body.to_json, symbolize_names: true)
      response[:expires_at] = (Time.now + 7200).to_i

      namespaced_redis.set(number, response.to_json, ex: 7200) if @@cache_port && @@cache_host

      response
    end
  else
    {}
  end
end