class Inkcite::Cli::Server::InkciteApp

Public Class Methods

new(email, opts) click to toggle source
# File lib/inkcite/cli/server.rb, line 128
def initialize email, opts
  @email = email
  @opts = opts
end

Public Instance Methods

call(env) click to toggle source
# File lib/inkcite/cli/server.rb, line 133
def call env

  request = Rack::Request.new(env)

  # If this request is for the root index page, render the email.  Otherwise
  # render the static asset.
  return if request.path_info != REQUEST_ROOT

  response = Rack::Response.new
  response[Rack::CONTENT_TYPE] = 'text/html'

  # Speedup for local development ensuring that a substantial number of reloads
  # (generated when developing multi-version emails simultaneously) can cause
  # the browser to slow down.
  response[Rack::CACHE_CONTROL] = NO_CACHE

  begin

    # Allow the designer to specify both short- and long-form versions of
    # the (e)nvironment, (f)ormat and (v)ersion.  Otherwise, use the values
    # that Inkcite was started with.
    params = request.params
    environment = Util.detect(params['e'], params['environment'], @opts[:environment])
    format = Util.detect(params['f'], params['format'], @opts[:format])
    version = Util.detect(params['v'], params['version'], @opts[:version])

    Util.log "Rendering your email", :environment => environment, :format => format, :version => version || 'default'

    view = @email.view(environment, format, version)

    # Check to see if images have been disabled in this rendering.
    view.images_off = true if params.key?('images-off')

    # Install the developer toolkit into the view.
    Inkcite::View::DeveloperScripts.install!(view)

    html = view.render!

    # If minification is disabled, then beautify the output to make it easier
    # for the designer to inspect the code being produced by Inkcite.
    html = HtmlBeautifier.beautify(html) unless view.is_enabled?(:minify)

    unless view.errors.blank?
      error_count = view.errors.count
      Util.log "#{error_count} error#{'s' if error_count > 1} or warning#{'s' if error_count > 1}:"
      view.errors.each { |e| Util.log(e) }
    end

    response.write html

  rescue Exception => e
    response.write "<html><head><title>Oops! There was a problem!</title></head><body style='padding: 30px; font-family: sans-serif;'>"
    response.write '<h2>Oops!</h2>'
    response.write "<p>There was a problem rendering your email: #{e.message}</p>"
    response.write "<pre>#{e.backtrace.join('<br>')}</pre>"
    response.write 'Please correct the problem and try reloading the page.'
    response.write '</body></html>'
  end

  response.finish

end