Comments (3)
I am trying to figure this one out as well.
From the specs, it looks like you should be able to something like this:
use Grape::Middleware::Formatter, {
:default_format => :json,
:formatters => {
:xml => lambda { |obj|
"<xml>format your object here</xml>"
}
}
}
but this throws an error for me:
[ pid=94431 thr=2151955260 file=utils.rb:176 time=2011-06-15 16:26:55.441 ]: *** Exception NoMethodError in application (undefined method `collect' for #<Rack::Response:0x000001062145d8>) (process 94431, thread #<Thread:0x00000100887678>):
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/formatter.rb:88:in `after'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/base.rb:23:in `call!'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/base.rb:16:in `call'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/base.rb:22:in `call!'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/base.rb:16:in `call'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/base.rb:22:in `call!'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/base.rb:16:in `call'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/error.rb:9:in `block in call!'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/error.rb:8:in `catch'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/error.rb:8:in `call!'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/middleware/base.rb:16:in `call'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/rack-mount-0.8.1/lib/rack/mount/route_set.rb:152:in `block in call'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/rack-mount-0.8.1/lib/rack/mount/code_generation.rb:93:in `block in recognize'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/rack-mount-0.8.1/lib/rack/mount/code_generation.rb:68:in `optimized_each'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/rack-mount-0.8.1/lib/rack/mount/code_generation.rb:92:in `recognize'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/rack-mount-0.8.1/lib/rack/mount/route_set.rb:141:in `call'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/grape-0.1.4/lib/grape/api.rb:25:in `call'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/rack-1.1.2/lib/rack/cascade.rb:23:in `block in call'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/rack-1.1.2/lib/rack/cascade.rb:22:in `each'
from /Users/dan/.rvm/gems/ruby-1.9.2-p180@grape_test/gems/rack-1.1.2/lib/rack/cascade.rb:22:in `call'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_request_handler.rb:513:in `accept_and_process_next_request'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/rack/application_spawner.rb:205:in `start_request_handler'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/rack/application_spawner.rb:170:in `block in handle_spawn_application'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/utils.rb:479:in `safe_fork'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/rack/application_spawner.rb:165:in `handle_spawn_application'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_server.rb:180:in `start'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/rack/application_spawner.rb:128:in `start'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/spawn_manager.rb:253:in `block (2 levels) in spawn_rack_application'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/spawn_manager.rb:246:in `block in spawn_rack_application'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_server_collection.rb:82:in `block in synchronize'
from <internal:prelude>:10:in `synchronize'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
from /Users/dan/.passenger/standalone/3.0.4-x86_64-ruby1.9.2-macosx-10.6/support/helper-scripts/passenger-spawn-server:99:in `<main>'
I'd appreciate any help here!
Thanks,
--Dan
from grape.
This is what I did to add CSV support, should be easy to modify to support xml/xhtml:
Grape::Middleware::Formatter::CONTENT_TYPES[:csv] = 'text/csv'
Grape::Middleware::Formatter::FORMATTERS[:csv] = Proc.new { |object|
return '' if !object.kind_of?(Enumerable) || object.empty?
header_keys = ENV['api.csv.headers']
headers = (header_keys && header_keys.split(',')) || begin
first = object.first
hsh = ((first.kind_of?(Hash) && first) ||
first.try(:serializable_hash) ||
first.try(:attributes) ||
first.try(:to_hsh) ).keys.sort
end
lines = object.collect do |row|
values = headers.inject([]) { |m, h| m << row[h]; m }
CSV.generate_line(values)
end
lines.unshift(CSV.generate_line(headers))
lines * "\n"
}
There really should be an easier way to specify custom formatters.
from grape.
It's terrible, but!
Grape::Middleware::Formatter::FORMATTERS[:xml] = Proc.new { |object|
object[object.keys.first].as_json.to_xml :root => object.keys.first, :skip_types => true
}
Converts to XML for active record...
from grape.
Related Issues (20)
- Ruby 3.3 && cookiejar = ArgumentError HOT 1
- Use Rack's HeadersHash instead of {}
- endpoint method_missing vs override inspect HOT 2
- Values Validator => Proc's arity < 2 ? HOT 3
- Rails Edge requires Ruby >= 3.1
- Fix specs for Rack Edge
- Fix `params` warning method redefined
- Better tracking of gems deprecation
- Rack::Lint::Error => a header value must be a String or Array of Strings, but the value of 'content-type' is a NilClass HOT 2
- Recognize_path should account for HTTP method HOT 1
- Grape and Protecting Against Mass Assignment Abuse HOT 8
- Why do you convert PATH_INFO? Is there some RFC? HOT 2
- Add support for exclusive parameter groups (e.g. exactly_one_of) HOT 2
- Drop support rack ~> 1 ? HOT 2
- Replace Appraisals by eval_gemfile ? HOT 3
- DeprecatedConstantProxy without a deprecator is deprecated HOT 1
- Investigation memory allocations HOT 1
- Add a memory usage check
- Dropping builder as dependency ? HOT 1
- Introduce standard gem ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grape.