Module: Grape::Middleware::Base::Formats
- Defined in:
- lib/grape/middleware/base.rb
Constant Summary collapse
- CONTENT_TYPES =
{ :xml => 'application/xml', :json => 'application/json', :atom => 'application/atom+xml', :rss => 'application/rss+xml', :txt => 'text/plain' }
- FORMATTERS =
{ :json => :encode_json, :txt => :encode_txt, :xml => :encode_xml }
- PARSERS =
{ :json => :decode_json, :xml => :decode_xml }
Instance Method Summary collapse
- #content_type ⇒ Object
- #content_types ⇒ Object
- #decode_json(object) ⇒ Object
- #decode_xml(object) ⇒ Object
- #encode_json(object) ⇒ Object
- #encode_txt(object) ⇒ Object
- #encode_xml(object) ⇒ Object
- #formatter_for(api_format) ⇒ Object
- #formatters ⇒ Object
- #mime_types ⇒ Object
- #parser_for(api_format) ⇒ Object
- #parsers ⇒ Object
Instance Method Details
#content_type ⇒ Object
77 78 79 |
# File 'lib/grape/middleware/base.rb', line 77 def content_type content_types[[:format]] || 'text/html' end |
#content_types ⇒ Object
73 74 75 |
# File 'lib/grape/middleware/base.rb', line 73 def content_types CONTENT_TYPES.merge([:content_types] || {}) end |
#decode_json(object) ⇒ Object
109 110 111 |
# File 'lib/grape/middleware/base.rb', line 109 def decode_json(object) MultiJson.load(object) end |
#decode_xml(object) ⇒ Object
131 132 133 |
# File 'lib/grape/middleware/base.rb', line 131 def decode_xml(object) MultiXml.parse(object) end |
#encode_json(object) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/grape/middleware/base.rb', line 113 def encode_json(object) return object if object.is_a?(String) if object.respond_to? :serializable_hash MultiJson.dump(object.serializable_hash) elsif object.kind_of?(Array) && !object.map {|o| o.respond_to? :serializable_hash }.include?(false) MultiJson.dump(object.map {|o| o.serializable_hash }) elsif object.respond_to? :to_json object.to_json else MultiJson.dump(object) end end |
#encode_txt(object) ⇒ Object
127 128 129 |
# File 'lib/grape/middleware/base.rb', line 127 def encode_txt(object) object.respond_to?(:to_txt) ? object.to_txt : object.to_s end |
#encode_xml(object) ⇒ Object
135 136 137 |
# File 'lib/grape/middleware/base.rb', line 135 def encode_xml(object) object.respond_to?(:to_xml) ? object.to_xml : object.to_s end |
#formatter_for(api_format) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/grape/middleware/base.rb', line 85 def formatter_for(api_format) spec = formatters[api_format] case spec when nil lambda { |obj| obj } when Symbol method(spec) else spec end end |
#formatters ⇒ Object
65 66 67 |
# File 'lib/grape/middleware/base.rb', line 65 def formatters FORMATTERS.merge([:formatters] || {}) end |
#mime_types ⇒ Object
81 82 83 |
# File 'lib/grape/middleware/base.rb', line 81 def mime_types content_types.invert end |
#parser_for(api_format) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/grape/middleware/base.rb', line 97 def parser_for(api_format) spec = parsers[api_format] case spec when nil nil when Symbol method(spec) else spec end end |
#parsers ⇒ Object
69 70 71 |
# File 'lib/grape/middleware/base.rb', line 69 def parsers PARSERS.merge([:parsers] || {}) end |