Created
February 3, 2016 17:54
-
-
Save dpsk/d426fbbadefbc74959c4 to your computer and use it in GitHub Desktop.
basic example on how to use xml api with grape
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module API | |
class Base < Grape::API | |
mount API::V1::ApplicationV1 => "/api" | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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