Skip to content

Instantly share code, notes, and snippets.

@pythonandchips
Created December 4, 2019 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythonandchips/7f7857d902d3e5ab99c1a8359879798a to your computer and use it in GitHub Desktop.
Save pythonandchips/7f7857d902d3e5ab99c1a8359879798a to your computer and use it in GitHub Desktop.
Compare recurring invoice html output
class CompareRecurringInvoiceProfiles
def self.perform
recurring_invoice_count = RecurringInvoiceProfile.count
progress = ProgressBar.new(recurring_invoice_count)
RecurringInvoiceProfile.all.find_each do |recurring_invoice_profile|
render_and_compare(recurring_invoice_profile.source_recurring_invoice, recurring_invoice_profile)
progress.increment!
end
end
def self.render_and_compare(recurring_invoice, recurring_invoice_profile)
instance = setup_instance(recurring_invoice: recurring_invoice, enabled: false)
html_feature_switch_off = instance.render_to_string(
"show", layout: false
).gsub(/\s/, "")
instance = setup_instance(recurring_invoice: recurring_invoice_profile, enabled: true)
html_feature_switch_on = instance.render_to_string(
"show", layout: false
).gsub(/\s/, "")
diff_strings(recurring_invoice.id, html_feature_switch_off, html_feature_switch_on)
end
def self.setup_instance(recurring_invoice:, enabled: false)
request = ActionDispatch::Request.new(
"HTTP_HOST"=>"example.org",
"HTTPS"=>"on",
"REQUEST_METHOD"=>"GET",
"SCRIPT_NAME"=>"",
"Content-Type" => "application/json",
"Accept" => "application/json",
"rack.input"=>"",
"rack.url_scheme"=>"https"
)
request.routes = RecurringInvoicesController._routes
instance = RecurringInvoicesController.new
instance.instance_variable_set(:@current_company, recurring_invoice.company)
instance.instance_variable_set(:@recurring_invoice_profile_read, enabled)
instance.instance_variable_set(:@invoice, recurring_invoice)
instance.set_request! request
instance.set_response! RecurringInvoicesController.make_response!(request)
instance
end
def self.diff_strings(id, html_feature_switch_off, html_feature_switch_on)
return if html_feature_switch_off == html_feature_switch_on
on_chars = html_feature_switch_on.chars
off_chars = html_feature_switch_off.chars
diff_start = -1
on_chars.each_with_index do |c, i|
if c != off_chars[i] && diff_start == -1
diff_start = i
end
end
Rails.logger.info({
log_tag: "RECURRING INVOICE COMPAIRE FAILURE",
id: id,
charater_failed_at: diff_start,
feature_on: on_chars[diff_start-30..diff_start+30].join("") ,
feature_off: off_chars[diff_start-30..diff_start+30].join("")
}.to_json)
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment