Sha256: 8324f141fa1a5fab5a8d2a23e89b2bfded73b2db885992b507ac7f1309af4de6

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

require 'tilt'
require 'erb'

require_relative './template_context'

module Restspec
  module Docs
    class DocsRunner < Thor::Group
      TEMPLATE_BY_EXTENSION = {
        '.md' => 'docs.md.erb',
        '.html' => 'docs.html.slim'
      }

      argument :file, :default => 'api_docs.md'

      def generate_docs
        require 'restspec'
        require config_file

        File.write(file, read_template(extension))
      end

      private

      def read_template(extension)
        template_file_name = "templates/#{template_name}"
        template_file = Pathname.new(File.dirname(__FILE__)).join(template_file_name)

        Tilt.new(template_file).render(TemplateContext.new)
      end

      def extension
        match = file.match(/\.[\w]+$/)
        raise NoValidExtensionError if match.blank?
        match[0]
      end

      def template_name
        TEMPLATE_BY_EXTENSION.fetch(extension) do
          raise NoValidExtensionError
        end
      end

      def config_file
        Pathname.new(Dir.pwd).join('spec/api/restspec/restspec_config.rb')
      end

      class NoValidExtensionError < StandardError
        ERROR_MESSAGE = "ERROR: The file passed as argument does not include a valid extension"

        def initialize(msg = ERROR_MESSAGE)
          super(msg)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
restspec-0.3.2 lib/restspec/runners/docs/docs_runner.rb
restspec-0.3.1 lib/restspec/runners/docs/docs_runner.rb
restspec-0.3.0 lib/restspec/runners/docs/docs_runner.rb
restspec-0.2.6 lib/restspec/runners/docs/docs_runner.rb
restspec-0.2.5 lib/restspec/runners/docs/docs_runner.rb
restspec-0.2.4 lib/restspec/runners/docs/docs_runner.rb
restspec-0.2.3 lib/restspec/runners/docs/docs_runner.rb
restspec-0.2.2 lib/restspec/runners/docs/docs_runner.rb
restspec-0.2.1 lib/restspec/runners/docs/docs_runner.rb