Class: Rain::DocPart

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocPart

Returns a new instance of DocPart.



5
6
7
8
9
10
11
# File 'lib/doc_part.rb', line 5

def initialize
  self.responses = []
  self.doc = []
  self.route = '//'
  self.params = []
  self.headers = []
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



3
4
5
# File 'lib/doc_part.rb', line 3

def doc
  @doc
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/doc_part.rb', line 3

def headers
  @headers
end

#http_methodObject

Returns the value of attribute http_method.



3
4
5
# File 'lib/doc_part.rb', line 3

def http_method
  @http_method
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/doc_part.rb', line 3

def params
  @params
end

#responsesObject

Returns the value of attribute responses.



3
4
5
# File 'lib/doc_part.rb', line 3

def responses
  @responses
end

#routeObject

Returns the value of attribute route.



3
4
5
# File 'lib/doc_part.rb', line 3

def route
  @route
end

Instance Method Details

#append_doc(text) ⇒ Object

appends the text to the current documentation for the part



72
73
74
# File 'lib/doc_part.rb', line 72

def append_doc(text)
  self.doc << text
end

#append_header(name, description) ⇒ Object

adds a http header with name and description of the header



111
112
113
114
115
116
# File 'lib/doc_part.rb', line 111

def append_header(name, description)
  self.headers << {
    name: name,
    text: description
  }
end

#append_param(name, description, type, default = nil) ⇒ Object

adds a parameter with the specified type. also sets a default value if specified



94
95
96
97
98
99
100
101
# File 'lib/doc_part.rb', line 94

def append_param(name, description, type, default = nil)
  self.params << {
    name: name,
    text: description,
    type: type,
    default: default
  }
end

#append_response(code, id, text) ⇒ Object

add or append a response with the specified code and the line of text passed in



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
# File 'lib/doc_part.rb', line 15

def append_response(code, id, text)

  # check that the response code is a number
  begin
    code.to_i
  rescue Exception => e
    raise ArgumentError, 'You can only use integer codes for HTTP response examples.'
  end

  # try and find the current response and id in the array
  current_response = self.responses.select { |resp| resp[:code] == code && resp[:id] == id }.first

  # add to array if nil
  if current_response.nil?
    self.responses << {
      code: code,
      id: id,
      text: [text]
    }
  else

    # otherwise append to the current response
    self.responses.each do |resp|
      if resp[:code] == code && resp[:id] == id
        resp[:text] << text
      end
    end
  end
end

#get_docObject

joins all of the text in the doc property of the part with spaces



78
79
80
# File 'lib/doc_part.rb', line 78

def get_doc
  self.doc.join(' ')
end

#get_header(name) ⇒ Object

gets a header’s description from the store



119
120
121
122
123
124
125
# File 'lib/doc_part.rb', line 119

def get_header(name)
  header = self.headers.select { |h| h[:name] == name }.first

  return nil if header.nil?

  return header[:text]
end

#get_param(name) ⇒ Object

gets a parameter by name. will return nil if the parameter does not exist



105
106
107
# File 'lib/doc_part.rb', line 105

def get_param(name)
  self.params.select { |param| param[:name] == name }.first
end

#get_response(code, id) ⇒ Object

gets a response part by code and id and joins the parts of the text



47
48
49
50
51
52
53
# File 'lib/doc_part.rb', line 47

def get_response(code, id)
  response = self.responses.select { |resp| resp[:code] == code && resp[:id] == id }.first

  raise 'Response code and id reference does not exist.' if response.nil?

  response[:text].join
end

#get_routeObject

gets the current route for the doc part



88
89
90
# File 'lib/doc_part.rb', line 88

def get_route
  self.route
end

#set_method(method) ⇒ Object

sets the http method for the documentation part



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/doc_part.rb', line 57

def set_method(method)

  # capitalize and convert to a symbol if not already a symbol
  method.upcase! if !method.kind_of? Symbol

  # check if the http method is valid
  valid_methods = [:GET, :PUT, :POST, :PATCH, :DELETE]
  if !valid_methods.include?(method.to_sym)
    raise ArgumentError, "HTTP method must be valid (#{valid_methods.join(', ')})"
  end

  self.http_method = method
end

#set_route(path) ⇒ Object

sets the current route for the doc part



83
84
85
# File 'lib/doc_part.rb', line 83

def set_route(path)
  self.route = path
end

#to_hashObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/doc_part.rb', line 127

def to_hash
  return {
    route: self.route,
    params: self.params,
    headers: self.headers,
    doc: self.doc,
    responses: self.responses,
    http_method: self.http_method
  }
end