Sha256: 2f8cc46e71896c8089d229cf56526f30b87352516434f3b3079faaad9c472b26

Contents?: true

Size: 1.76 KB

Versions: 9

Compression:

Stored size: 1.76 KB

Contents

require 'rails/generators/base'

module Webvalve
  module Generators
    class FakeServiceGenerator < Rails::Generators::Base
      desc "Creates a WebValve fake service"

      argument :service_name, required: true

      def create_webvalve_fake_service_file
        require_config!
        create_fake_service_file
        register_fake_in_config
      end

      private

      def create_fake_service_file
        create_file full_file_path, <<-FILE.strip_heredoc
          class #{fake_service_class_name} < WebValve::FakeService
            # # define your routes here
            #
            # get '/widgets' do
            #   json result: 'it works!'
            # end
            #
            # # set the base url for this API via ENV
            #
            # export #{parsed_service_name.upcase}_API_URL='http://whatever.dev'
            #
            # # toggle this service on via ENV
            #
            # export #{parsed_service_name.upcase}_ENABLED=true
          end
        FILE
      end

      def register_fake_in_config
        append_to_file config_file_path do <<~RUBY
          WebValve.register "#{fake_service_class_name}"
        RUBY
        end
      end

      def require_config!
        raise 'No WebValve configuration file found. Please run `rails generate webvalve:install` first' unless File.exists?(config_file_path)
      end

      def config_file_path
        "config/webvalve.rb"
      end

      def full_file_path
        "webvalve/#{fake_service_filename}.rb"
      end

      def fake_service_class_name
        fake_service_filename.camelize
      end

      def fake_service_filename
        "fake_#{parsed_service_name.underscore}"
      end

      def parsed_service_name
        service_name.sub(/fake/i, '')
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
webvalve-1.3.0 lib/generators/webvalve/fake_service_generator.rb
webvalve-1.2.0 lib/generators/webvalve/fake_service_generator.rb
webvalve-1.1.0 lib/generators/webvalve/fake_service_generator.rb
webvalve-1.0.2 lib/generators/webvalve/fake_service_generator.rb
webvalve-1.0.1 lib/generators/webvalve/fake_service_generator.rb
webvalve-1.0.0 lib/generators/webvalve/fake_service_generator.rb
webvalve-0.12.0 lib/generators/webvalve/fake_service_generator.rb
webvalve-0.11.0 lib/generators/webvalve/fake_service_generator.rb
webvalve-0.10.0 lib/generators/webvalve/fake_service_generator.rb