Skip to content

Instantly share code, notes, and snippets.

@liushooter
Created June 13, 2015 02:59
Show Gist options
  • Save liushooter/beb70f1c7e98fd30c377 to your computer and use it in GitHub Desktop.
Save liushooter/beb70f1c7e98fd30c377 to your computer and use it in GitHub Desktop.
💋 扩展grape的formatter, 支持返回 html
module Grape
module ContentTypes
def self.content_types_for(from_settings)
if from_settings.present?
from_settings
else
ActiveSupport::OrderedHash[
:xml, 'application/xml',
:serializable_hash, 'application/json',
:json, 'application/json',
:binary, 'application/octet-stream',
:txt, 'text/plain',
:html, 'text/html'
]
end
end
end
end
module Grape
module Formatter
module Html
class << self
def call(object, env)
end
end
end
end
end
Grape::Formatter::Base.formatters({html: Grape::Formatter::Html})
#####
class Doc < Grape::API
format :html
content_type :html, 'text/html'
get '/path' do
#render erb template
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment