class SwaggerDocsGenerator::ParserAction

# Parse action in controller classe to Rails application. It's adding paths to swagger docs file.

Public Class Methods

new(action, &block) click to toggle source
Calls superclass method
# File lib/swagger_docs_generator/parser/action.rb, line 22
def initialize(action, &block)
  super(binding.of_callers[1].klass)
  @action = action
  @parameter = []
  @response = {}
  instance_eval(&block)
end

Public Instance Methods

adding_path() click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 30
def adding_path
  create_file unless File.exist?(temporary_file)
  json = JSON.parse(File.read(temporary_file))
  File.open(temporary_file, 'w') do |file|
    path_exist(json, construct_routes)
    file.puts(JSON.pretty_generate(json))
  end
end

Private Instance Methods

add_tag(hash, path, tag) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 61
def add_tag(hash, path, tag)
  hash[path][@verb][:tags].push(tag)
  hash[path]['patch'][:tags].push(tag) if @verb.eql?('put')
end
construct_path() click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 87
def construct_path
  element = {}
  summary_text = @summary.present? ? @summary : @action.to_s
  element.merge!(summary: summary_text.humanize)
  element.merge!(description: @description)   if @description.present?
  element.merge!(parameters: @parameter)      if @parameter.present?
  element.merge!(consumes: @consume)          if @consume.present?
  element.merge!(deprecated: @deprecated)     if @deprecated.present?
  element.merge!(produces: @produce)          if @produce.present?
  element.merge!(responses: @response)
  element.merge!(tags: write_tag)
end
construct_routes() click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 76
def construct_routes
  yop = {}
  extract = Extractor.new(controller, @action)
  @verb = extract.verb
  @route = extract.path
  @route.each do |rte|
    yop.merge!(@verb.eql?('put') ? route_update(rte) : route(rte))
  end
  yop
end
consumes(text) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 120
def consumes(text)
  @consume = text
end
deprecated(value) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 128
def deprecated(value)
  @deprecated = value
end
description(text) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 142
def description(text)
  @description = text
end
extract_tag(route) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 112
def extract_tag(route)
  route.split('/').reject(&:empty?).first.humanize
end
merge_hashes(old_route, index, paths, hash) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 66
def merge_hashes(old_route, index, paths, hash)
  if !old_route.blank? && old_route.keys.include?(index)
    paths.each do |path|
      old_route[path].merge!(hash[path])
    end
  else
    old_route.merge!(hash)
  end
end
parameters(&block) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 137
def parameters(&block)
  param = SwaggerDocsGenerator::Actions::Parameter.new(&block)
  @parameter.push(param.to_hash)
end
path_exist(json, hash) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 41
def path_exist(json, hash)
  old_route = json['paths']

  keys_new = hash.keys[0]
  paths = hash.keys.split.first

  # test_tags(paths, hash)
  merge_hashes(old_route, keys_new.to_s, paths, hash)
end
produces(text) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 124
def produces(text)
  @produce = text
end
responses(&block) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 132
def responses(&block)
  rep = SwaggerDocsGenerator::Actions::Response.new(&block)
  @response.merge!(rep.to_hash)
end
route(rte) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 100
def route(rte)
  { rte => { @verb => construct_path } }
end
route_update(rte) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 104
def route_update(rte)
  { rte => { @verb => construct_path }.merge!('patch' => construct_path) }
end
summary(text) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 116
def summary(text)
  @summary = text
end
test_tags(paths, hash) click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 51
def test_tags(paths, hash)
  paths.each do |path|
    tag = extract_tag(path)
    next unless path.downcase.include?(tag.downcase) &&
                !hash[path][@verb][:tags].include?(tag) &&
                !tag.include?('.json')
    add_tag(hash, path, tag)
  end if paths.count <= 2
end
write_tag() click to toggle source
# File lib/swagger_docs_generator/parser/action.rb, line 108
def write_tag
  [@tag_name.humanize]
end