Class: BitClust::App

Inherits:
Object show all
Defined in:
lib/bitclust/app.rb

Overview

Main class of BitClust server application. Actual actions are implemneted by RequestHandler.

Supports Rack and WEBrick.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ App

Returns a new instance of App.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bitclust/app.rb', line 13

def initialize(options)
  @options = options
  dbpath = options[:dbpath]
  baseurl = options[:baseurl] || ''
  datadir = options[:datadir] || File.expand_path('../../data/bitclust', File.dirname(__FILE__))
  encoding = options[:encoding] || 'utf-8'
  viewpath = options[:viewpath]
  capi = options[:capi]
  if options[:rack]
    request_handler_class = BitClust::RackRequestHandler
  else
    request_handler_class = BitClust::RequestHandler
  end
  @interfaces = {}
  case dbpath
  when String
    dbpath = File.expand_path(dbpath)
    db = BitClust::MethodDatabase.new(dbpath)
    if capi
      db = [db, BitClust::FunctionDatabase.new(dbpath)]
    end
    manager = BitClust::ScreenManager.new(
      :base_url => baseurl,
      :cgi_url => File.join(baseurl, viewpath),
      :datadir => datadir,
      :templatedir => options[:templatedir],
      :theme => options[:theme],
      :encoding => encoding
      )
    handler = request_handler_class.new(db, manager)
    @interfaces[viewpath] = BitClust::Interface.new { handler }
  when Array
    dbpaths = dbpath
    @versions = []
    dbpaths.each do |dbpath|
      next unless /db-([\d_\.]+)/ =~ dbpath
      dbpath = File.expand_path(dbpath)
      version = $1.tr("_", ".")
      @versions << version
      if viewpath
        version_viewpath = File.join(version, viewpath)
      else
        version_viewpath = version
      end
      db = BitClust::MethodDatabase.new(dbpath)
      if capi
        db = [db, BitClust::FunctionDatabase.new(dbpath)]
      end
      manager = BitClust::ScreenManager.new(
        :base_url => baseurl,
        :cgi_url => File.join(baseurl, version_viewpath),
        :datadir => datadir,
        :templatedir => options[:templatedir],
        :theme => options[:theme],
        :encoding => encoding
        )
      handler = request_handler_class.new(db, manager)
      @interfaces[version_viewpath] = BitClust::Interface.new { handler }
      $bitclust_context_cache = nil # clear cache
    end
  end
end

Instance Attribute Details

#interfacesObject (readonly)

Returns the value of attribute interfaces.



76
77
78
# File 'lib/bitclust/app.rb', line 76

def interfaces
  @interfaces
end

#versionsObject (readonly)

Returns the value of attribute versions.



76
77
78
# File 'lib/bitclust/app.rb', line 76

def versions
  @versions
end

Instance Method Details

#call(env) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/bitclust/app.rb', line 126

def call(env)
  [
    200,
    {'Content-Type' => 'text/html; charset=utf-8'},
    [index(Rack::Request.new(env))]
  ]
end

#get_instance(server) ⇒ Object



114
115
116
# File 'lib/bitclust/app.rb', line 114

def get_instance(server)
  self
end

#index(req) ⇒ Object



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
# File 'lib/bitclust/app.rb', line 78

def index(req)
  case
  when @interfaces.size == 1 && viewpath = @options[:viewpath]
    # Redirect from '/' to "#{viewpath}/"
    @index = "<html><head><meta http-equiv='Refresh' content='0;URL=#{viewpath}'></head></html>"
  when 1 < @interfaces.size
    request_path = case
                   when req.respond_to?(:path_info)
                     req.path_info
                   when req.respond_to?(:path)
                     req.path_info
                   end
    if @versions.any?{|version| %r|\A/?#{version}/?\z| =~ request_path }
      viewpath = File.join(request_path, @options[:viewpath])
      @index = "<html><head><meta http-equiv='Refresh' content='0;URL=#{viewpath}'></head></html>"
    else
      links = "<ul>"
      @interfaces.keys.sort.each do |v|
        if @options[:viewpath]
          version = v.sub(@options[:viewpath], '')
        else
          version = v
        end
        url = v
        links << %Q(<li><a href="#{url}/">#{version}</a></li>)
      end
      links << "</ul>"
      if File.exist?("readme.html")
        @index = File.read("readme.html").sub(%r!\./bitclust!, '').sub(/<!--links-->/) { links }
      else
        @index = "<html><head><title>bitclust</title></head><body>#{links}</body></html>"
      end
    end
  end
end

#service(req, res) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/bitclust/app.rb', line 118

def service(req, res)
  unless  %r|/#{File.basename(@options[:baseurl])}/?\z| =~ req.path
    raise WEBrick::HTTPStatus::NotFound
  end
  res.body = index(req)
  res['Content-Type'] = 'text/html; charset=utf-8'
end