class Aquatone::Collector

Constants

DEFAULT_PRIORITY

Attributes

domain[R]
hosts[R]

Public Class Methods

cli_options() click to toggle source
# File lib/aquatone/collector.rb, line 26
def self.cli_options
  meta.key?(:cli_options) ? meta[:cli_options] : {}
end
descendants() click to toggle source
# File lib/aquatone/collector.rb, line 21
def self.descendants
  collectors = ObjectSpace.each_object(Class).select { |klass| klass < self }
  collectors.sort { |x, y| x.priority <=> y.priority }
end
meta() click to toggle source
# File lib/aquatone/collector.rb, line 12
def self.meta
  @meta || fail(MetadataNotSetError, "Metadata has not been set")
end
meta=(meta) click to toggle source
# File lib/aquatone/collector.rb, line 16
def self.meta=(meta)
  validate_metadata(meta)
  @meta = meta
end
new(domain, options) click to toggle source
# File lib/aquatone/collector.rb, line 35
def initialize(domain, options)
  check_key_requirements!
  @domain          = domain
  @options         = options
  @hosts           = []
  @host_dictionary = {}
end
priority() click to toggle source
# File lib/aquatone/collector.rb, line 52
def self.priority
  meta[:priority] || DEFAULT_PRIORITY
end
sluggified_name() click to toggle source
# File lib/aquatone/collector.rb, line 30
def self.sluggified_name
  return meta[:slug].downcase if meta[:slug]
  meta[:name].strip.downcase.gsub(/[^a-z0-9]+/, '-').gsub("--", "-")
end

Protected Class Methods

validate_metadata(meta) click to toggle source
# File lib/aquatone/collector.rb, line 118
def self.validate_metadata(meta)
  fail InvalidMetadataError, "Metadata is not a hash" unless meta.is_a?(Hash)
  fail InvalidMetadataError, "Metadata is empty" if meta.empty?
  fail InvalidMetadataError, "Metadata is missing key: name" unless meta.key?(:name)
  fail InvalidMetadataError, "Metadata is missing key: author" unless meta.key?(:author)
  fail InvalidMetadataError, "Metadata is missing key: description" unless meta.key?(:description)
  if meta.key?(:cli_options)
    fail InvalidMetadataError, "Metadata CLI options is not a hash" unless meta[:cli_options].is_a?(Hash)
    meta[:cli_options].each_pair do |option, description|
      fail InvalidMetadataError, "CLI option name is not a string" unless option.is_a?(String)
      fail InvalidMetadataError, "CLI option details is not a string" unless description.is_a?(String)
    end
  end
end

Public Instance Methods

execute!() click to toggle source
# File lib/aquatone/collector.rb, line 47
def execute!
  run
  hosts
end
run() click to toggle source
# File lib/aquatone/collector.rb, line 43
def run
  fail NotImplementedError
end

Protected Instance Methods

add_host(host) click to toggle source
# File lib/aquatone/collector.rb, line 58
def add_host(host)
  host.downcase!
  return unless Aquatone::Validation.valid_domain_name?(host)
  return if @host_dictionary.key?(host)
  @host_dictionary[host] = true
  @hosts << host
end
check_key_requirements!() click to toggle source
# File lib/aquatone/collector.rb, line 110
def check_key_requirements!
  return unless self.class.meta[:require_keys]
  keys = self.class.meta[:require_keys]
  keys.each do |key|
    fail MissingKeyRequirement, "Key '#{key}' has not been set" unless has_key?(key)
  end
end
failure(message) click to toggle source
# File lib/aquatone/collector.rb, line 106
def failure(message)
  fail Error, message
end
get_cli_option(name) click to toggle source
# File lib/aquatone/collector.rb, line 98
def get_cli_option(name)
  @options[name.to_s.gsub("-", "_").to_sym]
end
get_key(name) click to toggle source
# File lib/aquatone/collector.rb, line 90
def get_key(name)
  Aquatone::KeyStore.get(name)
end
get_request(uri, options={}) click to toggle source
# File lib/aquatone/collector.rb, line 66
def get_request(uri, options={})
  Aquatone::HttpClient.get(uri, options)
end
has_cli_option?(name) click to toggle source
# File lib/aquatone/collector.rb, line 102
def has_cli_option?(name)
  @options.key?(name.to_s.gsub("-", "_").to_sym)
end
has_key?(name) click to toggle source
# File lib/aquatone/collector.rb, line 94
def has_key?(name)
  Aquatone::KeyStore.key?(name)
end
post_request(uri, body=nil, options={}) click to toggle source
# File lib/aquatone/collector.rb, line 70
def post_request(uri, body=nil, options={})
  options = {
    :body => body
  }.merge(options)
  Aquatone::HttpClient.post(uri, options)
end
random_sleep(seconds) click to toggle source
# File lib/aquatone/collector.rb, line 85
def random_sleep(seconds)
  random_sleep = ((1 - (rand(30) * 0.01)) * seconds.to_i)
  sleep(random_sleep)
end
regex_escape(string) click to toggle source
# File lib/aquatone/collector.rb, line 81
def regex_escape(string)
  Regexp.escape(string)
end
url_escape(string) click to toggle source
# File lib/aquatone/collector.rb, line 77
def url_escape(string)
  CGI.escape(string)
end