-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Methods to set content type and content (#313)
* methods to set content type and content * respond_with * respond with works * organized methods better * allow hash or json for json response * formatting * small changes * moved content_types out to constant * tested IRL * refactored file structure * works after many hours * good for now, got it working as good as it used to. * added tests * tests work and fixes empty headers' * removed vestigul code Former-commit-id: 6d2d14d
- Loading branch information
Showing
5 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/amber/controller/render.cr → src/amber/controller/helpers/render.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Amber::Controller | ||
module Amber::Controller::Helpers | ||
module Render | ||
LAYOUT = "application.slang" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
module Amber::Controller::Helpers | ||
module Responders | ||
class Content | ||
TYPE = {html: "text/html", json: "application/json", text: "text/plain", xml: "application/xml"} | ||
@requested_responses : Array(String) | ||
@available_responses = Hash(String, String).new | ||
@type : String? = nil | ||
|
||
def initialize(@requested_responses) | ||
end | ||
|
||
# TODO: add JS type simlar to rails. | ||
def html(html : String) | ||
@available_responses[TYPE[:html]] = html; self | ||
end | ||
|
||
def xml(xml : String) | ||
@available_responses[TYPE[:xml]] = xml; self | ||
end | ||
|
||
def json(json : String | Hash(Symbol | String, String)) | ||
@available_responses[TYPE[:json]] = json.is_a?(String) ? json : json.to_json; self | ||
end | ||
|
||
def json(**args : Object) | ||
json(args.to_h) | ||
end | ||
|
||
def text(text : String) | ||
@available_responses[TYPE[:text]] = text; self | ||
end | ||
|
||
def type | ||
select_type.to_s | ||
end | ||
|
||
def body | ||
@available_responses[type] | ||
end | ||
|
||
private def select_type | ||
@type ||= begin | ||
raise "You must define at least one response_type." if @available_responses.empty? | ||
@requested_responses << @available_responses.keys.first | ||
@requested_responses.find do |resp| | ||
@available_responses.keys.includes?(resp) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private def requested_responses | ||
req_responses = Array(String).new | ||
|
||
if (accept = context.request.headers["Accept"]?) && !accept.empty? | ||
accepts = accept.split(";").first?.try(&.split(/,|,\s/)) | ||
req_responses.concat(accepts) if accepts.is_a?(Array) && accepts.any? | ||
end | ||
req_responses | ||
end | ||
|
||
protected def respond_with(&block) | ||
content = with Content.new(requested_responses) yield | ||
if content.body | ||
set_response(body: content.body, status_code: 200, content_type: content.type) | ||
else | ||
set_response(body: "Response unexceptable.", status_code: 406, content_type: Content::TYPE[:text]) | ||
end | ||
end | ||
|
||
private def set_response(body, status_code = 200, content_type = Content::TYPE[:html]) | ||
context.response.status_code = status_code | ||
context.response.content_type = content_type | ||
context.content = body | ||
end | ||
end | ||
end |