Class: Valvat

Inherits:
Object
  • Object
show all
Defined in:
lib/valvat.rb,
lib/valvat/error.rb,
lib/valvat/utils.rb,
lib/valvat/lookup.rb,
lib/valvat/syntax.rb,
lib/valvat/options.rb,
lib/valvat/version.rb,
lib/valvat/checksum.rb,
lib/valvat/checksum/at.rb,
lib/valvat/checksum/be.rb,
lib/valvat/checksum/bg.rb,
lib/valvat/checksum/cy.rb,
lib/valvat/checksum/de.rb,
lib/valvat/checksum/dk.rb,
lib/valvat/checksum/ee.rb,
lib/valvat/checksum/es.rb,
lib/valvat/checksum/fi.rb,
lib/valvat/checksum/fr.rb,
lib/valvat/checksum/gb.rb,
lib/valvat/checksum/gr.rb,
lib/valvat/checksum/hr.rb,
lib/valvat/checksum/hu.rb,
lib/valvat/checksum/ie.rb,
lib/valvat/checksum/it.rb,
lib/valvat/checksum/lt.rb,
lib/valvat/checksum/lu.rb,
lib/valvat/checksum/mt.rb,
lib/valvat/checksum/nl.rb,
lib/valvat/checksum/pl.rb,
lib/valvat/checksum/pt.rb,
lib/valvat/checksum/ro.rb,
lib/valvat/checksum/se.rb,
lib/valvat/checksum/si.rb,
lib/valvat/lookup/base.rb,
lib/valvat/lookup/hmrc.rb,
lib/valvat/lookup/vies.rb,
lib/valvat/configuration.rb,
lib/valvat/hmrc/access_token.rb

Defined Under Namespace

Modules: Checksum, HMRC, Syntax, Utils Classes: Configuration, Lookup, LookupError, Options

Constant Summary collapse

Error =
Class.new(RuntimeError)
MaintenanceError =
Class.new(LookupError)
ServiceUnavailable =
Class.new(MaintenanceError)
MemberStateUnavailable =
Class.new(MaintenanceError)
Timeout =
Class.new(LookupError)
InvalidRequester =
Class.new(LookupError)
BlockedError =
Class.new(LookupError)
RateLimitError =
Class.new(LookupError)
UnknownLookupError =
Class.new(LookupError)
HTTPError =
Class.new(LookupError)
AuthorizationError =
Class.new(LookupError)
VERSION =
'2.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Valvat

Returns a new instance of Valvat.



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

def initialize(raw)
  @raw = Valvat::Utils.normalize(raw || '')
  @vat_country_code, @to_s_wo_country = to_a
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



19
20
21
# File 'lib/valvat.rb', line 19

def raw
  @raw
end

#to_s_wo_countryObject (readonly)

Returns the value of attribute to_s_wo_country.



19
20
21
# File 'lib/valvat.rb', line 19

def to_s_wo_country
  @to_s_wo_country
end

#vat_country_codeObject (readonly)

Returns the value of attribute vat_country_code.



19
20
21
# File 'lib/valvat.rb', line 19

def vat_country_code
  @vat_country_code
end

Class Method Details

.configObject

Read-only access to config



18
19
20
# File 'lib/valvat/configuration.rb', line 18

def self.config
  Configuration.instance
end

.configure(options) ⇒ Object

Configuration options should be set by passing a hash:

Valvat.configure(
  uk: { client_id: '<client_id>', client_secret: '<client_secret>' }
)


13
14
15
# File 'lib/valvat/configuration.rb', line 13

def self.configure(options)
  Configuration.instance.configure(options) unless options.nil?
end

.Options(options) ⇒ Object



43
44
45
# File 'lib/valvat/options.rb', line 43

def self.Options(options)
  options.is_a?(Valvat::Options) ? options : Valvat::Options.new(options)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



57
58
59
# File 'lib/valvat.rb', line 57

def ==(other)
  raw == other.raw
end

#blank?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/valvat.rb', line 21

def blank?
  raw.nil? || raw.strip == ''
end

#european?Boolean

TODO: Remove method / not in use

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/valvat.rb', line 43

def european?
  puts 'DEPRECATED: #european? is deprecated. Instead access Valvat::Utils::EU_MEMBER_STATES directly.'

  Valvat::Utils::EU_MEMBER_STATES.include?(iso_country_code)
end

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/valvat.rb', line 33

def exists?(options = {})
  Valvat::Lookup.validate(self, options)
end

#inspectObject



62
63
64
# File 'lib/valvat.rb', line 62

def inspect
  "#<Valvat #{[raw, iso_country_code].compact.join(' ')}>"
end

#iso_country_codeObject



38
39
40
# File 'lib/valvat.rb', line 38

def iso_country_code
  Valvat::Utils.vat_country_to_iso_country(vat_country_code)
end

#to_aObject



49
50
51
# File 'lib/valvat.rb', line 49

def to_a
  Valvat::Utils.split(raw)
end

#to_sObject



53
54
55
# File 'lib/valvat.rb', line 53

def to_s
  raw
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  Valvat::Syntax.validate(self)
end

#valid_checksum?Boolean

Returns:

  • (Boolean)


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

def valid_checksum?
  Valvat::Checksum.validate(self)
end