Class: Inkcite::Cli::Server::InkciteApp

Inherits:
Object
  • Object
show all
Defined in:
lib/inkcite/cli/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(email, opts) ⇒ InkciteApp



93
94
95
96
# File 'lib/inkcite/cli/server.rb', line 93

def initialize email, opts
  @email = email
  @opts = opts
end

Instance Method Details

#call(env) ⇒ Object



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
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/inkcite/cli/server.rb', line 98

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'

  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])

    # Timestamp all of the messages from this rendering so it is clear which
    # messages are associated with this reload.
    ts = "[#{Time.now.strftime(DATEFORMAT)}]"

    puts ''
    puts "#{ts} Rendering your email [environment=#{environment}, format=#{format}, version=#{version || 'default'}]"

    # Before the rendering takes place, trigger image optimization of any
    # new or updated images.  The {image} tag takes care of injecting the
    # right path (optimized or not) depending on which version is needed.
    @email.optimize_images

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

    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
      puts "#{ts} #{error_count} error#{'s' if error_count > 1} or warning#{'s' if error_count > 1}:"
      puts "#{ts} - #{view.errors.join("\n#{ts} - ")}"
    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