Class: SiteDiff::Webserver::ResultServer

Inherits:
SiteDiff::Webserver show all
Defined in:
lib/sitediff/webserver/resultserver.rb

Overview

SiteDiff Result Server.

Defined Under Namespace

Classes: CacheServlet, SideBySideServlet

Constant Summary

Constants inherited from SiteDiff::Webserver

DEFAULT_PORT

Instance Attribute Summary

Attributes inherited from SiteDiff::Webserver

#ports

Instance Method Summary collapse

Methods inherited from SiteDiff::Webserver

#kill, #uris, #wait

Constructor Details

#initialize(port, dir, opts = {}) ⇒ ResultServer

Creates a Result Server.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sitediff/webserver/resultserver.rb', line 74

def initialize(port, dir, opts = {})
  unless File.exist?(File.join(dir, Report::SETTINGS_FILE))
    raise SiteDiffException,
          "Please run 'sitediff diff' before running 'sitediff serve'"
  end

  @settings = YAML.load_file(File.join(dir, Report::SETTINGS_FILE))
  puts @settings
  @cache = opts[:cache]
  super(port, [dir], opts)
end

Instance Method Details

#open_in_browser(url) ⇒ Object

Opens a URL in a browser.



118
119
120
121
122
123
# File 'lib/sitediff/webserver/resultserver.rb', line 118

def open_in_browser(url)
  commands = %w[xdg-open open]
  cmd = commands.find { |c| which(c) }
  system(cmd, url) if cmd
  cmd
end

#server(opts) ⇒ Object

TODO: Document what this method does.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sitediff/webserver/resultserver.rb', line 88

def server(opts)
  dir = opts.delete(:DocumentRoot)
  srv = super(opts)
  srv.mount_proc('/') do |req, res|
    if req.path == '/'
      res.set_redirect(WEBrick::HTTPStatus::Found,
                       "/files/#{Report::REPORT_FILE_HTML}")
    else
      res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect,
                       "#{@settings['after']}#{req.path}")
    end
  end

  srv.mount('/files', WEBrick::HTTPServlet::FileHandler, dir, true)
  srv.mount('/cache', CacheServlet, @cache)
  srv.mount('/sidebyside', SideBySideServlet, @cache, @settings)
  srv
end

#setupObject

Sets up the server.



109
110
111
112
113
114
# File 'lib/sitediff/webserver/resultserver.rb', line 109

def setup
  super
  root = uris.first
  puts "Serving at #{root}"
  open_in_browser(root) if @opts[:browse]
end

#which(cmd) ⇒ Object

TODO: Document what this method does.



127
128
129
130
131
132
133
# File 'lib/sitediff/webserver/resultserver.rb', line 127

def which(cmd)
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    file = File.join(path, cmd)
    return file if File.executable?(file)
  end
  nil
end