Module: Identizer::Responses

Included in:
App, Handlers::Base
Defined in:
lib/identizer/responses.rb

Overview

Tiny helpers for building Rack responses ([status, headers, body]).

Instance Method Summary collapse

Instance Method Details

#amz_json(payload) ⇒ Object

AWS SDK expects this content type back from the Cognito management API.



11
12
13
# File 'lib/identizer/responses.rb', line 11

def amz_json(payload)
  [200, { "content-type" => "application/x-amz-json-1.1" }, [JSON.generate(payload)]]
end

#escape_html(value) ⇒ Object



35
36
37
# File 'lib/identizer/responses.rb', line 35

def escape_html(value)
  CGI.escapeHTML(value.to_s)
end

#html(body, status: 200) ⇒ Object



15
16
17
# File 'lib/identizer/responses.rb', line 15

def html(body, status: 200)
  [status, { "content-type" => "text/html; charset=utf-8" }, [body]]
end

#json(status, payload) ⇒ Object



6
7
8
# File 'lib/identizer/responses.rb', line 6

def json(status, payload)
  [status, { "content-type" => "application/json" }, [JSON.generate(payload)]]
end

#no_contentObject



31
32
33
# File 'lib/identizer/responses.rb', line 31

def no_content
  [204, {}, []]
end

#not_found(message) ⇒ Object



27
28
29
# File 'lib/identizer/responses.rb', line 27

def not_found(message)
  json(404, { error: message })
end

#notice_page(heading, body_html) ⇒ Object

A small standalone HTML page (errors/notices). ‘body_html` is inserted raw, so callers must escape any user-controlled values they put in it.



41
42
43
44
# File 'lib/identizer/responses.rb', line 41

def notice_page(heading, body_html)
  html("<!doctype html><html><body style=\"font-family:sans-serif;max-width:480px;margin:64px auto\">" \
       "<h2>#{escape_html(heading)}</h2><p>#{body_html}</p></body></html>")
end

#redirect(location, status: 302) ⇒ Object



23
24
25
# File 'lib/identizer/responses.rb', line 23

def redirect(location, status: 302)
  [status, { "location" => location }, []]
end

#xml(body, headers: {}) ⇒ Object



19
20
21
# File 'lib/identizer/responses.rb', line 19

def xml(body, headers: {})
  [200, { "content-type" => "application/xml; charset=utf-8" }.merge(headers), [body]]
end