class Sinatra::DocDsl::PageDoc

Attributes

entries[RW]
the_header[RW]
the_introduction[RW]
the_title[RW]
the_url_prefix[RW]

Public Class Methods

new(&block) click to toggle source
# File lib/docdsl.rb, line 10
def initialize(&block)
  @the_title='DocDSL Documentation'
  @the_header="API"
  @the_url_prefix=""
  @the_introduction="API Documentation for this resource"
  @the_footer='Powered by <strong><a href="https://github.com/jillesvangurp/sinatra-docdsl">Sinatra DocDSL</a></strong>'
  configure_renderer do
    # default
    self.render_md
  end
  if(block)
    if block.arity == 1
      block(self)
    else
      instance_eval(&block)
    end
  end
end

Public Instance Methods

configure_renderer(&block) click to toggle source
# File lib/docdsl.rb, line 45
def configure_renderer(&block)
  @render_function=block
end
definition_list(title, definitions) click to toggle source
# File lib/docdsl.rb, line 70
      def definition_list(title, definitions)
        if definitions.length > 0
          definitions.inject("### #{title}\n\n") do | dl, (k,v) |
            dl << "
#{k}
:  #{v}
"
          end
        else
          ''
        end
      end
header(h) click to toggle source
# File lib/docdsl.rb, line 33
def header(h)
  @the_header=h
end
html() click to toggle source
# File lib/docdsl.rb, line 191
      def html
        begin
          body= <<-HTML
            <html>
              <head>
                <title>#{@the_title}</title>
                <style type="text/css">
                  #container{width:960px; margin:1em auto; font-family:monaco, monospace;font-size:11px;}
                  dt{ background:#f5f5f5; font-weight:bold; float:left; margin-right:1em; }
                  dd{ margin-left:1em; }
                </style>
              </head>
              <body>
                <div id="container">
                  <h1 id="title">#{@the_header}</h1>
                  <p>#{@the_introduction}</p>

                  #{render_html_entries}
                  <br/>
                  <hr>
                  <p>#{@the_footer}</p>
                </div>
              </body>
            </html>
          HTML
          [200,{'content-type' => 'text/html;charset=UTF8'},body]
        rescue => e
          [500,"oops, #{e.to_s}\n#{e.backtrace}"]
        end
      end
introduction(i) click to toggle source
# File lib/docdsl.rb, line 41
def introduction(i)
  @the_introduction=i
end
json() click to toggle source
# File lib/docdsl.rb, line 57
def json
  entries=[]
  @entries.each do |entry|
    entries << entry.json
  end
  object={
    :title=> @the_title,:header=>@the_header,:footer=>@the_footer,:introduction=>@the_introduction,
    :endPoints=>entries
  }

  [200,{'content-type' => 'application/json;charset=UTF8'},object.to_json]
end
md() click to toggle source
# File lib/docdsl.rb, line 109
def md
  [200,{'content-type' => 'text/plain;charset=UTF8'},to_markdown]
end
render() click to toggle source
# File lib/docdsl.rb, line 49
def render
  @render_function.call
end
render_html_entries() click to toggle source
# File lib/docdsl.rb, line 222
def render_html_entries
  @entries.inject('') { | markup, entry|
    path = entry.paths.join(', ')
    if entry.params.length >0
      params = entry.params.inject("<h3>Url Parameters</h3>\n<dl>") { |li,(k,v)|
        li << "<dt>:%s</dt><dd>%s</dd>" % [k,v]
      }
      params << "</dl>\n"
    end
    params ||= ''

    if entry.query_params.length >0
      query_params = entry.query_params.inject("<h3>Query Parameters</h3>\n<dl>") { |li,(k,v)|
        li << "<dt>:%s</dt><dd>%s</dd>" % [k,v]
      }
      query_params << "</dl>\n"
    end
    query_params ||=''


    if entry.headers.length >0
      headers = entry.headers.inject("<h3>Header Parameters</h3>\n<dl>") { |li,(k,v)|
        li << "<dt>%s</dt><dd>%s</dd>" % [k,v]
      }
      headers << "</dl>\n"
    end
    headers ||= ''

    if entry.the_payload
      payload="<dt>Payload</dt><dd>#{entry.the_payload}\n"
      if(entry.sample_request)
        payload << "<pre>#{::JSON.pretty_generate(entry.sample_request)}</pre>"
      end
      payload << "</dd>"
    end
    payload ||=''
    if entry.the_response
      statuscodes=''
      if entry.status_codes.length >0
        status_codes="<dl>\n"
        entry.status_codes.each do |status,meaning|
          statuscodes << "<dt>#{status}</dt><dd>#{meaning}</dd>\n"
        end
        status_codes << "</dl>\n"
      end

      response="<dt>Response</dt><dd>#{entry.the_response}\n#{statuscodes}\n"
      if(entry.sample_response)
        response << "<pre>#{::JSON.pretty_generate(entry.sample_response)}</pre>"
      end
      response << "</dd>"
    end
    response ||=''

    markup << "<h2>%s</h2>\n<p>%s</p>\n%s%s%s%s%s" % [path, entry.desc, params, query_params, headers,payload,response]
  } << ""
end
render_md() click to toggle source
# File lib/docdsl.rb, line 83
      def render_md
        begin
          html=Kramdown::Document.new(to_markdown).to_html
          body= <<-HTML
<html>
  <head>
    <title>#{@the_title}</title>
    <style type="text/css">
      #container{width:960px; margin:1em auto; font-family:monaco, monospace;font-size:11px;}
      dt{ background:#f5f5f5; font-weight:bold; float:left; margin-right:1em; }
      dd{ margin-left:1em; }
    </style>
  </head>
  <body>
    <div id="container">
      #{html}
    </div>
  </body>
</html>
HTML
          [200,{'content-type' => 'text/html;charset=UTF8'},body]
        rescue => e
          [500,"oops, #{e.to_s}\n#{e.backtrace}"]
        end
      end
title(t) click to toggle source
# File lib/docdsl.rb, line 29
def title(t)
  @the_title=t
end
to_markdown() click to toggle source
# File lib/docdsl.rb, line 113
      def to_markdown
        markdown="
#{@the_header}

# #{@the_title}

#{@the_introduction}

"
        markdown = @entries.inject(markdown) do | md, entry |
          path = entry.paths.join(', ')
          params = definition_list("Url Parameters", entry.params)
          query_params = definition_list("Query Parameters", entry.query_params)
          header_params = definition_list("Header Parameters", entry.headers)

          if entry.the_payload
            payload="
### Request body

#{entry.the_payload}

"
            if(entry.sample_request)
              payload << "
~~~ javascript
#{::JSON.pretty_generate(entry.sample_request)}
~~~

"
            end
          end
          payload ||=''


          if entry.the_response
            response="
### Response
#{entry.the_response}

"
            if(entry.sample_response)
              response << "
~~~ javascript
#{::JSON.pretty_generate(entry.sample_response)}
~~~

"
            end
          end
          response ||=''
          status_codes=definition_list("Status codes", entry.status_codes)


          md << "
## #{path}

#{entry.desc}

#{params}

#{query_params}

#{header_params}

#{payload}

#{response}

#{status_codes}
"
        end

        markdown << "
#{@the_footer}
"
        markdown
      end
url_prefix(prefix) click to toggle source
# File lib/docdsl.rb, line 53
def url_prefix(prefix)
  @the_url_prefix=prefix
end