Module: Nephos::Responder

Defined in:
lib/nephos-server/server/responder.rb

Defined Under Namespace

Classes: InvalidContentType

Constant Summary collapse

CT_CHARSET_PREFIX =
'; charset='
PRESET_CT =
{
  plain: "text/plain",
  html: "text/html",
  json: "text/javascript",
}

Class Method Summary collapse

Class Method Details

.content_type(kind, type, charset = 'UTF-8') ⇒ Object



8
9
10
# File 'lib/nephos-server/server/responder.rb', line 8

def self.content_type(kind, type, charset='UTF-8')
  {'Content-type' => "#{kind}/#{type}" + CT_CHARSET_PREFIX + charset}
end

.ct_specific(params) ⇒ Object

Parameters:

  • params (Hash)

    containing :type => “kind/type”, example: “text/html”



13
14
15
16
17
18
19
# File 'lib/nephos-server/server/responder.rb', line 13

def self.ct_specific(params)
  kind, type = params[:type].to_s.match(/^(\w+)\/(\w+)$/) && Regexp.last_match[1..2]
  if kind.nil? or type.nil?
    raise InvalidContentType, "params[:type] must match with \"kind/type\""
  end
  content_type(kind, type)
end

.render(params) ⇒ Object

Parameters:

  • params (Hash, Symbol)


66
67
68
69
70
71
72
73
74
# File 'lib/nephos-server/server/responder.rb', line 66

def self.render params
  return [204, ct_specific({type: PRESET_CT[:plain]}), [""]] if params == :empty
  params = set_default_params(params)
  return [
    params[:status],
    params[:type],
    [params[:content]],
  ]
end

.set_default_params(params) ⇒ Object

Fill params with default parameters (status, plain errors)



58
59
60
61
62
63
# File 'lib/nephos-server/server/responder.rb', line 58

def self.set_default_params params
  set_default_params_status(params)
  set_default_params_type(params)
  set_default_params_content(params)
  params
end