Last active
August 29, 2015 14:02
-
-
Save mje113/394af67e00451b096ae2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'rack/test' | |
require 'swagger/docs' | |
Minitest.after_run { | |
puts Swagger.to_doc | |
} | |
module Swagger | |
@apis = [] | |
def self.register(resource_listing) | |
@resource_listing = resource_listing | |
end | |
def self.apis | |
@apis | |
end | |
def self.to_doc | |
rl = ResourceListing.new(@resource_listing[:api_version], @resource_listing[:info]) | |
rl.apis = @apis | |
rl.to_doc | |
end | |
class ResourceListing | |
attr_accessor :apis | |
def initialize(api_version = '1.0', info = '') | |
@swagger_version = '1.2' | |
@apis = [] | |
@api_version = api_version | |
@info = info | |
@authorizations = [] | |
end | |
def to_doc | |
MultiJson.dump( | |
swaggerVersion: @swagger_version, | |
apis: @apis.map(&:to_doc), | |
apiVersion: @api_version, | |
info: @info, | |
authorizations: @authorizations | |
) | |
end | |
end | |
class Resource | |
def initialize | |
@operations = [] | |
end | |
def add_operation(operation) | |
@operations << Operation.new(operation) | |
end | |
def to_doc | |
{ | |
path: "I'm an Api", | |
operations: @operations.map(&:to_doc) | |
} | |
end | |
end | |
class Operation | |
def initialize(operation) | |
@operation = operation | |
end | |
def to_doc | |
@operation.to_s | |
end | |
end | |
class Test < Minitest::Test | |
include Rack::Test::Methods | |
module Assertions | |
require 'json-schema' | |
def assert_valid_json_schema(schema, json) | |
assert JSON::Validator.validate(schema, json) | |
end | |
end | |
include Assertions | |
def self.inherited(klass) | |
super | |
klass.instance_variable_set(:@resource, Resource.new) | |
Swagger.apis << klass | |
end | |
def self.resource | |
@resource | |
end | |
def self.to_doc | |
resource.to_doc | |
end | |
def swagger(operation, &block) | |
block.call | |
add_operation(operation) | |
end | |
def summary(str) | |
end | |
def notes(str) | |
end | |
def schema(json) | |
open(File.expand_path("../schema/#{json}", __FILE__)).read | |
end | |
private | |
def add_operation(operation) | |
self.class.resource.add_operation(operation) | |
end | |
end | |
end | |
This file contains hidden or 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
require 'test_helper' | |
class TestAccountCreation < Swagger::Test | |
def test_basic_creation | |
swagger(:create) do | |
summary 'Create Account' | |
notes 'This creates a new account' | |
post '/account?partner=cox', MultiJson.dump(body) | |
assert last_response.ok? | |
assert_valid_json_schema schema('account.json'), last_response.body | |
end | |
end | |
end |
This file contains hidden or 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
Swagger.register( | |
api_extension_type: :json, | |
api_file_path: 'public/api/v1/', | |
base_uri: 'http://api.somedomain.com', | |
api_version: '1.0', | |
info: '' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment