Sha256: 4ff32bd8eb9ab9b6e74bc20fda88235e052a4dd61d88558afda71dd100caf46c

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require 'thor/group'
require 'yaml'

module Grapethor
  class Api < Thor::Group
    include Thor::Actions
    include Grapethor::Utils

    attr_reader :api_version,
                :app_path

    namespace :api

    def self.exit_on_failure?
      true
    end

    def self.source_root
      File.join(__dir__, '..')
    end


    argument :version, type: :string,
                       desc: 'API version'

    class_option :path,  aliases: '-p',
                         type: :string,
                         default: '.',
                         desc: 'Relative path to application directory'


    def parse_args_and_opts
      @api_version = version.downcase
      @app_path    = options[:path]
    end


    def validate_target_app
      unless app_dir_exists?
        alert <<~MSG
                Directory '#{app_path}' does not seem to be generated by Grapethor or root application directory.\n
                Please 'cd' into application root diretory or use '--path' option.
              MSG
        exit
      end
    end


    def create_api
      report("Creating new API...") do
        directory 'templates/api', app_path
        directory "templates/api_#{app_test_framework}", app_path
        insert_into_file "#{app_path}/api/base.rb",
                         "\s\s\s\smount API#{api_version}::Base\n",
                         :before => "\s\s\s\s# mount API<VERSION>::Base\n"
      end
    end


    private

    def app_dir_exists?
      File.exist?("#{app_path}/api/base.rb")
    end

    def app_name
      @app_name ||= config_filename(app_path)['app_name']
    end

    def app_prefix
      @app_prefix ||= config_filename(app_path)['app_prefix']
    end

    def app_swagger?
      @app_swagger ||= config_filename(app_path)['app_swagger']
    end

    def app_test_framework
      @app_test_framework ||= config_filename(@app_path)['app_test_framework']
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grapethor-0.2.3 lib/grapethor/generators/api.rb