class ResponseContext

Public Class Methods

new(params, uri, options={}) click to toggle source
# File lib/gemini_server.rb, line 200
def initialize params, uri, options={}
  @__params = params
  @__uri = uri
  @__mime_type = options[:mime_type]
  @__charset = options[:charset]
  @__lang = options[:lang]
  @__views_folder = options[:views_folder]
  temporary_failure
end

Public Instance Methods

charset(c;) click to toggle source
# File lib/gemini_server.rb, line 221
def charset c; @__charset = c; end
erb(template, locals: {}) click to toggle source
# File lib/gemini_server.rb, line 224
def erb template, locals: {}
  b = TOPLEVEL_BINDING.dup
  b.local_variable_set(:params, params)
  locals.each { |key, val| b.local_variable_set(key, val) }
  template = File.basename(template.to_s, ".erb")
  mime_type = MIME::Types.type_for(template).first || GEMINI_MIME_TYPE
  t = ERB.new(File.read(File.join(@__views_folder, "#{template}.erb")))
  body = t.result(b)
  success(body, mime_type)
end
lang(l;) click to toggle source
# File lib/gemini_server.rb, line 222
def lang l; @__lang = l; end
mime_type(t) click to toggle source
# File lib/gemini_server.rb, line 210
def mime_type t
  type = MIME::Types[t].first
  if type
    @__mime_type = type
  else
    STDERR.puts("WARN: Unknown MIME type #{t.inspect}")
  end
end
params() click to toggle source
# File lib/gemini_server.rb, line 219
def params; @__params; end
respond(code, meta, body=nil) click to toggle source
# File lib/gemini_server.rb, line 235
def respond code, meta, body=nil
  @__response = { code: code, meta: meta, body: body }
end
response() click to toggle source
# File lib/gemini_server.rb, line 239
def response
  @__response.to_h.merge({ mime_type: @__mime_type, lang: @__lang, charset: @__charset })
end
uri() click to toggle source
# File lib/gemini_server.rb, line 220
def uri; @__uri; end