Skip to content

Commit

Permalink
DSL cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kubicek committed Mar 9, 2024
1 parent 650ea29 commit 240e862
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
12 changes: 6 additions & 6 deletions examples/nic.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
describe "nic.cz", type: :domain do
describe :rdap do
domain "nic.cz" do
rdap do
status :server_transfer_prohibited
registrar "REG-CZNIC"
registrant "CZ-NIC"
expires "2027-03-14"
end

describe :dns do
dns do
dnssec :valid
nameservers ["a.ns.nic.cz", "b.ns.nic.cz", "d.ns.nic.cz"]
record "www", :a, "217.31.205.50"
record "www", :aaaa, "2001:1488:0:3::2"
end
end

describe "217.31.205.50", type: :ipv4 do
port 80, type: :tcp do
ipv4 "217.31.205.50" do
tcp 80 do
status :open
end
port 443, type: :tcp, protocol: :https do
https 443 do
host "www.nic.cz"
server "nginx"
status_code 200
Expand Down
61 changes: 34 additions & 27 deletions lib/xmon/descriptions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Xmon
class Description
attr_reader :parent
attr_reader :parent, :results

def initialize
@descriptions = []
Expand All @@ -18,16 +18,20 @@ def define_attributes(attributes)
end
end

def domain(name, &)
@description = DomainDescription.new(name)
describe(&)
end

def ipv4(address, &)
@description = IPv4Description.new(address)
describe(&)
end

def describe(*args, **kwargs, &)
unless @description
if kwargs[:type] == :domain
@description = DomainDescription.new(args[0])
elsif kwargs[:type] == :ipv4
@description = IPv4Description.new(args[0])
else
puts "unknown block given with args: #{args} and kwargs: #{kwargs}"
@description = Description.new
end
puts "unknown block given with args: #{args} and kwargs: #{kwargs}"
exit
end

if block_given?
Expand Down Expand Up @@ -67,13 +71,14 @@ def friendly_name
@name
end

def describe(*args, **kwarg)
if args == [:rdap]
@description = Xmon::RDAP.new(self)
elsif args == [:dns]
@description = Xmon::DNS.new(self)
end
super
def rdap(&)
@description = Xmon::RDAP.new(self)
describe(&)
end

def dns(&)
@description = Xmon::DNS.new(self)
describe(&)
end
end

Expand All @@ -87,17 +92,19 @@ def friendly_name
@address
end

def describe(*, **kwargs)
if kwargs[:type] == :tcp
@description = if kwargs[:protocol] == :https
Xmon::SSL.new(self, *, **kwargs)
else
Xmon::TCP.new(self, *, **kwargs)
end
elsif kwargs[:type] == :udp
@description = Xmon::UDP.new(*, **kwargs)
end
super
def udp(*, **, &)
@description = Xmon::UDP.new(self, *, **)
describe(&)
end

def tcp(*, **, &)
@description = Xmon::TCP.new(self, *, **)
describe(&)
end

def https(*, **, &)
@description = Xmon::SSL.new(self, *, **)
describe(&)
end

def port(...)
Expand Down

0 comments on commit 240e862

Please sign in to comment.