Class: GrapeDocumenter::Formatters::Textile

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_documenter/formatters/textile.rb

Overview

Textile

Instance Method Summary collapse

Constructor Details

#initialize(structure) ⇒ Textile

Returns a new instance of Textile.



7
8
9
# File 'lib/grape_documenter/formatters/textile.rb', line 7

def initialize(structure)
  @structure = structure
end

Instance Method Details

#formatObject



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
# File 'lib/grape_documenter/formatters/textile.rb', line 11

def format
  doc = @structure

  output = "h1. #{doc.title}"
  output << "\n\n"
  output << "\n\n"

  doc.routes.each do |route|
    output << "\n\n"
    output << "h2. #{route.inferred_title}"
    output << "\n\n"

    output << "\n\n"
    output << "h3. #{route.http_method}: #{route.path.gsub(':version', doc.version)}"
    output << "\n\n"

    if route.description.present?
      output << "\n\n"
      output << route.description
      output << "\n\n"
    end

    if route.params.present?
      output << "h4. Required Parameters"
      output << "\n\n"
      output << tabulate_params(route.params)
      output << "\n\n"
    end

    if route.optional_params.present?
      output << "h4. Optional Parameters"
      output << "\n\n"
      output << tabulate_params(route.optional_params)
      output << "\n\n"
    end

    output << 'h4. Example Request'
    output << "\n\n"
    output << "#{doc.root_path}__request__#{route.http_method.downcase}__#{route.inferred_rails_action}"
    output << "\n\n"

    output << 'h4. Example Response'
    output << "\n\n"
    output << "#{doc.root_path}__response__#{route.http_method.downcase}__#{route.inferred_rails_action}"
    output << "\n\n"
  end

  output
end

#tabulate_params(params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/grape_documenter/formatters/textile.rb', line 61

def tabulate_params(params)
  string = "table(parameters).\n"
  string += "|_.Name|_.Type|_.Description|\n"

  params.each do |k,v|
    v = {:desc => v} unless v.is_a?(Hash)
    string << "|\\3. #{k}|\n||#{v[:type]}|#{v[:desc]}|\n"
  end

  string
end