class Rain::DocPart

the doc part is a model to store information about

Attributes

doc[RW]
headers[RW]
http_method[RW]
params[RW]
responses[RW]
route[RW]
signature[RW]

Public Class Methods

new() click to toggle source
# File lib/doc_part.rb, line 8
def initialize
  self.responses = []
  self.doc = []
  self.route = nil
  self.params = []
  self.headers = []
end

Public Instance Methods

append_doc(text) click to toggle source

appends the text to the current documentation for the part

# File lib/doc_part.rb, line 75
def append_doc(text)
  self.doc << text
end
append_header(name, description) click to toggle source

adds a http header with name and description of the header

# File lib/doc_part.rb, line 128
def append_header(name, description)

  # try and find the current header by name
  current_header = self.headers.select { |header| header[:name] == name }.first

  if current_header.nil?
    self.headers << {
      name: name,
      text: [description]
    }
  else
    # otherwise append to the current header
    self.headers.each do |header|
      if header[:name] == name
        header[:text] << description
      end
    end
  end
end
append_param(name, description, type, default = nil) click to toggle source

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

# File lib/doc_part.rb, line 97
def append_param(name, description, type, default = nil)

  # try and find the current param by name
  current_param = self.params.select { |param| param[:name] == name }.first

  # add new param if one doesn't exist
  if current_param.nil?
    self.params << {
      name: name,
      text: [description],
      type: type,
      default: default
    }
  else
    # otherwise append to the current param
    self.params.each do |param|
      if param[:name] == name
        param[:text] << description
      end
    end
  end
end
append_response(code, id, text) click to toggle source

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

# File lib/doc_part.rb, line 18
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_doc() click to toggle source

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

# File lib/doc_part.rb, line 81
def get_doc
  self.doc.join(' ')
end
get_header(name) click to toggle source

gets a header’s description from the store

# File lib/doc_part.rb, line 149
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) click to toggle source

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

# File lib/doc_part.rb, line 122
def get_param(name)
  self.params.select { |param| param[:name] == name }.first
end
get_response(code, id) click to toggle source

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

# File lib/doc_part.rb, line 50
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_route() click to toggle source

gets the current route for the doc part

# File lib/doc_part.rb, line 91
def get_route
  self.route
end
get_signature() click to toggle source

gets the method signature

# File lib/doc_part.rb, line 163
def get_signature
  self.signature
end
set_method(method) click to toggle source

sets the http method for the documentation part

# File lib/doc_part.rb, line 60
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) click to toggle source

sets the current route for the doc part

# File lib/doc_part.rb, line 86
def set_route(path)
  self.route = path
end
set_signature(sig) click to toggle source

sets a method signature

# File lib/doc_part.rb, line 158
def set_signature(sig)
  self.signature = sig
end
to_hash() click to toggle source
# File lib/doc_part.rb, line 167
def to_hash
  return {
    route: self.route,
    params: self.params,
    headers: self.headers,
    doc: self.doc,
    responses: self.responses,
    http_method: self.http_method,
    signature: self.signature
  }
end