Class: Wraith::Validate

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/wraith/validate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(config, options = {}) ⇒ Validate

Returns a new instance of Validate.



9
10
11
# File 'lib/wraith/validate.rb', line 9

def initialize(config, options = {})
  @wraith = Wraith::Wraith.new(config, options)
end

Instance Attribute Details

#wraithObject (readonly)

Returns the value of attribute wraith.



7
8
9
# File 'lib/wraith/validate.rb', line 7

def wraith
  @wraith
end

Instance Method Details

#docs_promptObject



75
76
77
# File 'lib/wraith/validate.rb', line 75

def docs_prompt
  "See the docs at http://bbc-news.github.io/wraith/"
end

#validate(mode = false) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/wraith/validate.rb', line 13

def validate(mode = false)
  list_debug_information if wraith.verbose
  validate_basic_properties
  validate_mode_properties(mode) if mode
  # if we get this far, we've only had warnings at worst, not errors.
  "Config validated. No serious issues found."
end

#validate_base_shots_existObject



69
70
71
72
73
# File 'lib/wraith/validate.rb', line 69

def validate_base_shots_exist
  unless File.directory?(wraith.history_dir)
    logger.error "You need to run `wraith history` at least once before you can run `wraith latest`!"
  end
end

#validate_basic_propertiesObject



21
22
23
24
25
26
27
28
29
# File 'lib/wraith/validate.rb', line 21

def validate_basic_properties
  fail MissingRequiredPropertyError, "You must specify a browser engine! #{docs_prompt}" if wraith.engine.nil?

  fail MissingRequiredPropertyError, "You must specify at least one domain for Wraith to do anything! #{docs_prompt}" unless wraith.domains

  fail MissingRequiredPropertyError, "You must specify a directory for capture! #{docs_prompt}" if wraith.directory.nil?

  # @TODO validate fuzz is not nil, etc
end

#validate_capture_modeObject



47
48
49
50
51
52
# File 'lib/wraith/validate.rb', line 47

def validate_capture_mode
  fail InvalidDomainsError, "`wraith capture` requires exactly two domains. #{docs_prompt}" if wraith.domains.length != 2

  logger.warn "You have specified a `history_dir` in your config, but this is"\
              " used in `history` mode, NOT `capture` mode. #{docs_prompt}" if wraith.history_dir
end

#validate_history_modeObject



54
55
56
57
58
59
# File 'lib/wraith/validate.rb', line 54

def validate_history_mode
  fail MissingRequiredPropertyError, "You must specify a `history_dir` to run"\
                " Wraith in history mode. #{docs_prompt}" unless wraith.history_dir

  fail InvalidDomainsError, "History mode requires exactly one domain. #{docs_prompt}" if wraith.domains.length != 1
end

#validate_mode_properties(mode) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wraith/validate.rb', line 31

def validate_mode_properties(mode)
  case mode
  when "capture"
    validate_capture_mode
  when "history"
    validate_history_mode
  when "latest"
    validate_history_mode
    validate_base_shots_exist
  when "spider"
    validate_spider_mode
  else
    logger.warn "Wraith doesn't know how to validate mode '#{mode}'. Continuing..."
  end
end

#validate_spider_modeObject



61
62
63
64
65
66
67
# File 'lib/wraith/validate.rb', line 61

def validate_spider_mode
  fail MissingRequiredPropertyError, "You must specify an `imports` YML"\
                " before running `wraith spider`. #{docs_prompt}" unless wraith.imports

  #fail PropertyOutOfContextError, "Tried running `wraith spider` but you have already"\
  #                              " specified paths in your YML. #{docs_prompt}" if wraith.paths
end