class Navigable::Server::Response

Constants

CONTENT_LENGTH
CONTENT_TYPE
EMPTY_CONTENT
MIME_TYPE_HTML
MIME_TYPE_JSON
MIME_TYPE_TEXT

Attributes

params[R]
status[R]

Public Class Methods

new(params) click to toggle source
# File lib/navigable/server/response.rb, line 15
def initialize(params)
  @params = params
  @status = params[:status] || 200
end

Public Instance Methods

to_rack_response() click to toggle source
# File lib/navigable/server/response.rb, line 20
def to_rack_response
  [status, headers, [content]]
end

Private Instance Methods

body() click to toggle source
# File lib/navigable/server/response.rb, line 63
def body
  params[:body]
end
content() click to toggle source
# File lib/navigable/server/response.rb, line 44
def content
  json || html || text || body || EMPTY_CONTENT
end
content_length() click to toggle source
# File lib/navigable/server/response.rb, line 40
def content_length
  content.bytesize.to_s
end
content_type() click to toggle source
# File lib/navigable/server/response.rb, line 34
def content_type
  return MIME_TYPE_JSON if json
  return MIME_TYPE_HTML if html
  return MIME_TYPE_TEXT if text
end
headers() click to toggle source
# File lib/navigable/server/response.rb, line 26
def headers
  headers = {}
  headers[CONTENT_TYPE] = content_type if content_type
  headers[CONTENT_LENGTH] = content_length
  headers.merge!(params[:headers]) if params[:headers]
  headers
end
html() click to toggle source
# File lib/navigable/server/response.rb, line 55
def html
  params[:html]
end
json() click to toggle source
# File lib/navigable/server/response.rb, line 48
def json
  return unless params[:json]
  return stringified_json if valid_json?

  params[:json].to_json
end
stringified_json() click to toggle source
# File lib/navigable/server/response.rb, line 74
def stringified_json
  @stringified_json ||= params[:json].to_s
end
text() click to toggle source
# File lib/navigable/server/response.rb, line 59
def text
  params[:text]
end
valid_json?() click to toggle source
# File lib/navigable/server/response.rb, line 67
def valid_json?
  JSON.parse(stringified_json)
  return true
rescue JSON::ParserError => e
  return false
end