Coder Social home page Coder Social logo

Return XML/XHTML? about grape HOT 3 CLOSED

ruby-grape avatar ruby-grape commented on May 29, 2024
Return XML/XHTML?

from grape.

Comments (3)

danhealy avatar danhealy commented on May 29, 2024

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.

cyu avatar cyu commented on May 29, 2024

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.

bobbytables avatar bobbytables commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.