Sha256: 99db80a90dd7ad8986496d126924dd00d05a7c51d47126cd25a3abcac0a96e52

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'savon'
require 'json'
require 'redis-namespace'
require_relative 'utils'

class Valabn
  class Lookup

    WSDL = 'http://www.abn.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL'
    @@cache_host = nil
    @@cache_port = nil
    @@guid = nil

    def self.guid=(value)
      @@guid = value
    end

    def self.cache_host=(value)
      @@cache_host = value
    end

    def self.cache_port=(value)
      @@cache_port = value
    end

    def self.configure
      yield self
    end

    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) if @@cache_port && @@cache_host

          response
        end
      else
        {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
valabn-0.1.0 lib/valabn/lookup.rb