aboutsummaryrefslogtreecommitdiff
path: root/src/guff/api-docs-html-view.cr
blob: 3ee1e340f0c50c92375ba4ea4c05d72b4cb588b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require "html"
require "ecr/macros"
require "./page-html-view"
require "./api/methods"

module Guff
  class APIDocsHTMLView
    TITLE = "Guff API Documentation"

    def self.run(context : HTTP::Server::Context)
      page = PageHTMLView.new(TITLE, new.to_s)
      context.response.content_type = page.content_type
      context.response.puts page
    end

    private def namespaces
      API::Methods::METHODS.keys.sort
    end

    private def methods(
      namespace : String
    )
      API::Methods::METHODS[namespace].keys.sort
    end

    private def method_text(
      namespace : String,
      method    : String
    )
      API::Methods::METHODS[namespace][method][:text] as String
    end

    private def method_args(
      namespace : String,
      method    : String
    )
      if method_has_args?(namespace, method)
        args = API::Methods::METHODS[namespace][method][:args] as \
          Hash(String, Hash(Symbol, String|Symbol|Bool)) | Nil
        args.keys.sort
      else
        [] of String
      end
    end

    private def arg_text(
      namespace : String,
      method    : String,
      name      : String
    ) : String
      if method_has_args?(namespace, method)
        arg = API::Methods::METHODS[namespace][method][:args][name] as\
          Hash(Symbol, String|Symbol|Bool)
        arg[:text] as String
      else
        ""
      end
    end

    private def method_has_args?(
      namespace : String,
      method    : String
    )
      API::Methods::METHODS[namespace][method].has_key?(:args)
    end

    def h(s : String)
      HTML.escape(s || "")
    end

    ECR.def_to_s("./src/guff/views/ecrs/api-docs.ecr")
  end
end