Class: Valbn

Inherits:
Object
  • Object
show all
Defined in:
lib/valbn.rb,
lib/valbn/version.rb

Constant Summary collapse

EU_COUNTRIES =
ISO3166::Country.find_all_countries_by_in_eu?(true).map(&:alpha2)
VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, country) ⇒ Valbn

Returns a new instance of Valbn.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/valbn.rb', line 33

def initialize(number, country)
  @country = country
  number = number.to_s.gsub(/\W/, '').upcase

  if EU_COUNTRIES.include?(country)
    vat_country = Valvat::Utils.iso_country_to_vat_country(country)
    number = "#{vat_country}#{number}" unless !!(number =~ /^#{vat_country}[A-Z0-9]+/) # add the country code if it does not exist
  end

  @number = number
end

Class Method Details

.abn=(value) ⇒ Object



15
16
17
18
19
20
# File 'lib/valbn.rb', line 15

def self.abn=(value)
  @@abn = value # Valabn
  value ? require('valabn') : false
rescue
  raise StandardError, 'WARNING: Valnzbn not found. Please run `gem install valabn`.'
end

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Valbn)

    the object that the method was called on



29
30
31
# File 'lib/valbn.rb', line 29

def self.configure
  yield self
end

.nzbn=(value) ⇒ Object



22
23
24
25
26
27
# File 'lib/valbn.rb', line 22

def self.nzbn=(value)
  @@abn = value # Valnzbn
  value ? require('valnzbn') : false
rescue
  raise StandardError, 'WARNING: Valnzbn not found. Please run `gem install valnzbn`.'
end

.vatmoss=(value) ⇒ Object



8
9
10
11
12
13
# File 'lib/valbn.rb', line 8

def self.vatmoss=(value)
  @@vatmoss = value # Valvat
  value ? require('valvat') : false
rescue
  raise StandardError, 'WARNING: Valvat not found. Please run `gem install valvat`.'
end

Instance Method Details

#exists?(options = {}) ⇒ Boolean Also known as: exist?

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
101
102
103
104
105
# File 'lib/valbn.rb', line 45

def exists?(options = {})
  options[:detail] = options[:formatted]

  case @country
  when *EU_COUNTRIES
    if defined?(Valvat) == 'constant'
      result = Valvat.new(@number).exists?(options)

      if options[:formatted] == true && !result.nil? && result.is_a?(Hash)
        result = {
          original_format: result,
          name: result[:name],
          street_line_1: result[:address]
        }
      end
    else
      raise StandardError, 'WARNING: Valvat not found. Please run `gem install valvat`.'
    end
  when 'AU'
    if defined?(Valvat) == 'constant'
      result = Valabn.new(@number).exists?(options)

      if options[:formatted] == true && !result.nil? && result.is_a?(Hash)
        begin
          business = result[:search_by_ab_nv201408_response][:abr_payload_search_results][:response][:business_entity201408]
        rescue
          business = {}
        end

        result = {
          original_format: result,
          name: (business[:main_name] || {})[:organisation_name],
          region: (business[:main_business_physical_address] || {})[:state_code],
          postal_code: (business[:main_business_physical_address] || {})[:postcode]
        }
      end
    else
      raise StandardError, 'WARNING: Valnzbn not found. Please run `gem install valabn`.'
    end
  when 'NZ'
    if defined?(Valnzbn) == 'constant'
      result = Valnzbn.new(@number).exists?(options)
      if options[:formatted] == true && !result.nil? && result.is_a?(Hash)
        address = result[:registeredAddress] || [{}]

        result = {
          original_format: result,
          name: result[:entityName],
          street_line_1: address.first[:address1],
          street_line_2: address.first[:address2],
          city: address.first[:address3],
          postal_code: address.first[:postCode]
        }
      end
    else
      raise StandardError, 'WARNING: Valnzbn not found. Please run `gem install valnzbn`.'
    end
  end

  result
end

#iso_country_codeObject



108
109
110
111
112
113
114
115
# File 'lib/valbn.rb', line 108

def iso_country_code
  case @country
  when *EU_COUNTRIES
    Valvat.new(@number).iso_country_code
  else
    @country
  end
end