Skip to content

Instantly share code, notes, and snippets.

@dpsk
Created February 3, 2016 17:54
Show Gist options
  • Select an option

  • Save dpsk/d426fbbadefbc74959c4 to your computer and use it in GitHub Desktop.

Select an option

Save dpsk/d426fbbadefbc74959c4 to your computer and use it in GitHub Desktop.

Revisions

  1. dpsk created this gist Feb 3, 2016.
    7 changes: 7 additions & 0 deletions api.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@

    module API
    class Base < Grape::API
    mount API::V1::ApplicationV1 => "/api"
    end
    end

    23 changes: 23 additions & 0 deletions application_v1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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
    13 changes: 13 additions & 0 deletions data.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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