Class: OopRailsServer::RailsServer
- Inherits:
-
Object
- Object
- OopRailsServer::RailsServer
- Defined in:
- lib/oop_rails_server/rails_server.rb
Defined Under Namespace
Classes: CommandFailedError, FailedStartupError
Instance Attribute Summary collapse
-
#actual_rails_version ⇒ Object
readonly
Returns the value of attribute actual_rails_version.
-
#actual_ruby_engine ⇒ Object
readonly
Returns the value of attribute actual_ruby_engine.
-
#actual_ruby_version ⇒ Object
readonly
Returns the value of attribute actual_ruby_version.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rails_root ⇒ Object
readonly
Returns the value of attribute rails_root.
-
#rails_version ⇒ Object
readonly
Returns the value of attribute rails_version.
Instance Method Summary collapse
- #get(path_or_uri, options = { }) ⇒ Object
- #get_response(path_or_uri, options = { }) ⇒ Object
-
#initialize(options) ⇒ RailsServer
constructor
A new instance of RailsServer.
- #localhost_name ⇒ Object
- #post(path_or_uri, options = { }) ⇒ Object
- #run_command_in_rails_root!(command) ⇒ Object
- #send_http_request(path_or_uri, options = { }) ⇒ Object
- #setup! ⇒ Object
- #start! ⇒ Object
- #stop! ⇒ Object
- #uri_for(path_or_uri, query_values = nil) ⇒ Object
Constructor Details
#initialize(options) ⇒ RailsServer
Returns a new instance of RailsServer.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/oop_rails_server/rails_server.rb', line 12 def initialize() .assert_valid_keys( :name, :template_paths, :runtime_base_directory, :rails_version, :rails_env, :gemfile_modifier, :log, :verbose ) @name = [:name] || raise(ArgumentError, "You must specify a name for the Rails server") @template_paths = [:template_paths] || raise(ArgumentError, "You must specify one or more template paths") @template_paths = Array(@template_paths).map { |t| File.(t) } @template_paths = base_template_directories + @template_paths @runtime_base_directory = [:runtime_base_directory] || raise(ArgumentError, "You must specify a runtime_base_directory") @runtime_base_directory = File.(@runtime_base_directory) @rails_version = [:rails_version] || :default @rails_env = ([:rails_env] || 'production').to_s @log = [:log] || $stderr @verbose = .fetch(:verbose, true) @gemfile_modifier = [:gemfile_modifier] || (Proc.new { |gemfile| }) @rails_root = File.(File.join(@runtime_base_directory, rails_version.to_s, name.to_s)) @port = 20_000 + rand(10_000) @server_pid = nil end |
Instance Attribute Details
#actual_rails_version ⇒ Object (readonly)
Returns the value of attribute actual_rails_version.
10 11 12 |
# File 'lib/oop_rails_server/rails_server.rb', line 10 def actual_rails_version @actual_rails_version end |
#actual_ruby_engine ⇒ Object (readonly)
Returns the value of attribute actual_ruby_engine.
10 11 12 |
# File 'lib/oop_rails_server/rails_server.rb', line 10 def actual_ruby_engine @actual_ruby_engine end |
#actual_ruby_version ⇒ Object (readonly)
Returns the value of attribute actual_ruby_version.
10 11 12 |
# File 'lib/oop_rails_server/rails_server.rb', line 10 def actual_ruby_version @actual_ruby_version end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/oop_rails_server/rails_server.rb', line 10 def name @name end |
#rails_root ⇒ Object (readonly)
Returns the value of attribute rails_root.
10 11 12 |
# File 'lib/oop_rails_server/rails_server.rb', line 10 def rails_root @rails_root end |
#rails_version ⇒ Object (readonly)
Returns the value of attribute rails_version.
10 11 12 |
# File 'lib/oop_rails_server/rails_server.rb', line 10 def rails_version @rails_version end |
Instance Method Details
#get(path_or_uri, options = { }) ⇒ Object
77 78 79 80 |
# File 'lib/oop_rails_server/rails_server.rb', line 77 def get(path_or_uri, = { }) out = get_response(path_or_uri, ) out.body.strip if out end |
#get_response(path_or_uri, options = { }) ⇒ Object
133 134 135 |
# File 'lib/oop_rails_server/rails_server.rb', line 133 def get_response(path_or_uri, = { }) send_http_request(path_or_uri, .merge(:http_method => :get)) end |
#localhost_name ⇒ Object
96 97 98 |
# File 'lib/oop_rails_server/rails_server.rb', line 96 def localhost_name "127.0.0.1" end |
#post(path_or_uri, options = { }) ⇒ Object
73 74 75 |
# File 'lib/oop_rails_server/rails_server.rb', line 73 def post(path_or_uri, = { }) send_http_request(path_or_uri, .merge(:http_method => :post)) end |
#run_command_in_rails_root!(command) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/oop_rails_server/rails_server.rb', line 137 def run_command_in_rails_root!(command) Bundler.with_clean_env do with_rails_env do in_rails_root do safe_system("bundle exec #{command}") end end end end |
#send_http_request(path_or_uri, options = { }) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/oop_rails_server/rails_server.rb', line 100 def send_http_request(path_or_uri, = { }) .assert_valid_keys(:ignore_status_code, :nil_on_not_found, :query, :no_layout, :accept_header, :http_method, :post_variables) uri = uri_for(path_or_uri, [:query]) data = nil accept_header = .fetch(:accept_header, 'text/html') http_method = [:http_method] || :get Net::HTTP.start(uri.host, uri.port) do |http| klass = Net::HTTP.const_get(http_method.to_s.strip.camelize) request = klass.new(uri.to_s) if [:post_variables] request.set_form_data([:post_variables]) end request['Accept'] = accept_header if accept_header data = http.request(request) end if (data.code.to_s != '200') if [:ignore_status_code] # ok, nothing elsif [:nil_on_not_found] data = nil else raise "'#{uri}' returned #{data.code.inspect}, not 200; body was: #{data.body.inspect}" end end data end |
#setup! ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/oop_rails_server/rails_server.rb', line 46 def setup! @set_up ||= begin Bundler.with_clean_env do with_rails_env do setup_directories! in_rails_root_parent do splat_bootstrap_gemfile! rails_new! update_gemfile! end in_rails_root do run_bundle_install!(:primary) splat_template_files! end end end true end end |
#start! ⇒ Object
42 43 44 |
# File 'lib/oop_rails_server/rails_server.rb', line 42 def start! do_start! unless server_pid end |
#stop! ⇒ Object
69 70 71 |
# File 'lib/oop_rails_server/rails_server.rb', line 69 def stop! stop_server! if server_pid end |
#uri_for(path_or_uri, query_values = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/oop_rails_server/rails_server.rb', line 82 def uri_for(path_or_uri, query_values = nil) query_values ||= { } if path_or_uri.kind_of?(::URI) path_or_uri else uri_string = "http://#{localhost_name}:#{@port}/#{path_or_uri}" if query_values.length > 0 uri_string += ("?" + query_values.map { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")) end URI.parse(uri_string) end end |