Class: BananaDocs::Helpers

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
Defined in:
lib/banana_docs/helpers.rb

Constant Summary collapse

FIXED_COLUMNS =
['Parâmetro', 'Descrição', 'Tipo', 'Formato', 'Observações', 'Requerido?']

Instance Method Summary collapse

Constructor Details

#initializeHelpers

Returns a new instance of Helpers.



10
11
12
# File 'lib/banana_docs/helpers.rb', line 10

def initialize
  @navigations = []
end

Instance Method Details

#action(title, options) ⇒ Object



28
29
30
31
32
# File 'lib/banana_docs/helpers.rb', line 28

def action(title, options)
  (:h5, encode("Ação: #{title}")) +
  (:code, "#{options[:method]} #{options[:url]}") + 
  (:br)
end

#add_nav_item(title, anchor) ⇒ Object



69
70
71
# File 'lib/banana_docs/helpers.rb', line 69

def add_nav_item(title, anchor)
  @navigations << { title: title, anchor: "##{anchor}" }
end

#build_span(text, clazz) ⇒ Object



59
60
61
# File 'lib/banana_docs/helpers.rb', line 59

def build_span(text, clazz)
  (:span, encode(text), class: clazz)
end

#code(&block) ⇒ Object



95
96
97
# File 'lib/banana_docs/helpers.rb', line 95

def code(&block) 
  (:code, yield.html_safe).html_safe
end

#emptyObject



108
109
110
# File 'lib/banana_docs/helpers.rb', line 108

def empty
  "[]" 
end

#encode(string) ⇒ Object



120
121
122
123
# File 'lib/banana_docs/helpers.rb', line 120

def encode(string)
  @coder ||= HTMLEntities.new
  @coder.encode(string, :named).html_safe
end

#include_assetsObject



116
117
118
# File 'lib/banana_docs/helpers.rb', line 116

def include_assets
  render 'assets' 
end

#key_value(key, value, show_keys = true) ⇒ Object



99
100
101
102
# File 'lib/banana_docs/helpers.rb', line 99

def key_value(key, value, show_keys = true)
  value = value.starts_with?('[') ? value : "\"#{value}\""
  (show_keys ? "{ \"#{key}\":#{value} }" : "\"#{key}\": #{value}, ").html_safe
end

#key_with_block(key, &block) ⇒ Object



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

def key_with_block(key, &block) 
  "{ \"#{key}\": { #{yield} } }".html_safe
end

#moreObject



112
113
114
# File 'lib/banana_docs/helpers.rb', line 112

def more
  "..."
end

#note(text) ⇒ Object



63
64
65
66
67
# File 'lib/banana_docs/helpers.rb', line 63

def note(text)
  (:blockquote) do 
    (:p, encode(text), style: 'font-size: 14px')
  end
end

#param(name, description, type, format, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/banana_docs/helpers.rb', line 45

def param(name, description, type, format, options = {})
  obs = options[:obs].nil? ? 'Sem observações' : options[:obs]
  required, clazz = options[:required] ? ['Sim', 'badge badge-warning'] : ['Não', 'badge badge-info']
  (:tr) do
    (:td, build_span(name, 'badge badge-default')) + 
    (:td, encode(description)) +
    (:td, encode(type)) +
    (:td, encode(format)) +
    (:td, encode(obs)) +
    (:td, build_span(required, clazz))
    #options...
  end
end

#params(&block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/banana_docs/helpers.rb', line 34

def params(&block)
  (:table, class: 'table table-striped') do
    (:thead) do
      (:tr) do
        FIXED_COLUMNS.map{ |th| (:th, encode(th)) }.reduce(:+)
      end
    end + 
    (:tbody, yield.html_safe)
  end
end

#render(view) ⇒ Object



14
15
16
17
18
19
# File 'lib/banana_docs/helpers.rb', line 14

def render(view)
  view = view.sub(/(\.slim|)$/, '.slim')
  banana_views_dir = File.join Gem::Specification.find_by_name('banana_docs').gem_dir, 'views'
  view = File.join(banana_views_dir, view) unless File.exists?(view)
  Slim::Template.new(view).render(self).html_safe
end

#response(&block) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/banana_docs/helpers.rb', line 80

def response(&block)
  (:hr).html_safe + 
  (:div) do 
    (:span, 'Exemplos de respostas', class: 'badge badge-info').html_safe + 
    (:br) +
    yield.html_safe
  end
end

#section(title, options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/banana_docs/helpers.rb', line 21

def section(title, options = {})
  anchor = title.parameterize
  add_nav_item(title, anchor)
  title =  :h3, encode(title)
  link_to(title, '#', name: anchor)  
end

#show_nav_menuObject



73
74
75
76
77
78
# File 'lib/banana_docs/helpers.rb', line 73

def show_nav_menu
  (:ul, class: 'nav nav-list') do 
    (:li, encode('Ações'), class: 'nav-header') +
    @navigations.map { |nav_item| (:li, link_to(encode(nav_item[:title]), nav_item[:anchor])).html_safe }.reduce(:+)
  end
end

#status_code(status) ⇒ Object



89
90
91
92
93
# File 'lib/banana_docs/helpers.rb', line 89

def status_code(status)
  status_codes = { '200' => 'success', '400' => 'important', '404' => 'important' }
  status = (:span, status, class: "label label-#{status_codes[status]}")
  "Status HTTP: #{status} #{(:br)}".html_safe    
end