Class: TDiary::Dispatcher::IndexMain

Inherits:
Object
  • Object
show all
Defined in:
lib/tdiary/dispatcher/index_main.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, cgi) ⇒ IndexMain

Returns a new instance of IndexMain.



10
11
12
13
14
15
# File 'lib/tdiary/dispatcher/index_main.rb', line 10

def initialize( request, cgi )
  @request = request
  @cgi = cgi
  @conf = TDiary::Configuration::new( cgi, request )
  @params = request.params
end

Instance Attribute Details

#cgiObject (readonly)

Returns the value of attribute cgi.



8
9
10
# File 'lib/tdiary/dispatcher/index_main.rb', line 8

def cgi
  @cgi
end

#confObject (readonly)

Returns the value of attribute conf.



8
9
10
# File 'lib/tdiary/dispatcher/index_main.rb', line 8

def conf
  @conf
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/tdiary/dispatcher/index_main.rb', line 8

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/tdiary/dispatcher/index_main.rb', line 8

def request
  @request
end

#tdiaryObject (readonly)

Returns the value of attribute tdiary.



8
9
10
# File 'lib/tdiary/dispatcher/index_main.rb', line 8

def tdiary
  @tdiary
end

Class Method Details

.run(request, cgi) ⇒ Object



4
5
6
# File 'lib/tdiary/dispatcher/index_main.rb', line 4

def self.run( request, cgi )
  new( request, cgi ).run
end

Instance Method Details

#create_tdiaryObject



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
# File 'lib/tdiary/dispatcher/index_main.rb', line 76

def create_tdiary
  begin
    if params['comment']
      tdiary = TDiary::TDiaryComment::new( cgi, "day.rhtml", conf )
    elsif params['plugin']
      tdiary = TDiary::TDiaryPluginView::new( cgi, '', conf )
    elsif (date = params['date'])
      if /^\d{8}-\d+$/ =~ date
        tdiary = TDiary::TDiaryLatest::new( cgi, "latest.rhtml", conf )
      elsif /^\d{8}$/ =~ date
        tdiary = TDiary::TDiaryDay::new( cgi, "day.rhtml", conf )
      elsif /^\d{6}$/ =~ date
        tdiary = TDiary::TDiaryMonth::new( cgi, "month.rhtml", conf )
      elsif /^\d{4}$/ =~ date
        tdiary = TDiary::TDiaryNYear::new( cgi, "month.rhtml", conf )
      end
    elsif params['category']
      tdiary = TDiary::TDiaryCategoryView::new( cgi, "category.rhtml", conf )
    elsif params['q']
      tdiary = TDiary::TDiarySearch::new( cgi, "search.rhtml", conf )
    else
      tdiary = TDiary::TDiaryLatest::new( cgi, "latest.rhtml", conf )
    end
  rescue TDiary::PermissionError
    raise
  rescue TDiary::TDiaryError
  end
  ( tdiary ? tdiary : TDiary::TDiaryLatest::new( cgi, "latest.rhtml", conf ) )
end

#runObject



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/tdiary/dispatcher/index_main.rb', line 17

def run
  begin
    status = nil
    @tdiary = create_tdiary

    begin
      head = {
        'Content-Type' => 'text/html; charset=UTF-8',
        'Vary' => 'User-Agent'
      }
      head['status'] = status if status
      body = ''
      head['Last-Modified'] = CGI::rfc1123_date( tdiary.last_modified )

      if request.head?
        head['Pragma'] = 'no-cache'
        head['Cache-Control'] = 'no-cache'
        return TDiary::Response.new( '', 200, head )
      else
        require 'digest/md5'
        body = tdiary.eval_rhtml
        head['ETag'] = %Q["#{Digest::MD5.hexdigest( body )}"]
        if ENV['HTTP_IF_NONE_MATCH'] == head['ETag'] and request.get? then
          head['status'] = CGI::HTTP_STATUS['NOT_MODIFIED']
        else
          head['charset'] = conf.encoding
          head['Content-Length'] = body.bytesize.to_s
        end
        head['Pragma'] = 'no-cache'
        head['Cache-Control'] = 'no-cache'
        head['X-Frame-Options'] = conf.x_frame_options if conf.x_frame_options
        head['cookie'] = tdiary.cookies if tdiary.cookies.size > 0
        TDiary::Response.new( body, ::TDiary::Dispatcher.extract_status_for_legacy_tdiary( head ), head )
      end
    rescue TDiary::NotFound
      body = %Q[
            <h1>404 Not Found</h1>
            <div>#{' ' * 500}</div>]
      TDiary::Response.new( body, 404, { 'Content-Type' => 'text/html' } )
    end
  rescue TDiary::ForceRedirect
    head = {
      #'Location' => $!.path
      'Content-Type' => 'text/html',
    }
    body = %Q[
          <html>
          <head>
          <meta http-equiv="refresh" content="1;url=#{$!.path}">
          <title>moving...</title>
          </head>
          <body>Wait or <a href="#{$!.path}">Click here!</a></body>
          </html>]
    head['cookie'] = tdiary.cookies if tdiary && tdiary.cookies.size > 0
    # TODO return code should be 302? (current behaviour returns 200)
    TDiary::Response.new( body, 200, head )
  end
end