Class: SiteDiff::Api
- Inherits:
-
Object
- Object
- SiteDiff::Api
- Defined in:
- lib/sitediff/api.rb
Overview
Sitediff API interface.
Class Method Summary collapse
-
.init(options) ⇒ Object
Intialize a SiteDiff project.
Instance Method Summary collapse
-
#crawl ⇒ Object
Crawl the ‘before` site to determine `paths`.
-
#diff(options) ⇒ Object
Diff the ‘before` and `after`.
-
#initialize(directory, config_file = nil) ⇒ Api
constructor
Initializes new Api object.
-
#serve(options) ⇒ Object
Serves SiteDiff report for accessing in the browser.
- #store(options) ⇒ Object
Constructor Details
Class Method Details
.init(options) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sitediff/api.rb', line 37 def self.init() # Prepare a config object and write it to the file system. creator = SiteDiff::Config::Creator.new([:debug], [:before_url], [:after_url]) include_regex = Config.create_regexp([:include]) exclude_regex = Config.create_regexp([:exclude]) creator.create( depth: [:depth], directory: [:directory], concurrency: [:concurrency], interval: [:interval], include: include_regex, exclude: exclude_regex, preset: [:preset], curl_opts: [:curl_opts] ) SiteDiff.log "Created #{creator.config_file.}", :success # TODO: implement crawl ^^^ # Discover paths, if enabled. # if options[:crawl] # crawl(creator.config_file) # SiteDiff.log 'You can now run "sitediff diff".', :success # else # SiteDiff.log 'Run "sitediff crawl" to discover paths. You should then be able to run "sitediff diff".', :info # end end |
Instance Method Details
#crawl ⇒ Object
Crawl the ‘before` site to determine `paths`.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/sitediff/api.rb', line 151 def crawl # Prepare cache. @cache = SiteDiff::Cache.new( create: true, directory: @dir ) @cache. << :before << :after # Crawl with Hydra to discover paths. hydra = Typhoeus::Hydra.new( max_concurrency: @config.setting(:concurrency) ) @paths = {} ignore_after = @config.roots if @config.roots['before'] == @config.roots['after'] ignore_after.delete('after') end ignore_after.each do |tag, url| Crawler.new( hydra, url, @config.setting(:interval), @config.setting(:include), @config.setting(:exclude), @config.setting(:depth), @config.curl_opts, debug: @debug ) do |info| SiteDiff.log "Visited #{info.uri}, cached." after_crawl(tag, info) end end hydra.run # Write paths to a file. @paths = @paths.values.reduce(&:|).to_a.sort if @paths.none? | @paths.nil? return end @config.paths_file_write(@paths) # Log output. file = Pathname.new(@dir) + Config::DEFAULT_PATHS_FILENAME SiteDiff.log '' SiteDiff.log "#{@paths.length} page(s) found." SiteDiff.log "Created #{file.}.", :success, 'done' end |
#diff(options) ⇒ Object
Diff the ‘before` and `after`.
Calling:
Api.diff(
paths: ['paths'],
paths_file: ['paths-file'],
ignore_whitespace: ['ignore-whitespace'],
export: ['export'],
before: ['before'],
after: ['after'],
cached: ['cached'],
verbose: ['verbose'],
report_format: ['report-format'],
before_report: ['before-report'],
after_report: ['after-report'],
cli_mode: false
)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/sitediff/api.rb', line 82 def diff() @config.ignore_whitespace = [:ignore_whitespace] @config.export = [:export] @config.remove_html_comments = [:remove_html_comments] # Apply "paths" override, if any. if [:paths] @config.paths = [:paths] else paths_file = [:paths_file] paths_file ||= File.join(@dir, Config::DEFAULT_PATHS_FILENAME) paths_file = File.(paths_file) paths_count = @config.paths_file_read(paths_file) SiteDiff.log "Read #{paths_count} paths from: #{paths_file}" end # TODO: Why do we allow before and after override during diff? @config.before['url'] = [:before] if [:before] @config.after['url'] = [:after] if [:after] # Prepare cache. cache = SiteDiff::Cache.new( create: [:cached] != 'none', directory: @dir ) cache. << :before if %w[before all].include?([:cached]) cache. << :after if %w[after all].include?([:cached]) cache. << :before << :after # Run sitediff. sitediff = SiteDiff.new( @config, cache, verbose: [:verbose], debug: [:debug] ) num_failing = sitediff.run exit_code = num_failing.positive? ? 2 : 0 # Generate HTML report. if [:report_format] == 'html' || @config.export sitediff.report.generate_html( @dir, [:before_report], [:after_report] ) end # Generate JSON report. if [:report_format] == 'json' && @config.export == false sitediff.report.generate_json @dir end SiteDiff.log 'Run "sitediff serve" to see a report.' unless [:export] rescue Config::InvalidConfig => e SiteDiff.log "Invalid configuration: #{e.}", :error SiteDiff.log e.backtrace, :error if [:verbose] rescue Config::ConfigNotFound => e SiteDiff.log "Invalid configuration: #{e.}", :error SiteDiff.log e.backtrace, :error if [:verbose] else # no exception was raised # Thor::Error --> exit(1), guaranteed by exit_on_failure? # Failing diff --> exit(2), populated above exit(exit_code) if [:cli_mode] end |
#serve(options) ⇒ Object
Serves SiteDiff report for accessing in the browser.
Calling:
api.serve(browse: true, port: 13080)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/sitediff/api.rb', line 207 def serve() @cache = Cache.new(directory: @dir) @cache. << :before << :after SiteDiff::Webserver::ResultServer.new( [:port], @dir, browse: [:browse], cache: @cache, config: @config ).wait rescue SiteDiffException => e SiteDiff.log e., :error SiteDiff.log e.backtrace, :error if [:verbose] end |
#store(options) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/sitediff/api.rb', line 225 def store() # TODO: Figure out how to remove this config.validate call. @config.validate(need_before: false) @config.paths_file_read @cache = SiteDiff::Cache.new(directory: @dir, create: true) @cache. << :before base = [:url] || @config.after['url'] fetcher = SiteDiff::Fetch.new(@cache, @config.paths, @config.setting(:interval), @config.setting(:concurrency), get_curl_opts(@config.settings), debug: [:debug], before: base) fetcher.run do |path, _res| SiteDiff.log "Visited #{path}, cached" end end |