Module: Useless::Doc::Server::Sinatra

Defined in:
lib/useless/doc/server/sinatra.rb

Overview

Provides access to the Doc::DSL via the .doc method. The JSON of the API doc that is built will be served via an OPTIONS request to the root. Resource documentation is similarly served via an OPTIONS request to the corresponding path.

Examples:

class ResourceApp < Sinatra::Base
  register Useless::Doc::Sinatra

  doc 'Resouces API' do
    url 'resources.useless.io'
    description 'A place with resources'
  end

  doc.get '/some-resources' do
    description 'Get all of these resources'

    request do
      parameter 'page', 'The page of resources to return.'
    end

    response do
      body do
        attribute 'name', 'The name of the resource.'
      end
    end
  end

  ...

end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/useless/doc/server/sinatra.rb', line 57

def self.registered(app)
  app.options '/' do
    if api = self.class.generated_doc
      last_modified api.timestamp
      Useless::Doc::Serialization::Dump.api(api)
    end
  end

  app.options '/*' do
    if api = self.class.generated_doc
      resource = api.resources.find do |resource|
        resource.path == "/#{params[:splat].first}"
      end

      if resource
        last_modified api.timestamp
        return Useless::Doc::Serialization::Dump.resource(resource)
      end
    end

    pass
  end
end

Instance Method Details

#doc(name = nil, &block) ⇒ Object



47
48
49
50
51
# File 'lib/useless/doc/server/sinatra.rb', line 47

def doc(name = nil, &block)
  @dsl ||= Useless::Doc::DSL::API.new(name: name)
  @dsl.instance_eval(&block) if block_given?
  @dsl
end

#doc=(doc) ⇒ Object



43
44
45
# File 'lib/useless/doc/server/sinatra.rb', line 43

def doc=(doc)
  @doc = doc
end

#generated_docObject



53
54
55
# File 'lib/useless/doc/server/sinatra.rb', line 53

def generated_doc
  @doc ||= @dsl.generate if @dsl
end