lib/slideit/container.rb in slideit-0.1.1 vs lib/slideit/container.rb in slideit-0.1.2
- old
+ new
@@ -1,5 +1,7 @@
+require 'socket'
+
require "slideit/template"
module Slideit
class Container
def initialize(file, options = {})
@@ -27,20 +29,41 @@
private
def prepare
root = File.expand_path "../../../res/reveal.js-3.3.0", __FILE__
- @port = @options[:port] ? @options[:port].to_i : 8000
+ @port = @options[:port] ? @options[:port].to_i : get_available_port
@server = WEBrick::HTTPServer.new Port: @port, DocumentRoot: root
trap 'INT' do @server.shutdown end
mount_slide
mount_assets
end
+ def get_available_port
+ port = 8000
+ times = 0
+ while is_port_open?(port)
+ times += 1
+ break if times > 5
+ port = 8000 + rand(10240)
+ end
+ port
+ end
+
+ def is_port_open?(port)
+ begin
+ s = TCPSocket.new('127.0.0.1', port)
+ s.close
+ return true
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
+ return false
+ end
+ end
+
def mount_slide
file_name = File.basename(@file)
@server.mount_proc "/#{file_name}" do |req, res|
res.status = 200
res['Content-Type'] = 'text/html'
@@ -67,10 +90,10 @@
def asset_files
md_dir = File.expand_path "..", @file
files = Dir["#{md_dir}/**/*"]
pairs = {}
files.each do |file|
- if File.file?(file) && !EXECLUDES.include?(File.extname(file))
+ if FileTest.file?(file) && !EXECLUDES.include?(File.extname(file))
key = file.sub md_dir, ""
pairs[key] = file
end
end
pairs