Class: ABNSearch

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

Defined Under Namespace

Modules: Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(guid = nil, options = {}) ⇒ ABNSearch

Setup a new instance of the ABN search class.

Parameters:

  • guid (String) (defaults to: nil)
    • the ABR GUID for Web Services access

  • options (Hash) (defaults to: {})
    • options detailed below

Options Hash (options):



32
33
34
35
36
37
38
39
# File 'lib/abn_search.rb', line 32

def initialize(guid=nil, options = {})
  self.errors = []
  self.guid = guid unless guid.nil?
  self.proxy = options[:proxy] || nil
  self.client_options = {}
  self.client_options = { :wsdl => "http://www.abn.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL" }
  self.client_options.merge!({ :proxy => self.proxy }) unless self.proxy.nil?
end

Instance Attribute Details

#client_optionsObject

Returns the value of attribute client_options.



23
24
25
# File 'lib/abn_search.rb', line 23

def client_options
  @client_options
end

#errorsObject

Returns the value of attribute errors.



23
24
25
# File 'lib/abn_search.rb', line 23

def errors
  @errors
end

#guidObject

Returns the value of attribute guid.



23
24
25
# File 'lib/abn_search.rb', line 23

def guid
  @guid
end

#proxyObject

Returns the value of attribute proxy.



23
24
25
# File 'lib/abn_search.rb', line 23

def proxy
  @proxy
end

Instance Method Details

#parse_search_result(result) ⇒ Object

Parses results for a search by ABN



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/abn_search.rb', line 103

def parse_search_result(result)
  result = {
    abn:            result[:abn][:identifier_value],
    entity_type:    result[:entity_type].blank? ? "" : result[:entity_type][:entity_description],
    status:         result[:entity_status].blank? ? "" : result[:entity_status][:entity_status_code],
    main_name:      result[:main_name].blank? ? "" : result[:main_name][:organisation_name],
    trading_name:   result[:main_trading_name].blank? ? "" : result[:main_trading_name][:organisation_name],
    legal_name:     result[:legal_name].blank? ? "" : "#{result[:legal_name][:given_name]} #{result[:legal_name][:family_name]}",
    other_trading_name: result[:other_trading_name].blank? ? "" : result[:other_trading_name][:organisation_name]
  }

  # Work out what we should return as a name
  if !result[:trading_name].blank?
    result[:name] = result[:trading_name]
  elsif !result[:main_name].blank?
    result[:name] = result[:main_name]
  elsif !result[:other_trading_name].blank?
    result[:name] = result[:other_trading_name]
  else
    result[:name] = result[:legal_name]
  end

  return result
end

#search(abn) ⇒ ABNSearch

Performs an ABR search for the ABN setup upon initialization

Parameters:

  • abn (String)
    • the abn you wish to search for

Returns:

  • (ABNSearch)

    search results in class instance



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/abn_search.rb', line 45

def search(abn)
  self.errors << "No ABN provided." && return if abn.nil?
  self.errors << "No GUID provided. Please obtain one at - http://www.abr.business.gov.au/Webservices.aspx" && return if self.guid.nil?

  begin
    client = Savon.client(self.client_options)

    response = client.call(:abr_search_by_abn, message: { authenticationGuid: self.guid, searchString: abn.gsub(" ", ""), includeHistoricalDetails: "N" })
    result = response.body[:abr_search_by_abn_response][:abr_payload_search_results][:response][:business_entity]
    return parse_search_result(result)
  rescue => ex
    self.errors << ex.to_s
  end
end

#search_by_name(name, states = ['NSW'], postcode = 'ALL') ⇒ Object

Searches the ABR registry by name. Simply pass in the search term and which state(s) to search in.

Parameters:

  • name (String)
    • the search term

  • states (Array) (defaults to: ['NSW'])
    • a list of states that you wish to filter by

  • postcode (String) (defaults to: 'ALL')
    • the postcode you wish to filter by



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/abn_search.rb', line 65

def search_by_name(name, states=['NSW'], postcode='ALL')

  begin
    client = Savon.client(self.client_options)
    request = {
      externalNameSearch: {
        authenticationGuid: self.guid, name: name,
        filters: {
          nameType: {
            tradingName: 'Y', legalName: 'Y'
          },
          postcode: postcode,
          "stateCode" => {
            'QLD' => states.include?('QLD') ? "Y" : "N",
            'NT' => states.include?('NT') ? "Y" : "N",
            'SA' => states.include?('SA') ? "Y" : "N",
            'WA' => states.include?('WA') ? "Y" : "N",
            'VIC' => states.include?('VIC') ? "Y" : "N",
            'ACT' => states.include?('ACT') ? "Y" : "N",
            'TAS' => states.include?('TAS') ? "Y" : "N",
            'NSW' => states.include?('NSW') ? "Y" : "N"
          }
        }
      },
      authenticationGuid: self.guid
    }

    response = client.call(:abr_search_by_name, message: request)
    results = response.body[:abr_search_by_name_response][:abr_payload_search_results][:response][:search_results_list][:search_results_record]

    return [parse_search_result(results)] if !results.is_a?(Array)
    return results.map do |r| parse_search_result(r) end
  rescue => ex
    self.errors << ex.to_s
  end
end

#valid?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/abn_search.rb', line 128

def valid?
  self.errors.size == 0
end