Class: TaxPub::Validator

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

Class Method Summary collapse

Class Method Details

.validate_nokogiri(data) ⇒ Object



13
14
15
16
17
# File 'lib/taxpub/validator.rb', line 13

def self.validate_nokogiri(data)
  if !data.is_a?(Nokogiri::XML::Document)
    raise InvalidTypeError, "Must be a Nokogiri XML document or the parse method has not been executed"
  end
end

.validate_type(data, type) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/taxpub/validator.rb', line 19

def self.validate_type(data, type)
  case type
  when 'String', 'Array', 'Integer', 'Hash'
    raise InvalidParameterValueError, "Must be a #{type}" unless data.is_a?(Object.const_get(type))
  when 'Boolean'
    raise InvalidParameterValueError, "Must be a Boolean" unless [true, false].include?(data)
  when 'File'
    raise InvalidParameterValueError, "Must be a file path & file must exist" unless File.file?(data)
  end
end

.validate_url(data) ⇒ Object



6
7
8
9
10
11
# File 'lib/taxpub/validator.rb', line 6

def self.validate_url(data)
  validate_type(data, 'String')
  if data !~ /\A#{URI::regexp(['http', 'https'])}\z/
    raise InvalidParameterValueError, "URL must be in the form http:// or https://"
  end
end