Class: Hyperdrive::Docs

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperdrive/docs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources) ⇒ Docs

Returns a new instance of Docs.



7
8
9
# File 'lib/hyperdrive/docs.rb', line 7

def initialize(resources)
  @resources = resources
end

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



5
6
7
# File 'lib/hyperdrive/docs.rb', line 5

def resources
  @resources
end

Instance Method Details

#bold(string) ⇒ Object



36
37
38
# File 'lib/hyperdrive/docs.rb', line 36

def bold(string)
  "__#{string}__"
end

#bullet(string, nest = 1) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
# File 'lib/hyperdrive/docs.rb', line 48

def bullet(string, nest = 1)
  raise ArgumentError, "Nest level must be between 1 and 3." unless (1..3).cover?(nest)
  nest = "  " * nest
  "#{nest}- #{string}\n"
end

#code(string) ⇒ Object



44
45
46
# File 'lib/hyperdrive/docs.rb', line 44

def code(string)
  "`#{string}`"
end

#code_options(options) ⇒ Object



71
72
73
# File 'lib/hyperdrive/docs.rb', line 71

def code_options(options)
  options.map { |option| code(option) }.join(", ")
end

#header(string, level = 1) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/hyperdrive/docs.rb', line 26

def header(string, level = 1)
  raise ArgumentError, "Header level must be between 1 and 6." unless (1..6).cover?(level)
  header = "#" * level
  "\n\n#{header} #{string}\n\n"
end

#italics(string) ⇒ Object



40
41
42
# File 'lib/hyperdrive/docs.rb', line 40

def italics(string)
  "_#{string}_"
end

#list(items) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hyperdrive/docs.rb', line 54

def list(items)
  list = ""
  items.each do |item|
    list += bullet(bold(item[:name]), 1)
    item.each do |key, value|
      list += bullet(italics(key), 2)

      if value.kind_of? Array
        list += bullet(code_options(value), 3)
      else
        list += bullet(value, 3)
      end
    end
  end
  list
end

#outputObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hyperdrive/docs.rb', line 11

def output
  out = ""
  resources.each_value do |resource|
    out += header(resource.name)
    out += paragraph(resource.description)
    out += header("Endpoint URL", 2)
    out += paragraph(bullet(code(resource.endpoint), 1))
    out += header("Params", 2)
    out += list(resource.params.map { |_,param| param.to_hash })
    out += header("Filters", 2)
    out += list(resource.filters.map { |_,filter| filter.to_hash })
  end
  out
end

#paragraph(string) ⇒ Object



32
33
34
# File 'lib/hyperdrive/docs.rb', line 32

def paragraph(string)
  "#{string}\n\n"
end