21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-05-18 08:28:39 +02:00
chexpire/app/services/whois.rb

51 lines
1.1 KiB
Ruby
Raw Normal View History

2018-05-30 12:04:07 +02:00
require "null_logger"
require "domain_helper"
2018-05-30 13:23:15 +02:00
require "system_command"
require_relative "whois/parser"
require_relative "whois/response"
require_relative "whois/errors"
2018-05-30 12:04:07 +02:00
module Whois
class << self
2018-05-30 13:23:15 +02:00
def ask(domain, system_klass: SystemCommand, logger: NullLogger.new)
Service.new(domain, system_klass, logger: logger).call
2018-05-30 12:04:07 +02:00
end
end
class Service
attr_reader :domain
attr_reader :logger
2018-05-30 13:23:15 +02:00
attr_reader :system_klass
2018-05-30 12:04:07 +02:00
2018-05-30 13:23:15 +02:00
def initialize(domain, system_klass: SystemCommand, logger: NullLogger.new)
2018-05-30 12:04:07 +02:00
@domain = domain
@logger = logger
2018-05-30 13:23:15 +02:00
@system_klass = system_klass
2018-05-30 12:04:07 +02:00
end
def call
2018-05-30 13:23:15 +02:00
result = run_command
parse(result)
2018-05-30 16:51:24 +02:00
rescue StandardError => ex
logger.log :service_error, ex
raise
2018-05-30 13:23:15 +02:00
end
2018-05-30 12:04:07 +02:00
2018-05-30 13:23:15 +02:00
def run_command
command = system_klass.new("whois", domain, logger: logger)
result = command.execute
unless result.exit_status.zero?
fail WhoisCommandError, "Whois command failed with status #{result.exit_status}"
end
2018-05-30 12:04:07 +02:00
2018-05-30 13:23:15 +02:00
result
end
def parse(result)
parser = Parser.for(domain, logger: logger)
parser.parse(result.stdout)
2018-05-30 12:04:07 +02:00
end
end
end