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', 'Obrigatório?']

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(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



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

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

#emptyObject



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

def empty
  "[]" 
end

#encode(string) ⇒ Object



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

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

#include_assetsObject



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

def include_assets
  render 'assets' 
end

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



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

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

#key_with_block(key, &block) ⇒ Object



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

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

#moreObject



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

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? ? '-' : options[:obs]
  required, clazz = options[:required] ? ['Sim', 'badge badge-important'] : ['Não', 'badge badge-info']
  (:tr) do
    (:td, build_span(name, 'label label-inverse')) + 
    (: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
# File 'lib/banana_docs/helpers.rb', line 80

def response(&block)
  (:div) do 
    (:span, (:i, "", class: 'icon-chevron-right').html_safe + 'Exemplos de respostas', class: 'response') +
    (: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



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

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