Class: Mihari::DataType
- Inherits:
-
Object
- Object
- Mihari::DataType
- Defined in:
- lib/mihari/data_type.rb
Overview
(Artifact) Data Type
Instance Attribute Summary collapse
- #data ⇒ String readonly
Class Method Summary collapse
Instance Method Summary collapse
- #detailed_type ⇒ String?
- #domain? ⇒ Boolean
- #hash? ⇒ Boolean
-
#initialize(data) ⇒ DataType
constructor
A new instance of DataType.
- #ip? ⇒ Boolean
- #mail? ⇒ Boolean
- #md5? ⇒ Boolean
- #sha1? ⇒ Boolean
- #sha256? ⇒ Boolean
- #sha512? ⇒ Boolean
- #type ⇒ String?
- #url? ⇒ Boolean
Constructor Details
#initialize(data) ⇒ DataType
Returns a new instance of DataType.
16 17 18 19 20 |
# File 'lib/mihari/data_type.rb', line 16 def initialize(data) raise ArgumentError if data.is_a?(Hash) @data = data.to_s end |
Instance Attribute Details
#data ⇒ String (readonly)
11 12 13 |
# File 'lib/mihari/data_type.rb', line 11 def data @data end |
Class Method Details
.detailed_type(data) ⇒ String?
98 99 100 |
# File 'lib/mihari/data_type.rb', line 98 def detailed_type(data) new(data).detailed_type end |
.type(data) ⇒ String?
93 94 95 |
# File 'lib/mihari/data_type.rb', line 93 def type(data) new(data).type end |
Instance Method Details
#detailed_type ⇒ String?
64 65 66 67 68 69 |
# File 'lib/mihari/data_type.rb', line 64 def detailed_type found = %i[md5? sha1? sha256? sha512?].find { |method| send(method) if respond_to?(method) } return found[...-1].to_s unless found.nil? type end |
#domain? ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/mihari/data_type.rb', line 35 def domain? Try[Addressable::URI::InvalidURIError] do uri = Addressable::URI.parse("http://#{data}") uri.host == data && PublicSuffix.valid?(uri.host) end.recover { false }.value! end |
#hash? ⇒ Boolean
23 24 25 |
# File 'lib/mihari/data_type.rb', line 23 def hash? md5? || sha1? || sha256? || sha512? end |
#ip? ⇒ Boolean
28 29 30 31 32 |
# File 'lib/mihari/data_type.rb', line 28 def ip? Try[IPAddr::InvalidAddressError] do IPAddr.new(data).to_s == data end.recover { false }.value! end |
#mail? ⇒ Boolean
51 52 53 |
# File 'lib/mihari/data_type.rb', line 51 def mail? EmailAddress.valid? data, host_validation: :syntax end |
#md5? ⇒ Boolean
72 73 74 |
# File 'lib/mihari/data_type.rb', line 72 def md5? data.match?(/^[A-Fa-f0-9]{32}$/) end |
#sha1? ⇒ Boolean
77 78 79 |
# File 'lib/mihari/data_type.rb', line 77 def sha1? data.match?(/^[A-Fa-f0-9]{40}$/) end |
#sha256? ⇒ Boolean
82 83 84 |
# File 'lib/mihari/data_type.rb', line 82 def sha256? data.match?(/^[A-Fa-f0-9]{64}$/) end |
#sha512? ⇒ Boolean
87 88 89 |
# File 'lib/mihari/data_type.rb', line 87 def sha512? data.match?(/^[A-Fa-f0-9]{128}$/) end |
#type ⇒ String?
56 57 58 59 60 61 |
# File 'lib/mihari/data_type.rb', line 56 def type found = %i[hash? ip? domain? url? mail?].find { |method| send(method) if respond_to?(method) } return nil if found.nil? found[...-1].to_s end |
#url? ⇒ Boolean
43 44 45 46 47 48 |
# File 'lib/mihari/data_type.rb', line 43 def url? Try[Addressable::URI::InvalidURIError] do uri = Addressable::URI.parse(data) uri.scheme && uri.host && uri.path && PublicSuffix.valid?(uri.host) end.recover { false }.value! end |