class Zine::Server

Local preview web server

Public Class Methods

new(rel_path_build, upload_options, delete_array, upload_array) click to toggle source

Create a new instance of Server, used to preview the built site locally

Attributes

  • rel_path_build - string, relative path to the build folder

  • upload_options - hash created from the upload section of zine.yaml

  • delete_array - array of path strings of files to delete

  • upload_array - ditto for files to upload, both can include duplicates

# File lib/zine/server.rb, line 19
def initialize(rel_path_build, upload_options, delete_array, upload_array)
  @delete_array = delete_array
  @upload_array = upload_array
  @thin = create_server File.absolute_path(rel_path_build)
  motd unless upload_options['test']
  @thin.start
  possible_upload rel_path_build, upload_options
  @thin
end

Public Instance Methods

stop() click to toggle source

Stop the server - only used in test

# File lib/zine/server.rb, line 30
def stop
  @thin.stop
end

Private Instance Methods

create_server(root) click to toggle source
# File lib/zine/server.rb, line 36
def create_server(root)
  rules = header_rules
  Thin::Logging.silent = true
  Thin::Server.new('127.0.0.1', 8080) do
    use Rack::Static,
        urls: ['/'],
        index: 'index.html',
        root: root,
        header_rules: rules
    run ->(_env) { [200, {}, ['Have to call run...']] }
  end
end
header_rules() click to toggle source
# File lib/zine/server.rb, line 49
def header_rules
  [[:all,
    { 'ETag'          => nil,
      'Last-Modified' => (Time.now + 100**4).to_s,
      'Cache-Control' =>
        'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
      'Pragma'        => 'no-cache',
      'Expires'       => (Time.now - 100**4).to_s }]]
end
motd() click to toggle source
# File lib/zine/server.rb, line 59
def motd
  puts "\nPreview running on " +
       Rainbow('http://127.0.0.1:8080/').blue.underline +
       "\nCommand double click the URL to open, Control C to quit\n"
end
possible_upload(rel_path_build, upload_options) click to toggle source
# File lib/zine/server.rb, line 65
def possible_upload(rel_path_build, upload_options)
  return if upload_options['method'] == 'none' ||
            (@delete_array.empty? && @upload_array.empty?)
  uploader = Zine::Upload.new rel_path_build, upload_options,
                              @delete_array, @upload_array
  uploader.upload_decision Query
end