Class: HTMLProofer::Cache

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/html_proofer/cache.rb

Constant Summary collapse

CACHE_VERSION =
2
DEFAULT_STORAGE_DIR =
File.join("tmp", ".htmlproofer")
DEFAULT_CACHE_FILE_NAME =
"cache.json"
URI_REGEXP =
URI::DEFAULT_PARSER.make_regexp
SECONDS_PER_HOUR =
3600
SECONDS_PER_DAY =
86400
SECONDS_PER_WEEK =
604800
SECONDS_PER_MONTH =

1/12 of a gregorian year

2629746

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blank?, #create_nokogiri, #pluralize

Constructor Details

#initialize(runner, options) ⇒ Cache

Returns a new instance of Cache.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/html_proofer/cache.rb', line 20

def initialize(runner, options)
  @runner = runner
  @logger = @runner.logger

  @cache_datetime = Time.now
  @cache_time = @cache_datetime.to_time

  if blank?(options)
    define_singleton_method(:enabled?) { false }
    define_singleton_method(:external_enabled?) { false }
    define_singleton_method(:internal_enabled?) { false }
  else
    # we still consider the cache as enabled, regardless of the specic timeframes
    define_singleton_method(:enabled?) { true }
    setup_cache!(options)

    @external_timeframe = parsed_timeframe(options[:timeframe][:external])
    define_singleton_method(:external_enabled?) { !@external_timeframe.nil? }
    @internal_timeframe = parsed_timeframe(options[:timeframe][:internal])
    define_singleton_method(:internal_enabled?) { !@internal_timeframe.nil? }
  end
end

Instance Attribute Details

#cache_fileObject (readonly)

Returns the value of attribute cache_file.



18
19
20
# File 'lib/html_proofer/cache.rb', line 18

def cache_file
  @cache_file
end

#cache_logObject (readonly)

Returns the value of attribute cache_log.



18
19
20
# File 'lib/html_proofer/cache.rb', line 18

def cache_log
  @cache_log
end

#existsObject (readonly)

Returns the value of attribute exists.



18
19
20
# File 'lib/html_proofer/cache.rb', line 18

def exists
  @exists
end

#storage_dirObject (readonly)

Returns the value of attribute storage_dir.



18
19
20
# File 'lib/html_proofer/cache.rb', line 18

def storage_dir
  @storage_dir
end

Instance Method Details

#add_external(url, filenames, status_code, msg, found) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/html_proofer/cache.rb', line 70

def add_external(url, filenames, status_code, msg, found)
  return unless external_enabled?

  clean_url = cleaned_url(url)
  @cache_log[:external][clean_url] =
    { time: @cache_time.to_s, found: found, status_code: status_code, message: msg, metadata: filenames }
end

#add_internal(url, metadata, found) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/html_proofer/cache.rb', line 62

def add_internal(url, , found)
  return unless internal_enabled?

  @cache_log[:internal][url] = { time: @cache_time, metadata: [] } if @cache_log[:internal][url].nil?

  @cache_log[:internal][url][:metadata] << (, found)
end

#detect_url_changes(urls_detected, type) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/html_proofer/cache.rb', line 78

def detect_url_changes(urls_detected, type)
  determine_deletions(urls_detected, type)

  additions = determine_additions(urls_detected, type)

  additions
end

#empty?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/html_proofer/cache.rb', line 115

def empty?
  blank?(@cache_log) || (@cache_log[:internal].empty? && @cache_log[:external].empty?)
end

#parsed_timeframe(timeframe) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/html_proofer/cache.rb', line 43

def parsed_timeframe(timeframe)
  return if timeframe.nil?

  time, date = timeframe.match(/(\d+)(\D)/).captures
  time = time.to_i
  case date
  when "M"
    time_ago(time, :months)
  when "w"
    time_ago(time, :weeks)
  when "d"
    time_ago(time, :days)
  when "h"
    time_ago(time, :hours)
  else
    raise ArgumentError, "#{date} is not a valid timeframe!"
  end
end

#retrieve_urls(urls_detected, type) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/html_proofer/cache.rb', line 92

def retrieve_urls(urls_detected, type)
  # if there are no urls, bail
  return {} if urls_detected.empty?

  if type == :external
    urls_detected = urls_detected.transform_keys do |url|
      cleaned_url(url)
    end
  end

  urls_to_check = detect_url_changes(urls_detected, type)

  urls_to_check
end

#size(type) ⇒ Object



119
120
121
# File 'lib/html_proofer/cache.rb', line 119

def size(type)
  @cache_log[type].size
end

#within_external_timeframe?(time) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/html_proofer/cache.rb', line 107

def within_external_timeframe?(time)
  within_timeframe?(time, @external_timeframe)
end

#within_internal_timeframe?(time) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/html_proofer/cache.rb', line 111

def within_internal_timeframe?(time)
  within_timeframe?(time, @internal_timeframe)
end

#writeObject



86
87
88
89
90
# File 'lib/html_proofer/cache.rb', line 86

def write
  return unless enabled?

  File.write(@cache_file, @cache_log.to_json)
end