<commit_before>require 'action_controller'
require 'rqrcode'
require 'rqrcode-rails3/size_calculator.rb'
require 'rqrcode-rails3/renderers/svg.rb'

module RQRCode
  Mime::Type.register "image/svg+xml", :svg  unless Mime::Type.lookup_by_extension(:svg)
  Mime::Type.register "image/png",     :png  unless Mime::Type.lookup_by_extension(:png)
	Mime::Type.register "image/jpeg",    :jpeg unless Mime::Type.lookup_by_extension(:jpeg)
  Mime::Type.register "image/gif",     :gif  unless Mime::Type.lookup_by_extension(:gif)

  extend SizeCalculator

  ActionController::Renderers.add :qrcode do |string, options|
    format = self.request.format.symbol
    size   = options[:size]  || RQRCode.minimum_qr_size_from_string(string)
    level  = options[:level] || :h

    qrcode = RQRCode::QRCode.new(string, :size => size, :level => level)
    svg    = RQRCode::Renderers::SVG::render(qrcode, options)

    data = \
    if format && format == :svg
      svg
    else
      image = MiniMagick::Image.read(svg) { |i| i.format "svg" }
      image.format format
      image.to_blob
    end

    self.response_body = render_to_string(:text => data, :template => nil)
  end
end<commit_msg>Refactor render code into callable module function<commit_after>require 'action_controller'
require 'rqrcode'
require 'rqrcode-rails3/size_calculator.rb'
require 'rqrcode-rails3/renderers/svg.rb'

module RQRCode
  Mime::Type.register "image/svg+xml", :svg  unless Mime::Type.lookup_by_extension(:svg)
  Mime::Type.register "image/png",     :png  unless Mime::Type.lookup_by_extension(:png)
	Mime::Type.register "image/jpeg",    :jpeg unless Mime::Type.lookup_by_extension(:jpeg)
  Mime::Type.register "image/gif",     :gif  unless Mime::Type.lookup_by_extension(:gif)

  extend SizeCalculator


  def render_qrcode(string, format, options)
    size   = options[:size]  || RQRCode.minimum_qr_size_from_string(string)
    level  = options[:level] || :h

    qrcode = RQRCode::QRCode.new(string, :size => size, :level => level)
    svg    = RQRCode::Renderers::SVG::render(qrcode, options)

    if format && format == :svg
      svg
    else
      image = MiniMagick::Image.read(svg) { |i| i.format "svg" }
      image.format format
      image.to_blob
    end
  end

  module_function :render_qrcode

  ActionController::Renderers.add :qrcode do |string, options|
    format = self.request.format.symbol
    data = RQRCode.render_qrcode(string, format, options)
    self.response_body = render_to_string(:text => data, :template => nil)
  end
end
{'fraction_non_alphanumeric': 0.12976190476190477, 'fraction_numerical': 0.0015873015873015873, 'mean_word_length': 4.208677685950414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3788387', 'n_tokens_mistral': 896, 'n_tokens_neox': 845, 'n_words': 169}