Class: ResponseContext

Inherits:
Object
  • Object
show all
Includes:
Responses
Defined in:
lib/gemini_server.rb

Instance Method Summary collapse

Methods included from Responses

#bad_request, #certificate_not_authorized, #certificate_not_valid, #cgi_error, #client_certificate_required, #gone, #input, #not_found, #permanent_failure, #proxy_error, #proxy_request_refused, #redirect_permanent, #redirect_temporary, #sensitive_input, #server_unavailable, #slow_down, #success, #temporary_failure

Constructor Details

#initialize(params, uri, options = {}) ⇒ ResponseContext

Returns a new instance of ResponseContext.



229
230
231
232
233
234
235
236
237
# File 'lib/gemini_server.rb', line 229

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

Instance Method Details

#charset(c) ⇒ Object



250
# File 'lib/gemini_server.rb', line 250

def charset c; @__charset = c; end

#erb(template, locals: {}) ⇒ Object



253
254
255
256
257
258
259
260
261
262
# File 'lib/gemini_server.rb', line 253

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) ⇒ Object



251
# File 'lib/gemini_server.rb', line 251

def lang l; @__lang = l; end

#mime_type(t) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/gemini_server.rb', line 239

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

#paramsObject



248
# File 'lib/gemini_server.rb', line 248

def params; @__params; end

#respond(code, meta, body = nil) ⇒ Object



264
265
266
# File 'lib/gemini_server.rb', line 264

def respond code, meta, body=nil
  @__response = { code: code, meta: meta, body: body }
end

#responseObject



268
269
270
# File 'lib/gemini_server.rb', line 268

def response
  @__response.to_h.merge({ mime_type: @__mime_type, lang: @__lang, charset: @__charset })
end

#uriObject



249
# File 'lib/gemini_server.rb', line 249

def uri; @__uri; end