Skip to content

Instantly share code, notes, and snippets.

@dpsk
Created February 3, 2016 17:54
Show Gist options
  • Save dpsk/d426fbbadefbc74959c4 to your computer and use it in GitHub Desktop.
Save dpsk/d426fbbadefbc74959c4 to your computer and use it in GitHub Desktop.
basic example on how to use xml api with grape
module API
class Base < Grape::API
mount API::V1::ApplicationV1 => "/api"
end
end
module API
module V1
class ApplicationV1 < Grape::API
require 'builder'
format :xml
default_format :xml
unless Rails.env.development?
rescue_from :all do |message, backtrace, options, env|
Rails.logger.info message.inspect
Rails.logger.info backtrace.inspect
Rails.logger.info options.inspect
Rails.logger.info env.inspect
Rack::Response.new([{message: "Invalid Request"}.to_xml(root: :error)], 400, { "Content-type" => "application/xml" }).finish
end
end
get "/action" do
@data = Data.get(params)
XMLResponses::Data::generate(@data)
end
end
end
end
module API
module V1
module XMLResponses::Data
def self.generate(data)
xml = Builder::XmlMarkup.new(:indent => 2)
xml.Response do |resp|
resp.APIVersion "1.0"
resp.DataField data.field
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment