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?
96 97 98 |
# File 'lib/mihari/data_type.rb', line 96 def detailed_type(data) new(data).detailed_type end |
.type(data) ⇒ String?
91 92 93 |
# File 'lib/mihari/data_type.rb', line 91 def type(data) new(data).type end |
Instance Method Details
#detailed_type ⇒ String?
62 63 64 65 66 67 |
# File 'lib/mihari/data_type.rb', line 62 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
33 34 35 36 37 38 |
# File 'lib/mihari/data_type.rb', line 33 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 |
# File 'lib/mihari/data_type.rb', line 28 def ip? Try[IPAddr::InvalidAddressError] { IPAddr.new(data).to_s == data }.recover { false }.value! end |
#mail? ⇒ Boolean
49 50 51 |
# File 'lib/mihari/data_type.rb', line 49 def mail? EmailAddress.valid? data, host_validation: :syntax end |
#md5? ⇒ Boolean
70 71 72 |
# File 'lib/mihari/data_type.rb', line 70 def md5? data.match?(/^[A-Fa-f0-9]{32}$/) end |
#sha1? ⇒ Boolean
75 76 77 |
# File 'lib/mihari/data_type.rb', line 75 def sha1? data.match?(/^[A-Fa-f0-9]{40}$/) end |
#sha256? ⇒ Boolean
80 81 82 |
# File 'lib/mihari/data_type.rb', line 80 def sha256? data.match?(/^[A-Fa-f0-9]{64}$/) end |
#sha512? ⇒ Boolean
85 86 87 |
# File 'lib/mihari/data_type.rb', line 85 def sha512? data.match?(/^[A-Fa-f0-9]{128}$/) end |
#type ⇒ String?
54 55 56 57 58 59 |
# File 'lib/mihari/data_type.rb', line 54 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
41 42 43 44 45 46 |
# File 'lib/mihari/data_type.rb', line 41 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 |