Module: Joshua::Doc

Extended by:
Doc
Included in:
Doc
Defined in:
lib/doc/doc.rb

Instance Method Summary collapse

Instance Method Details

#icon(data, size: 24, color: nil, style: nil) ⇒ Object

render single icon



109
110
111
# File 'lib/doc/doc.rb', line 109

def icon data, size: 24, color: nil, style: nil
  %[<svg style="width: #{size}px; height: #{size}px; #{style}" viewBox="0 0 24 24" fill="currentColor">#{data}</svg>]
end

#indexObject

render doc for all documented classes



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/doc/doc.rb', line 139

def index
  HtmlTag.div do |n|
    for @klass in Joshua.documented
      @opts = @klass.opts
      icon = @opts.dig(:opts, :icon)

      n._sticky(style: 'background: #f7f7f7; padding-bottom: 5px; padding-top: 30px; margin-top: 2px;') do |n|
        n.push name_link @klass, 40
        n.push self.icon icon, style: 'position: absolute; margin-left: -40px; margin-top: 1px; fill: #777; background: #f7f7f7;' if icon
        n.h4 { @klass.to_s.sub(/Api$/, '') }
      end

      if desc = @opts.dig(:opts, :desc)
        n.p { desc }
      end

      if detail = @opts.dig(:opts, :detail)
        n.p { detail }
      end

      n.push render_type :member
      n.push render_type :collection

      n.br
      n.hr
      n.br
    end
  end
end

#left_navObject

left side navigation



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/doc/doc.rb', line 124

def left_nav
  HtmlTag.div do |n|
    Joshua.documented.each do |name|
      n.a({ class:'btn btn-outline-info btn-sm', style: '-font-size: 14px; margin-bottom: 10px;', href: '#%s' % name}) do |n|
        icon = name.opts.dig(:opts, :icon)
        n.push self.icon icon, size: 20 if icon
        n.push name.to_s.sub(/Api$/, '')
      end

      n.br
    end
  end
end

#list_errorsObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/doc/doc.rb', line 228

def list_errors
  HtmlTag.div do |n|
    n.push name_link :api_errors
    n.push icon ICONS[:error][:image], style: 'position: absolute; margin-left: -40px; margin-top: 1px; fill: #777;'
    n.h4 { 'Named errors' }

    n._box do |n|
      if RESCUE_FROM.keys.length == 0
        n.p 'No named errors defiend via'
        n.code "rescue from :name, 'Error description'"
      end

      n._row({ style: 'margin-bottom: 30px;' }) do |n|
        for key, desc in RESCUE_FROM
          next if key == :all
          next unless key.is_a?(Symbol) && desc.is_a?(String)

          n._col_4 { "<code>#{key}</code>" }
          n._col_8 { desc }
        end
      end
    end
  end
end

#misc_file(name) ⇒ Object



26
27
28
# File 'lib/doc/doc.rb', line 26

def misc_file name
  File.read [__dir__, '../misc/%s' % name].join('/')
end


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/doc/doc.rb', line 253

def modal_dialog
  %[
    <script>#{misc_file('doc.js')}</script>
    <div id="modal" class="modal" tabindex="-1" role="dialog">
      <div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(99,99,99,0.3)"></div>
      <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title"></h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="Modal.close()">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
          </div>
        </div>
      </div>
    </div>
  ]
end

anchor link



104
105
106
# File 'lib/doc/doc.rb', line 104

def name_link name, top=nil
  %[<a name="#{name}" class="anchor" style="top: #{top || -95}px;"></a>]
end

#render(mount_on: nil, request: nil, bearer: nil) ⇒ Object

render full page



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/doc/doc.rb', line 31

def render mount_on: nil, request: nil, bearer: nil
  mount_on ||= request.url.split('?').first+'/'
  mount_on.sub! %r{//$}, '/'

  HtmlTag.html do |n|
    n.tag(:head) do |n|
      n.title 'Joshua Tester'
      n.link({ href: "https://fonts.googleapis.com/css?family=Inter:300,400,500,600,700,800,900&display=swap",  rel:"stylesheet" })
      n.link({ rel:"stylesheet", href:"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" })
      n.script({ src: 'https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js' })
      n.script %[window.api_opts = { mount_on: '#{mount_on}', bearer: '#{bearer}' }]
    end
    n.tag(:body) do |n|
      n.style { misc_file('doc.css') }
      n.header({ style: 'border-bottom: 1px solid rgb(228, 228, 228);'}) do |n|
        n._container do |n|
          n.push top_icons
          n.push %[<button id="bearer_button" onclick="AuthButton.set()" class="btn btn-sm btn-outline-primary" style="float: right; margin-top: 15px; margin-right: 20px;">-</button>]
          n.h1({ class: :nav}) { %[<a href="#top">Joshua &nbsp; <gray>Docs</gray></a>] }
        end
      end

      n.img src:"https://camo.githubusercontent.com/4b5b5447d6af920f58a030740bc8e9cf52dc490a8a560492c6ad3a5c6a0ad076/68747470733a2f2f692e696d6775722e636f6d2f48576f557a356b2e706e67", style: 'width: 40px; z-index: 1; position: absolute; top: 10px; left: 50%;', onclick: "window.open('https://github.com/dux/joshua')"

      n.push modal_dialog

      n._container do |n|
        n._row do |n|
          n._col_3 do |n|
            n._sticky(style: 'padding-top: 30px;') do |n|
              n.a({ class: :dark, href: '#top' }) { '<p><b>API OBJECTS</b></p>' }
              n.push left_nav

              n.br
              n.br

              n.p '<b>TOOLS</b>'
              n.div do |n|
                n.push %[<p><a class="badge badge-light" href="#api_errors">Named errors</a></p>]
                n.push %[<p><a class="badge badge-light" href="#{mount_on}_/postman" target="capi_postman">Postman/Insomnija import URL</a></p>]
                n.push %[<p><a class="badge badge-light" href="#{mount_on}_/raw" target="capi_raw">Raw doc data</a></p>]
              end

              n.br

              n.p '<b>API LIBRARIES</b>'
              n.div do |n|
                n.push %[<a class="badge badge-light" href="https://github.com/dux/joshua/tree/master/client/ruby" target="capi_ruby">Ruby</a>]
                n.push %[<a class="badge badge-light" href="https://github.com/dux/joshua/tree/master/client/javascript" target="capi_js">Javascript</a>]
                n.push %[<a class="badge badge-light" href="#">Python</a>]
                n.push %[<a class="badge badge-light" href="#">C#</a>]
              end

              n.br

              n.p '<b>RESOURCES</b>'
              n.div do |n|
                n.push %[<a class="badge badge-light" href="http://vmrcre.org/web/scribe/home/-/blogs/why-rest-sucks" target="capi_why">Why we only prefer POST?</a>]
              end
            end
          end

          n._col_9 do |n|
            n.push index
            n.push list_errors
          end
        end
      end
    end
  end
end

#render_method(name:, m_name:, opts:) ⇒ Object

render api method



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/doc/doc.rb', line 186

def render_method name:, m_name:, opts:
  HtmlTag._box do |n|
    # n.push %[<button onclick="" class="btn btn-info btn-sm request">request</button>]
    anchor = [@klass, m_name].join('-')

    n.push name_link anchor
    n.h5 style: 'margin-bottom: 20px;' do |n|
      n.push "<a href='##{anchor}'>#{m_name}</a>"
      n.push ' <gray>&nbsp; &mdash; &nbsp; %s</gray>' % opts[:desc] if opts[:desc]
    end

    if opts[:detail]
      n.h6 'Details'
      n.pre opts[:detail]
    end

    if mopts = opts[:params]
      n.h6 'Params'
      n.ul do |n|
        for name, opt in mopts
          n.li do |n|
            n.push '<bold>%s</bold>: ' % name
            n.push opt[:type]

            data = []
            data.push 'required' if opt[:required]
            data.push 'default: %s' % opt[:default].to_s unless opt[:default].nil?
            n.push ' &mdash; (%s)' % data.join(', ') if data.length > 0
          end
        end
      end
    end

    n.p style: 'margin: 30px 0 10px 0;' do |n|
      path = @klass.api_path
      path += '/:id' if name == :member
      path += "/#{m_name}"
      n.push %[<button href="#{path}" class="btn btn-outline-info btn-sm" onclick="ModalForm.render(api_opts.mount_on+this.innerHTML, #{(opts[:params] || {}).to_json.gsub('"', '&quot;')})">#{path}</button>]
    end
  end
end

#render_type(name) ⇒ Object

render members or collection



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/doc/doc.rb', line 170

def render_type name
  base = @opts[name] || return

  HtmlTag.div do |n|
    n.br
    n.h5 '<gray>%s methods</gray>' % name

    for m_name, member in base
      n.div do |n|
        n.push render_method name: name, m_name: m_name, opts: member
      end
    end
  end
end

#top_iconsObject

top side navigation icons



114
115
116
117
118
119
120
121
# File 'lib/doc/doc.rb', line 114

def top_icons
  tag.div style: 'float: right; margin-top: 18px;' do
    for icon in ICONS.values
      next unless icon[:url]
      a icon(icon[:image]), target: '_new', href: icon[:url]
    end
  end
end