Skip to content

Commit

Permalink
Merge pull request #139 from edgi-govdata-archiving/add-html-diffs-be…
Browse files Browse the repository at this point in the history
…cause-they-are-only-the-most-important-kind

Add visual HTML diffs
  • Loading branch information
Mr0grog committed Sep 19, 2017
2 parents 875da83 + e810c6c commit bfc2932
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export DIFFER_PAGEFREEZER=http://localhost:8888/pagefreezer
export DIFFER_HTML_SOURCE=http://localhost:8888/html_source_diff
export DIFFER_HTML_TEXT=http://localhost:8888/html_text_diff
export DIFFER_SIDE_BY_SIDE_TEXT=http://localhost:8888/side_by_side_text
export DIFFER_SIDE_BY_SIDE_TEXT=http://localhost:8888/html_visual_diff

# Set a private RSA key to use for signing auth tokens
# This MUST be replaced with a unique value in production!
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api/v0/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Api::V0::ApiController < ApplicationController
rescue_from StandardError, with: :render_errors if Rails.env.production? || Rails.env.test?
rescue_from Api::NotImplementedError, with: :render_errors
rescue_from Api::InputError, with: :render_errors
rescue_from Api::DynamicError, with: :render_errors

rescue_from ActiveRecord::RecordInvalid, with: :render_errors
rescue_from ActiveModel::ValidationError do |error|
Expand Down
46 changes: 18 additions & 28 deletions config/initializers/differ.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
require_dependency 'differ/differ'
require_dependency 'differ/simple_diff'

if ENV['DIFFER_SOURCE']
Differ.register(:source, Differ::SimpleDiff.new(ENV['DIFFER_SOURCE']))
end

if ENV['DIFFER_LENGTH']
Differ.register(:length, Differ::SimpleDiff.new(ENV['DIFFER_LENGTH']))
end

if ENV['DIFFER_IDENTICAL_BYTES']
Differ.register(:identical_bytes,
Differ::SimpleDiff.new(ENV['DIFFER_IDENTICAL_BYTES']))
end

if ENV['DIFFER_SIDE_BY_SIDE_TEXT']
Differ.register(:side_by_side_text,
Differ::SimpleDiff.new(ENV['DIFFER_SIDE_BY_SIDE_TEXT']))
end

if ENV['DIFFER_PAGEFREEZER']
Differ.register(:pagefreezer, Differ::SimpleDiff.new(ENV['DIFFER_PAGEFREEZER']))
end

if ENV['DIFFER_HTML_SOURCE']
Differ.register(:html_source, Differ::SimpleDiff.new(ENV['DIFFER_HTML_SOURCE']))
end

if ENV['DIFFER_HTML_TEXT']
Differ.register(:html_text, Differ::SimpleDiff.new(ENV['DIFFER_HTML_TEXT']))
# Automatically create SimpleDiff instances with the name of whatever is after
# "DIFFER_" in the name of the following env vars.
[
'DIFFER_SOURCE',
'DIFFER_LENGTH',
'DIFFER_IDENTICAL_BYTES',
'DIFFER_SIDE_BY_SIDE_TEXT',
'DIFFER_PAGEFREEZER',
'DIFFER_HTML_SOURCE',
'DIFFER_HTML_TEXT',
'DIFFER_HTML_VISUAL'
].each do |env_var|
if ENV[env_var]
Differ.register(
env_var.gsub(/^DIFFER_/, '').downcase.to_sym,
Differ::SimpleDiff.new(ENV[env_var])
)
end
end
9 changes: 9 additions & 0 deletions lib/api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ def status_code
400
end
end

class DynamicError < StandardError
attr_accessor :status_code

def initialize(message, status_code)
@status_code = status_code
super(message)
end
end
end
14 changes: 9 additions & 5 deletions lib/differ/simple_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ def diff(change, options = nil)

# TODO: get our simple differ to return correct Content-Type header
# and remove check for magical 'format' query arg
if response.request.format == :json || options['format'] == 'json'
JSON.parse(response.body)
else
response.body
end
body =
if response.request.format == :json || options['format'] == 'json'
JSON.parse(response.body)
else
response.body
end

raise Api::DynamicError.new(body, response.code) if response.code >= 400
body
end
end
end

0 comments on commit bfc2932

Please sign in to comment.