Module: VatidEu

Extended by:
VatidEu
Included in:
VatidEu
Defined in:
lib/vatid_eu.rb

Constant Summary collapse

BASE_URL =
'http://vatid.eu/check/'
DEFAULT_TIMEOUT =
10
VALID_COUNTRIES =
['AT','BE','BG','CY','CZ','DK','EE','FI','FR','DE','EL','HU','IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES','SE','GB']
@@requester =
nil
@@timeout =
nil

Instance Method Summary collapse

Instance Method Details

#requesterObject



17
18
19
# File 'lib/vatid_eu.rb', line 17

def requester
  @@requester
end

#requester=(data) ⇒ Object



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

def requester=(data)
  @@requester = data
end

#timeoutObject



25
26
27
# File 'lib/vatid_eu.rb', line 25

def timeout
  @@timeout || DEFAULT_TIMEOUT
end

#timeout=(new_timeout) ⇒ Object



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

def timeout=(new_timeout)
  @@timeout = new_timeout
end

#valid?(country_or_id, id = nil) ⇒ Boolean

Returns:

  • (Boolean)


29
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
# File 'lib/vatid_eu.rb', line 29

def valid?(country_or_id, id=nil)
  if id.nil?
    country = country_or_id[0..1].upcase
    id      = country_or_id[2..-1]
  else
    country = country_or_id.upcase
  end
  return false  unless VALID_COUNTRIES.include?(country)
  response = check(country, id)
  if response['error']
    raise response['error'].first['text'].first
  else
    if response['response'].first['valid'].first == 'true'
      if requester
        {
          :request_identifier => response['response'].first['request-identifier'].first,
          :request_date       => response['response'].first['request-date'].first
        }
      else
        true
      end
    else
      false
    end
  end
end