Class: Widow

Inherits:
Object
  • Object
show all
Defined in:
lib/widow.rb

Defined Under Namespace

Classes: CompileError, Exclude

Constant Summary collapse

@@mime_map =
[
  ['js', 'application/javascript'],
  ['css', 'text/css'],
  ['html', 'text/html']
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Widow

Returns a new instance of Widow.



28
29
30
31
32
# File 'lib/widow.rb', line 28

def initialize options = {}
  log_level = options.delete(:debug) ? :debug : :info
  @logger = Logger.new(STDOUT, level: log_level)
  @logger.info "log level #{log_level}"
end

Class Method Details

.perform(method, options) ⇒ Object



22
23
24
25
26
# File 'lib/widow.rb', line 22

def self.perform method, options
  widow = Widow.new(options)
  widow.public_send(method, options)
  widow.kill
end

Instance Method Details

#cut(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/widow.rb', line 34

def cut options = {}
  options = {from: Pathname.pwd, to: Pathname.pwd}.merge options
  from = Pathname.new(options[:from])
  to = Pathname.new(options[:to])
  @logger.info "cut from #{from} to #{to}"
  exclude = Exclude.new from, @logger

  Find.find from do |path|
    path = Pathname.new path
    next if path == from
    if exclude.ignored? path
      @logger.info "#{path} ignored"
      next
    end
    begin
      make from, path.relative_path_from(from), to
    rescue CompileError
    end
  end
  return
end

#killObject

Called to clean up



117
118
119
120
# File 'lib/widow.rb', line 117

def kill
  @logger.close
  return
end

#spin(options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/widow.rb', line 56

def spin options = {}
  options = {from: Pathname.pwd, to: Pathname.pwd, port: 8888, serve_source: false}.merge options
  from = Pathname.new(options[:from])
  to = Pathname.new(options[:to])
  @logger.info "spin from #{from} to #{to}"
  @logger.info "port #{options[:port]}"
  @logger.info "serve_source = #{options[:serve_source]}"
  exclude = Exclude.new from, @logger

  mime_to_ext = Hash.new { |hash, key| hash[key] = [] }
  Tilt.lazy_map.map do |ext, _|
    begin
      tilt = Tilt[ext]
      @logger.info "loaded template #{tilt} for extention #{ext}"
      next [tilt.[:mime_type] || tilt.default_mime_type, ext]
    rescue LoadError
      next
    end
  end.compact.each do | mime_type, ext |
    mime_to_ext[mime_type] << ext
  end

  Rack::Handler.default.run(
    Proc.new do |env|
      @logger.debug "#{env["PATH_INFO"]} requested"

      file = Pathname.new env["PATH_INFO"][1..-1]
      file += 'index.html' if (from + file).directory?
      search = from + file.dirname + "#{file.basename file.extname}.{#{(mime_to_ext[mime_type file] + [file.extname[1..-1]]).join ?,}}"
      @logger.debug "searching for \'#{search}\'"
      original = Pathname.glob(search).first
      file = to + file

      if (exclude.ignored?(original) && !(options[:serve_source] && original.basename == file.basename))
        @logger.info "#{file} ignored"
        next file_not_found
      elsif (original.nil? || !original.exist?)
        @logger.debug "source not found"
        next file_not_found
      end
      @logger.debug "#{original} found"

      begin
        if !file.exist? || file.mtime < original.mtime
          if original.basename == file.basename
            proxy from, original.relative_path_from(from), to
          else
            make from, original.relative_path_from(from), to
          end
        end
        next [200, {'Content-Type' => mime_type(file)}, [file.file? ? file.read : '']]
      rescue CompileError => e
        next [502, {'Content-Type' => 'text/html'}, [e.message]]
      end
    end,
    Port: options[:port]
  )
  return
end