Class: Valabn::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/valabn/lookup.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.cache_host=(value) ⇒ Object



18
19
20
# File 'lib/valabn/lookup.rb', line 18

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

.cache_port=(value) ⇒ Object



22
23
24
# File 'lib/valabn/lookup.rb', line 22

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

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



26
27
28
# File 'lib/valabn/lookup.rb', line 26

def self.configure
  yield self
end

.guid=(value) ⇒ Object



14
15
16
# File 'lib/valabn/lookup.rb', line 14

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

.validate(number, options = {}) ⇒ Object



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
# 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