Class: Mihari::TypeChecker

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/mihari/type_checker.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs) ⇒ TypeChecker

Returns a new instance of TypeChecker.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/mihari/type_checker.rb', line 9

def initialize(*args, **kwargs)
  super

  raise ArgumentError if data.is_a?(Hash)

  @data = data.to_s
end

Class Method Details

.detailed_type(data) ⇒ String?

Returns:

  • (String, nil)


77
78
79
# File 'lib/mihari/type_checker.rb', line 77

def detailed_type(data)
  new(data).detailed_type
end

.type(data) ⇒ String?

Returns:

  • (String, nil)


72
73
74
# File 'lib/mihari/type_checker.rb', line 72

def type(data)
  new(data).type
end

Instance Method Details

#detailed_typeString?

Returns:

  • (String, nil)


61
62
63
64
65
66
67
68
# File 'lib/mihari/type_checker.rb', line 61

def detailed_type
  return "md5" if md5?
  return "sha1" if sha1?
  return "sha256" if sha256?
  return "sha512" if sha512?

  type
end

#domain?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/mihari/type_checker.rb', line 31

def domain?
  uri = Addressable::URI.parse("http://#{data}")
  uri.host == data && PublicSuffix.valid?(uri.host)
rescue Addressable::URI::InvalidURIError => _e
  false
end

#hash?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/mihari/type_checker.rb', line 18

def hash?
  md5? || sha1? || sha256? || sha512?
end

#ip?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/mihari/type_checker.rb', line 23

def ip?
  IPAddr.new data
  true
rescue IPAddr::InvalidAddressError => _e
  false
end

#mail?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/mihari/type_checker.rb', line 47

def mail?
  EmailAddress.valid? data, host_validation: :syntax
end

#typeString?

Returns:

  • (String, nil)


52
53
54
55
56
57
58
# File 'lib/mihari/type_checker.rb', line 52

def type
  return "hash" if hash?
  return "ip" if ip?
  return "domain" if domain?
  return "url" if url?
  return "mail" if mail?
end

#url?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/mihari/type_checker.rb', line 39

def url?
  uri = Addressable::URI.parse(data)
  uri.scheme && uri.host && uri.path && PublicSuffix.valid?(uri.host)
rescue Addressable::URI::InvalidURIError => _e
  false
end