Sha256: 858a97bb1ebd9dafecbc862d49b995dc022f2d51d2e330edd6ce095ebaed1a1f

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 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, ex: 7200) 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.3 lib/valabn/lookup.rb