ruby RailsのAPIをswaggerでドキュメント化してみるref:http://qiita.com/kitaro_tn/items/ace80a36ac3cfc060788

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby RailsのAPIをswaggerでドキュメント化してみるref:http://qiita.com/kitaro_tn/items/ace80a36ac3cfc060788相关的知识,希望对你有一定的参考价值。

# Swagger settings
# baseのcontrollerの指定、変換パスの設定
class Swagger::Docs::Config
  def self.base_api_controller; ActionController::API end
  def self.transform_path(path, api_version); "apidocs/#{path}" end
end

# 出力JSON設定
Swagger::Docs::Config.register_apis({
  "v1" => {
    :api_extension_type => nil,
    :api_file_path => "public/apidocs/", # JSONが置かれるPATH
    :base_path => "http://localhost:3000/", # 最後の`/`が置換されてしまうのでURLを記載
    :clean_directory => true,
    :formatting => :pretty,
    :camelize_model_properties => false,
    :controller_base_path => "",
    :attributes => {
      :info => {
        "title"       => "Books API",
        "description" => "Books operation API",
        "contact"     => "tora.1986.tatsu@gmail.com",
        "license"     => "Apache 2.0",
        "licenseUrl"  => "http://www.apache.org/licenses/LICENSE-2.0.html"
      }
    }
  }
})

GrapeSwaggerRails.options.app_name    = "Books API"
# 基盤となるJSON
GrapeSwaggerRails.options.url         = "/apidocs/api-docs.json"
# こっちの`/`は置換されないのでこれでOK
GrapeSwaggerRails.options.app_url     = "/"
  mount GrapeSwaggerRails::Engine => "/swagger"
$ rails s
$ rake swagger:docs
  swagger_api :create do
    summary "Create a book image"
    consumes [ "application/json" ]
    param :body, :body, :object, :required, "Book image parameters", { "$ref": "BookImage" }
    response :ok, "Success", :BookImage
    response :not_found
    response :internal_server_error
  end


  swagger_model :BookImage do
    description "Book a object"
    property :id, :integer, :required, "Book image Id"
    property :book_id, :integer, :required, "Book Id"
    property :url, :string, :required, "Book image URL"
    property :alt, :string, :required, "Book image alt"
  end
  swagger_api :show do
    summary "Get a book"
    consumes [ "application/json" ]
    param :path, :id, :integer, :required, "Book Id"
    response :ok, "Success", :Book
    response :not_found
    response :internal_server_error
  end
  swagger_controller :Books, "Book"
//
//= require_tree .
/*
 *= require_tree .
 */
# grapeは使用しないが、中にswagger-uiのassets一式が入っているので、viewに利用
gem 'grape-swagger-rails', '~> 0.3.0' 
gem 'swagger-docs', '~> 0.2.9'

以上是关于ruby RailsのAPIをswaggerでドキュメント化してみるref:http://qiita.com/kitaro_tn/items/ace80a36ac3cfc060788的主要内容,如果未能解决你的问题,请参考以下文章

text APIアクセスキーを用いて,Ruby / RailsからRedmineのREST APIにアクセスする#memo #ruby#web-api #redmine

ruby 写真/动画の作成日/更新日情报を的Exifの情报に置换する

ruby 标准出力に変数の内容を展开する

ruby 配列から共通の要素を抜き出す

ruby RubyでWindowsとUNIXの标准出力とエラー出力を抑止する

ruby aws ec2のpublic ipを表示させる