grape and ui

Grape demo

1. grape

gem 'grape'

1.1 基础使用

mkdir app/api/v1 -p  or mkdir app/api/v2 -p
touch app/api/v1/api.rb ...  #版本主挂载
touch app/base.rb #主挂载
1.1.1 配置

routes.rb

mount Base => 'api'

application.rb

config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb') #加载api下所有的文件,递归加载
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]

base.rb

class Base < Grape::API
  mount V1::API
  mount V2::API
  .....
end

api.rb

module Vx
  class API < Grape::API

    format :json
    version "vx"

    get '/' do
      "hello version xxx"
    end
  end
end

2. grape-swagger-rails or grape-swagger

gem 'grape-swagger' or  gem 'grape-swagger-rails'

api/vx/api.rb

require 'grape-swagger'
module Vx
  class API < Grape::API

    format :json
    version "vx"

    before do
      header['Access-Control-Allow-Origin'] = '*'
      header['Access-Control-Request-Method'] = '*'
    end

    get '/' do
      "hello version xxx"
    end

    add_swagger_documentation mount_path: 'doc.json', base_path: 'api', api_version: 'v1'
  end
end

3.swagger-ui_rails

gem 'swagger-ui_rails'

application.js

//= require swagger-ui

application.css

*= require swagger-ui

4 使用

<!DOCTYPE html>
<html>
  <head>
    <title>Swagger UI</title>
    <style>
      .swagger-ui-wrap {
        max-width: 80%;
        min-width: 960px;
        margin-left: auto;
        margin-right: auto;
      }
    </style>
    <script>
      $(function(){
        $.ajaxPrefilter(function(o, options){
          options.headers = { 'X-CSRF-Token': '#{form_authenticity_token.to_s}' }
        });
      });

      $(function () {
        window.swaggerUi = new SwaggerUi({
          url: 'http://localhost:3000/api/v1/doc.json',
          apiKey:"",
          dom_id:"swagger-ui-container",
          supportHeaderParams: false,
          supportedSubmitMethods: ['get', 'post', 'put', 'delete']
        });
        window.swaggerUi.load();
      });
    </script>
  </head>
  <body>
    <div class='swagger-ui-wrap' id='message-bar'>&nbsp;</div>
    <div class='swagger-ui-wrap' id='swagger-ui-container'></div>
  </body>
</html>

5. refer

6. 使用json和xml View

6.1 jbuilder
gem "grape-jbuilder"

application.rb

config.middleware.use(Rack::Config) do |env|
  env['api.tilt.root'] = Rails.root.join 'app', 'api'
  env['api.tilt.layout'] = 'layouts/another' #一般很少定义
  env['api.tilt.template'] = "v1/views/#{path}" #一般在helper中定义
end

在引用的helper中定义

def views_path(path)
 env['api.tilt.template'] = "v1/views/#{path}"    
end

在api.rb中

version 'v1'
format :json
content_type :json, "application/json;charset=UTF-8"
formatter :json, Grape::Formatter::Jbuilder #必须做这个申明,不然无法使用
6.2

注意:grape-ui_rails 不支持 xml(应该可以自定义)

version 'v1'
format :xml
content_type :xml, "application/xml;charset=UTF-8"
formatter :json, Grape::Formatter::Xml

可以在views中自己添加

module V1
  module Views
    module Weixin
      def xxxx
      end
    end
  end
end

从中引用

helpers Views::Weixin

def xxxx
    builder = Nokogiri::XML::Builder.new do |xml| #必须只有一个根目录??!
      xml.root {
        xml.ToUserName    "<![CDATA[%s]]>" % params['FromUserName']
        xml.FromUserName  "<![CDATA[%s]]>" % params['ToUserName']
        xml.MsgType       "<![CDATA[%s]]>" % "text"
        xml.CreateTime    "<![CDATA[%d]]>" % Time.now.to_i
        xml.Content       "<![CDATA[%s]]>" % "content"
      }
    end
    builder.doc.root.children
end