Class: SiteDiff::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/sitediff/api.rb

Overview

Sitediff API interface.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, config_file = nil) ⇒ Api

Initializes new Api object.



17
18
19
20
# File 'lib/sitediff/api.rb', line 17

def initialize(directory, config_file = nil)
  @dir = get_dir(directory)
  @config = SiteDiff::Config.new(config_file, @dir)
end

Class Method Details

.init(options) ⇒ Object

Intialize a SiteDiff project.

Calling:

SiteDiff::Api.init(
  depth: 3,
  directory: 'sitediff',
  concurrency: 3,
  interval: 0,
  include: nil,
  exclude: '*.pdf',
  preset: 'drupal',
  curl_opts: {timeout: 60},
  crawl: false
)


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(options)
  # Prepare a config object and write it to the file system.
  creator = SiteDiff::Config::Creator.new(options[:debug], options[:before_url], options[:after_url])
  include_regex = Config.create_regexp(options[:include])
  exclude_regex = Config.create_regexp(options[:exclude])
  creator.create(
    depth: options[:depth],
    directory: options[:directory],
    concurrency: options[:concurrency],
    interval: options[:interval],
    include: include_regex,
    exclude: exclude_regex,
    preset: options[:preset],
    curl_opts: options[:curl_opts]
  )
  SiteDiff.log "Created #{creator.config_file.expand_path}", :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

#crawlObject

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.write_tags << :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.expand_path}.", :success, 'done'
end

#diff(options) ⇒ Object

Diff the ‘before` and `after`.

Calling:

Api.diff(
  paths: options['paths'],
  paths_file: options['paths-file'],
  ignore_whitespace: options['ignore-whitespace'],
  export: options['export'],
  before: options['before'],
  after: options['after'],
  cached: options['cached'],
  verbose: options['verbose'],
  report_format: options['report-format'],
  before_report: options['before-report'],
  after_report: options['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(options)
  @config.ignore_whitespace = options[:ignore_whitespace]
  @config.export = options[:export]
  @config.remove_html_comments = options[:remove_html_comments]

  # Apply "paths" override, if any.
  if options[:paths]
    @config.paths = options[:paths]
  else
    paths_file = options[:paths_file]
    paths_file ||= File.join(@dir, Config::DEFAULT_PATHS_FILENAME)
    paths_file = File.expand_path(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'] = options[:before] if options[:before]
  @config.after['url'] = options[:after] if options[:after]

  # Prepare cache.
  cache = SiteDiff::Cache.new(
    create: options[:cached] != 'none',
    directory: @dir
  )
  cache.read_tags << :before if %w[before all].include?(options[:cached])
  cache.read_tags << :after if %w[after all].include?(options[:cached])
  cache.write_tags << :before << :after

  # Run sitediff.
  sitediff = SiteDiff.new(
    @config,
    cache,
    verbose: options[:verbose],
    debug: options[:debug]
  )
  num_failing = sitediff.run
  exit_code = num_failing.positive? ? 2 : 0

  # Generate HTML report.
  if options[:report_format] == 'html' || @config.export
    sitediff.report.generate_html(
      @dir,
      options[:before_report],
      options[:after_report]
    )
  end

  # Generate JSON report.
  if options[:report_format] == 'json' && @config.export == false
    sitediff.report.generate_json @dir
  end

  SiteDiff.log 'Run "sitediff serve" to see a report.' unless options[:export]
rescue Config::InvalidConfig => e
  SiteDiff.log "Invalid configuration: #{e.message}", :error
  SiteDiff.log e.backtrace, :error if options[:verbose]
rescue Config::ConfigNotFound => e
  SiteDiff.log "Invalid configuration: #{e.message}", :error
  SiteDiff.log e.backtrace, :error if options[: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 options[: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(options)
  @cache = Cache.new(directory: @dir)
  @cache.read_tags << :before << :after

  SiteDiff::Webserver::ResultServer.new(
    options[:port],
    @dir,
    browse: options[:browse],
    cache: @cache,
    config: @config
  ).wait
rescue SiteDiffException => e
  SiteDiff.log e.message, :error
  SiteDiff.log e.backtrace, :error if options[: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(options)
  # 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.write_tags << :before

  base = options[:url] || @config.after['url']
  fetcher = SiteDiff::Fetch.new(@cache,
                                @config.paths,
                                @config.setting(:interval),
                                @config.setting(:concurrency),
                                get_curl_opts(@config.settings),
                                debug: options[:debug],
                                before: base)
  fetcher.run do |path, _res|
    SiteDiff.log "Visited #{path}, cached"
  end
end