Class: Hermeneutics::Cgi

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

Overview

Example:

class MyCgi < Cgi

def run
  p = parameters
  if p.empty? then
    location "/sorry.rb"
  else
    document MyHtml
  end
rescue
  document MyErrorPage
end

end Cgi.execute

Defined Under Namespace

Classes: Done

Constant Summary collapse

CGIENV =
%w(content document gateway http query
remote request script server unique)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/hermeneutics/cgi.rb', line 92

def method_missing sym, *args
  if args.empty? and CGIENV.include? sym[ /\A(\w+?)_\w+\z/, 1] then
    ENV[ sym.to_s.upcase]
  else
    super
  end
end

Class Attribute Details

.mainObject

Returns the value of attribute main.



59
60
61
# File 'lib/hermeneutics/cgi.rb', line 59

def main
  @main
end

Class Method Details

.execute(out = nil) ⇒ Object



63
64
65
# File 'lib/hermeneutics/cgi.rb', line 63

def execute out = nil
  (@main||self).new.execute out
end

.inherited(cls) ⇒ Object



60
61
62
# File 'lib/hermeneutics/cgi.rb', line 60

def inherited cls
  Cgi.main = cls
end

Instance Method Details

#document(cls = Html, *args, &block) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/hermeneutics/cgi.rb', line 201

def document cls = Html, *args, &block
  done { |res|
    doc = cls.new self
    res.body = ""
    doc.generate res.body do
      doc.document *args, &block
    end

    ct = if doc.respond_to?    :content_type then doc.content_type
    elsif   cls.const_defined? :CONTENT_TYPE then doc.class::CONTENT_TYPE
    end
    ct and res.headers.add :content_type, ct,
                charset: res.body.encoding||Encoding.default_external
    if doc.respond_to? :cookies then
      doc.cookies do |c|
        res.headers.add :set_cookie, c
      end
    end
  }
end

#execute(out = nil) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/hermeneutics/cgi.rb', line 183

def execute out = nil
  @out ||= $stdout
  begin
    run
  rescue
    done { |res|
      res.body = "#$! (#{$!.class})#$/"
      $@.each { |a| res.body << "\t" << a << $/ }
      res.headers.add :content_type,
                        "text/plain", charset: res.body.encoding
    }
  end
rescue Done
  @out << $!.result.to_s
ensure
  @out = nil
end

#fullpath(dest) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/hermeneutics/cgi.rb', line 234

def fullpath dest
  if dest then
    dest =~ %r{\A/} || dest =~ /\.\w+\z/ ? dest : dest + ".rb"
  else
    File.basename script_name
  end
end

#https?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/hermeneutics/cgi.rb', line 100

def https?
  ENV[ "HTTPS"].notempty?
end

#location(dest = nil, params = nil, anchor = nil) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/hermeneutics/cgi.rb', line 222

def location dest = nil, params = nil, anchor = nil
  if Hash === dest then
    dest, params, anchor = anchor, dest, params
  end
  utx = URLText.new mask_space: true
  unless dest =~ %r{\A\w+://} then
    dest = %Q'#{https? ? "https" : "http"}://#{http_host}#{fullpath dest}'
  end
  url = utx.mkurl dest, params, anchor
  done { |res| res.headers.add "Location", url }
end

#parameters(&block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hermeneutics/cgi.rb', line 73

def parameters &block
  if block_given? then
    case request_method
      when "GET", "HEAD" then parse_query query_string, &block
      when "POST"        then parse_posted &block
      else                    parse_input &block
    end
  else
    p = {}
    parameters do |k,v|
      p[ k] = v
    end
    p
  end
end

#query_stringObject

This has not been tested.



245
246
247
# File 'lib/hermeneutics/cgi.rb', line 245

def query_string
  Apache::request.args
end

#runObject

Overwrite this.



69
70
71
# File 'lib/hermeneutics/cgi.rb', line 69

def run
  document Html
end