Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods to set content type and content #313

Merged
merged 24 commits into from
Nov 4, 2017
Merged
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1e8a04a
methods to set content type and content
elorest Oct 18, 2017
fd9a719
Merge branch 'master' into is/render_content_types
eliasjpr Oct 25, 2017
e901a43
Merge branch 'master' into is/render_content_types
eliasjpr Oct 27, 2017
e4b48bb
Merge branch 'master' into is/render_content_types
eliasjpr Oct 28, 2017
94a7c34
respond_with
elorest Oct 29, 2017
006fdbe
Merge branch 'is/render_content_types' of github.com:Amber-Crystal/am…
elorest Oct 29, 2017
7be1a54
respond with works
elorest Oct 29, 2017
796f84a
organized methods better
elorest Oct 29, 2017
f5698bf
allow hash or json for json response
elorest Oct 29, 2017
5e540c8
formatting
elorest Oct 29, 2017
a068f4a
small changes
elorest Oct 29, 2017
5dd0796
Merge branch 'master' into is/render_content_types
eliasjpr Oct 31, 2017
77e1307
Merge branch 'master' into is/render_content_types
eliasjpr Oct 31, 2017
555f2ee
Merge branch 'master' into is/render_content_types
elorest Nov 2, 2017
60bce41
moved content_types out to constant
elorest Nov 2, 2017
70b4813
tested IRL
elorest Nov 3, 2017
a6cbee5
refactored file structure
elorest Nov 3, 2017
73b65b6
works after many hours
elorest Nov 3, 2017
2493d73
good for now, got it working as good as it used to.
elorest Nov 3, 2017
1478f5e
added tests
elorest Nov 3, 2017
560de4f
tests work and fixes empty headers'
elorest Nov 3, 2017
5efb25f
Merge branch 'master' into is/render_content_types
elorest Nov 3, 2017
1d60857
removed vestigul code
elorest Nov 3, 2017
58791b1
Merge branch 'is/render_content_types' of github.com:Amber-Crystal/am…
elorest Nov 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/amber/controller/render.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,43 @@ module Amber::Controller
Kilt.render("#{{{path}}}/{{filename.id}}")
{% end %}
end

protected def respond_with(html : String? = nil, json : Hash | String? = nil, xml : String? = nil, text : String? = nil)
accepts = context.request.headers["Accept"].split(";").try(&.split(/,|,\s/))

if accepts.includes?("text/html") && html
Copy link
Contributor

@eliasjpr eliasjpr Oct 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a constant if you want to keep the string as such:

RESPOND = { 
  html: "text/html", 
  json: "application/json", 
  text: "text/plain", 
  xml: "application/xml"
}

accepts.includes? RESPOND[:html]
accepts.includes? RESPOND[:json]
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that looks good.

respond_with_html(html)
elsif accepts.includes?("application/json") && json
respond_with_json(json.is_a?(Hash) ? json.to_json : json)
elsif accepts.includes?("application/xml") && xml
respond_with_xml(xml)
elsif accepts.includes?("text/plain") && text
respond_with_text(text)
else
respond_with_text("Response not acceptable", 406)
end
end

protected def respond_with_html(body, status_code = 200)
set_response(body, status_code, "text/html")
end

protected def respond_with_text(body, status_code = 200)
set_response(body, status_code, "text/plain")
end

protected def respond_with_json(body, status_code = 200)
set_response(body, status_code, "application/json")
end

protected def respond_with_xml(body, status_code = 200)
set_response(body, status_code, "application/xml")
end

private def set_response(body, status_code = 200, content_type = "text/html")
context.response.status_code = status_code
context.response.content_type = content_type
context.content = body
end
end
end