Skip to content

Commit

Permalink
Replace .parse and .from methods with .lookup and .import met…
Browse files Browse the repository at this point in the history
…hods.
  • Loading branch information
postmodern committed Oct 9, 2022
1 parent 7582ea7 commit 0b230d2
Show file tree
Hide file tree
Showing 18 changed files with 486 additions and 242 deletions.
21 changes: 18 additions & 3 deletions lib/ronin/db/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#

require 'ronin/db/model'
require 'ronin/db/model/importable'
require 'ronin/db/model/last_scanned_at'

require 'active_record'
Expand All @@ -34,6 +35,7 @@ module DB
class Address < ActiveRecord::Base

include Model
include Model::Importable
include Model::LastScannedAt

self.abstract_class = true
Expand All @@ -58,7 +60,20 @@ class Address < ActiveRecord::Base
attribute :created_at, :time

#
# Parses the address.
# Looks up the address.
#
# @param [String] address
# The address to query.
#
# @return [Address, nil]
# The found address.
#
def self.lookup(address)
find_by(address: address)
end

#
# Imports an address.
#
# @param [String] address
# The address to parse.
Expand All @@ -68,8 +83,8 @@ class Address < ActiveRecord::Base
#
# @api public
#
def self.parse(address)
find_or_initialize_by(address: address)
def self.import(address)
create(address: address)
end

#
Expand Down
21 changes: 15 additions & 6 deletions lib/ronin/db/advisory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#

require 'ronin/db/model'
require 'ronin/db/model/importable'

require 'active_record'

Expand All @@ -30,6 +31,7 @@ module DB
class Advisory < ActiveRecord::Base

include Model
include Model::Importable

self.primary_key = :id

Expand Down Expand Up @@ -107,6 +109,17 @@ def self.parse(string)
end
end

#
# Looks up the advisory.
#
# @param [String] id
#
# @return [Advisory, nil]
#
def self.lookup(id)
find_by(id: id)
end

#
# Parses an Advisory ID String.
#
Expand All @@ -118,12 +131,8 @@ def self.parse(string)
#
# @api public
#
def self.parse(id)
if (advisory = find_by(id: id))
advisory
else
new(**ID.parse(id))
end
def self.import(id)
create(**ID.parse(id))
end

#
Expand Down
45 changes: 20 additions & 25 deletions lib/ronin/db/email_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#

require 'ronin/db/model'
require 'ronin/db/model/importable'

require 'active_record'
require 'uri/mailto'
Expand All @@ -31,6 +32,7 @@ module DB
class EmailAddress < ActiveRecord::Base

include Model
include Model::Importable

# @!attribute [rw] id
# The primary key of the email address.
Expand Down Expand Up @@ -129,7 +131,20 @@ def self.with_user_name(name)
end

#
# Parses an email address.
# Looks up the email address.
#
# @param [String] email
# The raw email address string.
#
# @return [EmailAddress, nil]
# The found email address.
#
def self.lookup(email)
find_by(address: email)
end

#
# Imports an email address.
#
# @param [String] email
# The email address to parse.
Expand All @@ -142,7 +157,7 @@ def self.with_user_name(name)
#
# @api public
#
def self.parse(email)
def self.import(email)
if email =~ /\s/
raise(ArgumentError,"email address #{email.inspect} must not contain spaces")
end
Expand All @@ -158,33 +173,13 @@ def self.parse(email)
raise(ArgumentError,"email address #{email.inspect} must have a host name")
end

return find_or_initialize_by(
return create(
address: normalized_email,
user_name: UserName.find_or_initialize_by(name: user),
host_name: HostName.find_or_initialize_by(name: host)
user_name: UserName.find_or_import(user),
host_name: HostName.find_or_import(host)
)
end

#
# Creates a new Email Address.
#
# @param [URI::MailTo, #to_s] email
# The URI or String to create the Email Address from.
#
# @return [EmailAddress]
# The new Email Address.
#
# @api public
#
def self.from(email)
email = case email
when URI::MailTo then email.to # URI::MailTo#to
else email.to_s
end

return parse(email.to_s)
end

#
# The user of the email address.
#
Expand Down
27 changes: 20 additions & 7 deletions lib/ronin/db/host_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#

require 'ronin/db/model'
require 'ronin/db/model/importable'
require 'ronin/db/model/last_scanned_at'

require 'active_record'
Expand All @@ -32,6 +33,7 @@ module DB
class HostName < ActiveRecord::Base

include Model
include Model::Importable
include Model::LastScannedAt

# @!attribute [rw] id
Expand Down Expand Up @@ -98,18 +100,29 @@ class HostName < ActiveRecord::Base
has_many :urls, class_name: 'URL'

#
# Parses the host name.
# Looks up the host name.
#
# @param [String] name
# The host name to parse.
# The raw host name.
#
# @return [HostName]
# The new or previously saved host name record.
# @return [HostName, nil]
# The found host name.
#
# @api public
def self.lookup(name)
find_by(name: name)
end

#
# Creates a new host name.
#
# @param [String] name
# The host name.
#
# @return [HostName]
# The created host name record.
#
def self.parse(name)
find_or_initialize_by(name: name)
def self.import(name)
create(name: name)
end

#
Expand Down
3 changes: 3 additions & 0 deletions lib/ronin/db/mac_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ def to_i
end
end
end

require 'ronin/db/ip_address_mac_address'
require 'ronin/db/ip_address'
25 changes: 21 additions & 4 deletions lib/ronin/db/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,34 @@ class Password < ActiveRecord::Base
# @return [Array<UserName>]
has_many :user_names, through: :credentials

#
# Looks up the password.
#
# @param [#to_s] password
# The password to lookup.
#
# @return [Password, nil]
# The found password.
#
# @api public
#
def self.lookup(password)
find_by(plain_text: password.to_s)
end

#
# Parses a password.
#
# @param [#to_s] password
# The password to parse.
# The password to import.
#
# @return [Password]
# The parsed password.
# The imported password.
#
# @api public
#
def self.parse(password)
find_or_initialize_by(plain_text: password.to_s)
def self.import(password)
create(plain_text: password.to_s)
end

#
Expand Down Expand Up @@ -145,3 +160,5 @@ def to_s
end
end
end

require 'ronin/db/credential'
22 changes: 11 additions & 11 deletions lib/ronin/db/port.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,33 @@ class Port < ActiveRecord::Base
has_many :open_ports, dependent: :destroy

#
# Creates a new Port.
# Looks up a port by it's number.
#
# @param [String, Integer] number
# The port number.
# The port number to query.
#
# @return [Port]
# The new or previously saved port.
# @return [Port, nil]
# The found port number.
#
# @api public
#
def self.from(number)
find_or_initialize_by(number: number)
def self.lookup(number)
find_by(number: number)
end

#
# Parses a port number.
# Creates a new Port.
#
# @param [String, Integer] number
# The port number to parse.
# The port number.
#
# @return [Port]
# The parsed port.
# The new or previously saved port.
#
# @api public
#
def self.parse(number)
from(number.to_i)
def self.import(number)
create(number: number)
end

#
Expand Down
Loading

0 comments on commit 0b230d2

Please sign in to comment.