Class: Nephos::Responder

Inherits:
Object
  • Object
show all
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: "application/json",
}

Instance Method Summary collapse

Instance Method Details

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



11
12
13
# File 'lib/nephos-server/server/responder.rb', line 11

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

#ct_specific(params) ⇒ Object

Parameters:

  • params (Hash)

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



16
17
18
19
20
21
22
# File 'lib/nephos-server/server/responder.rb', line 16

def 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

#empty_respObject



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

def empty_resp
  resp = Rack::Response.new
  resp.status = 204
  resp["Content-Type"] = ct_specific({type: PRESET_CT[:plain]})
  return resp
end

#render(params) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nephos-server/server/responder.rb', line 92

def render params
  return empty_resp if params == :empty
  return render(status: params) if params.is_a? Integer
  raise "Not a valid params argument" unless params.is_a? Hash

  resp = Rack::Response.new
  params = set_default_params(params)
  resp.status = params[:status]
  resp["Content-Type"] = params[:type]
  resp.body = [params[:content] + "\n"]
  return resp
end

#render_from_controller(req, call) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nephos-server/server/responder.rb', line 73

def render_from_controller req, call
  extension = req.path.match(call[:match])['extension']
  controller = Module.const_get(call[:controller]).new(req, call, extension)
  method_to_call = call[:method]

  controller.execute_before_action(method_to_call)
  # puts "Call #{controller} # #{method_to_call}"
  params = controller.send(method_to_call)
  controller.execute_after_action(method_to_call)

  # puts "--- Params #{params.class} ---", "<<", params, ">>"
  resp = render(params)
  controller.cookies.to_h.each do |k, v|
    resp.set_cookie k, v[:content]
    resp.header["Set-Cookie"] += ";path=" + v[:path]
  end
  return resp
end

#set_default_params(params) ⇒ Object

Fill params with default parameters (status, plain errors)



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

def set_default_params params
  set_default_params_status(params)
  set_default_params_type(params)
  set_default_params_content(params)
  return params
end

#set_default_params_content(params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nephos-server/server/responder.rb', line 42

def set_default_params_content params
  type = (params.keys & [:plain, :html, :json, :content]).first
  if type.nil?
    if params[:status].to_s.match(/^[45]\d\d$/)
      params[:content] = "Error: #{params[:status]} code"
    elsif params[:status].to_s.match(/^[3]\d\d$/)
      params[:content] = "Redirected: #{params[:status]} code"
    else
      params[:content] = "Status: #{params[:status]} code"
    end
  else
    params[type] = params[type].to_json if type == :json
    params[:content] = params[type]
  end
end

#set_default_params_status(params) ⇒ Object

if not :status entry, set to 200



31
32
33
# File 'lib/nephos-server/server/responder.rb', line 31

def set_default_params_status params
  params[:status] ||= 200
end

#set_default_params_type(params) ⇒ Object



35
36
37
38
39
40
# File 'lib/nephos-server/server/responder.rb', line 35

def set_default_params_type params
  if params[:type].nil?
    params[:type] = PRESET_CT[(params.keys & [:plain, :html, :json]).first || :plain]
  end
  params[:type] = ct_specific(params)
end