Class: RubyAPI::CLI
- Inherits:
-
Object
- Object
- RubyAPI::CLI
- Defined in:
- lib/fastrb/cli.rb
Class Method Summary collapse
- .config_ru_content ⇒ Object
- .gemfile_content(name) ⇒ Object
- .hello_spec_content ⇒ Object
- .invoke(args) ⇒ Object
- .main_rb_content ⇒ Object
- .new_project(name) ⇒ Object
- .readme_content(name) ⇒ Object
- .routes_rb_content ⇒ Object
- .spec_helper_content ⇒ Object
- .start_server ⇒ Object
Class Method Details
.config_ru_content ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/fastrb/cli.rb', line 72 def self.config_ru_content <<~RUBY require_relative "app/main" require_relative "app/routes" run App.new RUBY end |
.gemfile_content(name) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/fastrb/cli.rb', line 62 def self.gemfile_content(name) <<~GEMFILE source "https://rubygems.org" gem "fastrb", "~> #{RubyAPI::VERSION}" gem "rackup" gem "puma" GEMFILE end |
.hello_spec_content ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/fastrb/cli.rb', line 124 def self.hello_spec_content <<~RUBY require "spec_helper" RSpec.describe "GET /hello" do include Rack::Test::Methods def app App.new end it "returns hello world" do get "/hello" expect(last_response.status).to eq(200) expect(JSON.parse(last_response.body)["message"]).to eq("Hello World") end end RUBY end |
.invoke(args) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/fastrb/cli.rb', line 6 def self.invoke(args) command = args[0] name = args[1] case command when "new" new_project(name) when "server" start_server when "version" puts "fastrb #{VERSION}" else puts "Usage: fastrb <command> [options]" puts "" puts "Commands:" puts " new <name> Create a new FastRb project" puts " server Start the server (Falcon/Puma)" puts " version Show version" exit 1 end end |
.main_rb_content ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fastrb/cli.rb', line 81 def self.main_rb_content <<~RUBY require "fastrb" class App < RubyAPI::App def initialize super do Routes.apply(self) end end end RUBY end |
.new_project(name) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fastrb/cli.rb', line 28 def self.new_project(name) puts "Creating new FastRb project: #{name}" FileUtils.mkdir_p(name) FileUtils.mkdir_p("#{name}/app") FileUtils.mkdir_p("#{name}/config/environments") FileUtils.mkdir_p("#{name}/public") FileUtils.mkdir_p("#{name}/spec/unit") FileUtils.mkdir_p("#{name}/spec/requests") File.write("#{name}/Gemfile", gemfile_content(name)) File.write("#{name}/config.ru", config_ru_content) File.write("#{name}/app/main.rb", main_rb_content) File.write("#{name}/app/routes.rb", routes_rb_content) File.write("#{name}/.rspec", ".rspec") File.write("#{name}/spec/spec_helper.rb", spec_helper_content) File.write("#{name}/spec/requests/hello_spec.rb", hello_spec_content) File.write("#{name}/README.md", readme_content(name)) puts "Done! cd #{name} && bundle install && fastrb server" end |
.readme_content(name) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/fastrb/cli.rb', line 144 def self.readme_content(name) <<~MARKDOWN # #{name} A FastRb application. ## Setup bundle install ## Running fastrb server ## Testing bundle exec rspec MARKDOWN end |
.routes_rb_content ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/fastrb/cli.rb', line 95 def self.routes_rb_content <<~RUBY module Routes def self.apply(app) app.get "/hello" do { message: "Hello World" } end end end RUBY end |
.spec_helper_content ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/fastrb/cli.rb', line 107 def self.spec_helper_content <<~RUBY require "fastrb" require "rack/test" RSpec.configure do |config| config.expect_with :rspec do |expectations| expectations.include_chain_clauses_in_custom_matcher_descriptions = true end config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end end RUBY end |
.start_server ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/fastrb/cli.rb', line 50 def self.start_server require "rackup" rackup_file = File.join(Dir.pwd, "config.ru") unless File.exist?(rackup_file) puts "Error: config.ru not found in current directory" exit 1 end Rackup::Server.start(config: rackup_file, Host: "0.0.0.0", Port: 3000) end |