Class: Bcat

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils
Defined in:
lib/bcat.rb,
lib/bcat/ansi.rb,
lib/bcat/html.rb,
lib/bcat/reader.rb,
lib/bcat/server.rb,
lib/bcat/browser.rb

Defined Under Namespace

Classes: ANSI, Browser, HeadParser, Reader, Server, TeeFilter, TextFilter

Constant Summary collapse

VERSION =
'0.6.0.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = [], config = {}) ⇒ Bcat

Returns a new instance of Bcat.



14
15
16
17
18
# File 'lib/bcat.rb', line 14

def initialize(args=[], config={})
  @config = {:Host => '127.0.0.1', :Port => 8091}.merge(config)
  @reader = Bcat::Reader.new(@config[:command], args)
  @format = @config[:format]
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



12
13
14
# File 'lib/bcat.rb', line 12

def format
  @format
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/bcat.rb', line 20

def [](key)
  @config[key]
end

#assembleObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/bcat.rb', line 41

def assemble
  @reader.open

  @format = @reader.sniff if @format.nil?

  @filter = @reader
  @filter = TeeFilter.new(@filter) if @config[:tee]
  @filter = TextFilter.new(@filter) if @format == 'text'
  @filter = ANSI.new(@filter) if @format == 'text' || @config[:ansi]
end

#call(env) ⇒ Object



36
37
38
39
# File 'lib/bcat.rb', line 36

def call(env)
  notice "#{env['REQUEST_METHOD']} #{env['PATH_INFO'].inspect}"
  [200, {"Content-Type" => "text/html;charset=utf-8"}, self]
end

#closeObject



107
108
109
110
111
112
# File 'lib/bcat.rb', line 107

def close
  unless @config[:persist]
    notice "closing with interrupt"
    raise Interrupt, "connection closed"
  end
end

#content_for_head(inject = '') ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bcat.rb', line 83

def content_for_head(inject='')
  [
    "\n" * 1000,
    "<!DOCTYPE html>",
    "<html>",
    "<head>",
    "<!-- bcat was here -->",
    inject.to_s,
    "<link href=\"data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=\" rel=\"icon\" type=\"image/x-icon\" />",
    "<title>#{self[:title] || 'bcat'}</title>",
    "</head>"
  ].join("\n")
end

#eachObject



52
53
54
55
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
# File 'lib/bcat.rb', line 52

def each
  assemble

  head_parser = Bcat::HeadParser.new

  @filter.each do |buf|
    if head_parser.nil?
      yield buf
    elsif head_parser.feed(buf)
      yield content_for_head(inject=head_parser.head)
      yield "\n"
      yield head_parser.body
      head_parser = nil
    end
  end

  if head_parser
    yield content_for_head(inject=head_parser.head) +
          "\n" +
          head_parser.body
  end

  yield foot
rescue Errno::EINVAL
  # socket was closed
  notice "browser client went away"
rescue => boom
  notice "boom: #{boom.class}: #{boom.to_s}"
  raise
end

#escape_js(string) ⇒ Object



101
102
103
104
105
# File 'lib/bcat.rb', line 101

def escape_js(string)
  string = string.gsub(/['\\]/) { |char| "\\#{char}" }
  string.gsub!(/\n/, '\n')
  string
end

#footObject



97
98
99
# File 'lib/bcat.rb', line 97

def foot
  "</body>\n</html>\n"
end

#notice(message) ⇒ Object



114
115
116
117
# File 'lib/bcat.rb', line 114

def notice(message)
  return if !@config[:debug]
  warn "#{File.basename($0)}: #{message}"
end

#serve!(&bk) ⇒ Object



32
33
34
# File 'lib/bcat.rb', line 32

def serve!(&bk)
  Bcat::Server.run to_app, @config, &bk
end

#to_appObject



24
25
26
27
28
29
30
# File 'lib/bcat.rb', line 24

def to_app
  app = self
  Rack::Builder.new do
    use Rack::Chunked
    run app
  end
end