Skip to content

Instantly share code, notes, and snippets.

@muffinista
Created August 28, 2018 18:51
Show Gist options
  • Save muffinista/5192c376145d8472527243d78cb3e4fe to your computer and use it in GitHub Desktop.
Save muffinista/5192c376145d8472527243d78cb3e4fe to your computer and use it in GitHub Desktop.
mastodon swagger example
{
"basePath": "/api",
"consumes": [
"application/json"
],
"definitions": {
"Status": {
"properties": {
"id": {
"format": "int64",
"type": "integer"
},
"text": {
"type": "string"
}
},
"required": [
"id"
]
}
},
"host": "mastodon.social",
"info": {
"description": "A proof-of-concept of a Mastodon Swagger API definition",
"title": "Mastodon",
"version": "1.0.0"
},
"paths": {
"/api/v1/statuses/{id}": {
"get": {
"description": "Returns a single status if the user has access",
"operationId": "findStatusById",
"parameters": [
{
"description": "ID of status to fetch",
"format": "int64",
"in": "path",
"name": "id",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "status response",
"schema": {
"$ref": "#/definitions/Statuss"
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/ErrorModel"
}
}
},
"summary": "Find Status by ID",
"tags": [
"status"
]
}
}
},
"produces": [
"application/json"
],
"swagger": "2.0"
}
diff --git a/Gemfile b/Gemfile
index bdac00b48..e339566b6 100644
--- a/Gemfile
+++ b/Gemfile
@@ -145,3 +145,5 @@ group :production do
gem 'lograge', '~> 0.10'
gem 'redis-rails', '~> 5.0'
end
+
+gem 'swagger-blocks', '~> 2.0.2'
diff --git a/Gemfile.lock b/Gemfile.lock
index 502d08efe..1294a3ef5 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -584,6 +584,7 @@ GEM
multi_json (~> 1.8)
strong_migrations (0.2.2)
activerecord (>= 3.2.0)
+ swagger-blocks (2.0.2)
temple (0.8.0)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
@@ -752,6 +753,7 @@ DEPENDENCIES
stoplight (~> 2.1.3)
streamio-ffmpeg (~> 3.0)
strong_migrations (~> 0.2)
+ swagger-blocks (~> 2.0.2)
thor (~> 0.20)
tty-command (~> 0.8)
tty-prompt (~> 0.16)
@@ -765,4 +767,4 @@ RUBY VERSION
ruby 2.5.0p0
BUNDLED WITH
- 1.16.3
+ 1.16.4
diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb
index 49a52f7a6..e8267d5b3 100644
--- a/app/controllers/api/v1/statuses_controller.rb
+++ b/app/controllers/api/v1/statuses_controller.rb
@@ -2,7 +2,8 @@
class Api::V1::StatusesController < Api::BaseController
include Authorization
-
+ include Swagger::Blocks
+
before_action -> { authorize_if_got_token! :read, :'read:statuses' }, except: [:create, :destroy]
before_action -> { doorkeeper_authorize! :write, :'write:statuses' }, only: [:create, :destroy]
before_action :require_user!, except: [:show, :context, :card]
@@ -16,6 +17,37 @@ class Api::V1::StatusesController < Api::BaseController
# than this anyway
CONTEXT_LIMIT = 4_096
+
+ swagger_path '/api/v1/statuses/{id}' do
+ operation :get do
+ key :summary, 'Find Status by ID'
+ key :description, 'Returns a single status if the user has access'
+ key :operationId, 'findStatusById'
+ key :tags, [
+ 'status'
+ ]
+ parameter do
+ key :name, :id
+ key :in, :path
+ key :description, 'ID of status to fetch'
+ key :required, true
+ key :type, :integer
+ key :format, :int64
+ end
+ response 200 do
+ key :description, 'status response'
+ schema do
+ key :'$ref', :Statuss
+ end
+ end
+ response :default do
+ key :description, 'unexpected error'
+ schema do
+ key :'$ref', :ErrorModel
+ end
+ end
+ end
+ end
def show
@status = cache_collection([@status], Status).first
render json: @status, serializer: REST::StatusSerializer
diff --git a/app/models/status.rb b/app/models/status.rb
index 35655bff2..9bc11c4b8 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -28,7 +28,8 @@ class Status < ApplicationRecord
include Streamable
include Cacheable
include StatusThreadingConcern
-
+ include Swagger::Blocks
+
# If `override_timestamps` is set at creation time, Snowflake ID creation
# will be based on current time instead of `created_at`
attr_accessor :override_timestamps
@@ -104,6 +105,18 @@ class Status < ApplicationRecord
REAL_TIME_WINDOW = 6.hours
+ swagger_schema :Status do
+ key :required, [:id]
+ property :id do
+ key :type, :integer
+ key :format, :int64
+ end
+ property :text do
+ key :type, :string
+ end
+ end
+
+
def searchable_by(preloaded = nil)
ids = [account_id]
diff --git a/config/routes.rb b/config/routes.rb
index 0e54157dc..a45dcbba1 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -40,6 +40,9 @@ Rails.application.routes.draw do
get '/users/:username', to: redirect('/@%{username}'), constraints: lambda { |req| req.format.nil? || req.format.html? }
+
+ resources :apidocs, only: [:index]
+
resources :accounts, path: 'users', only: [:show], param: :username do
resources :stream_entries, path: 'updates', only: [:show] do
member do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment