Class: RackToolkit::Server
- Inherits:
-
Object
- Object
- RackToolkit::Server
- Defined in:
- lib/rack_toolkit/server.rb,
lib/rack_toolkit/response_enhancer.rb
Defined Under Namespace
Modules: ResponseEnhancer
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#bind_host ⇒ Object
readonly
Returns the value of attribute bind_host.
-
#cookie_jar ⇒ Object
readonly
Returns the value of attribute cookie_jar.
-
#current_uri ⇒ Object
readonly
Returns the value of attribute current_uri.
-
#default_domain ⇒ Object
Returns the value of attribute default_domain.
-
#default_env ⇒ Object
Returns the value of attribute default_env.
-
#default_headers ⇒ Object
Returns the value of attribute default_headers.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#env_from_server ⇒ Object
readonly
Returns the value of attribute env_from_server.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
-
#rack_env ⇒ Object
readonly
Returns the value of attribute rack_env.
-
#referer ⇒ Object
Returns the value of attribute referer.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#virtual_hosts ⇒ Object
readonly
Returns the value of attribute virtual_hosts.
Instance Method Summary collapse
- #base_uri ⇒ Object
- #current_path ⇒ Object
- #current_url ⇒ Object
- #follow_redirect!(limit: 5, env_override: {}) ⇒ Object
-
#get(url_or_path, params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) ⇒ Object
response = get(‘/’); body = response.body; headers = response.to_hash response # => string response.get_fields(‘set-cookie’) # => array # same as response.to_hash.
-
#initialize(app: nil, port: nil, bind_host: '127.0.0.1', virtual_hosts: [], host: nil, dynamic_app: nil, default_env: {}, default_domain: nil, default_headers: {}, start: false) ⇒ Server
constructor
A new instance of Server.
- #port ⇒ Object
- #post(url_or_path, params: nil, query_params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) ⇒ Object
- #post_data(url_or_path, data, query_params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) ⇒ Object
- #reset_session! ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(app: nil, port: nil, bind_host: '127.0.0.1', virtual_hosts: [], host: nil, dynamic_app: nil, default_env: {}, default_domain: nil, default_headers: {}, start: false) ⇒ Server
Returns a new instance of Server.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rack_toolkit/server.rb', line 17 def initialize(app: nil, port: nil, bind_host: '127.0.0.1', virtual_hosts: [], host: nil, dynamic_app: nil, default_env: {}, default_domain: nil, default_headers: {}, start: false) @app, @port, @bind_host, @default_env, @default_domain, @default_headers = app, port, bind_host, default_env, default_domain, default_headers @host = host || bind_host @virtual_hosts = Set.new virtual_hosts @virtual_hosts << default_domain if default_domain @virtual_hosts << host @dynamic_app = dynamic_app || default_dynamic_app @request_env = {} @referer = nil @cookie_jar = HTTP::CookieJar.new self.start if start end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
13 14 15 |
# File 'lib/rack_toolkit/server.rb', line 13 def app @app end |
#bind_host ⇒ Object (readonly)
Returns the value of attribute bind_host.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def bind_host @bind_host end |
#cookie_jar ⇒ Object (readonly)
Returns the value of attribute cookie_jar.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def @cookie_jar end |
#current_uri ⇒ Object (readonly)
Returns the value of attribute current_uri.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def current_uri @current_uri end |
#default_domain ⇒ Object
Returns the value of attribute default_domain.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def default_domain @default_domain end |
#default_env ⇒ Object
Returns the value of attribute default_env.
13 14 15 |
# File 'lib/rack_toolkit/server.rb', line 13 def default_env @default_env end |
#default_headers ⇒ Object
Returns the value of attribute default_headers.
13 14 15 |
# File 'lib/rack_toolkit/server.rb', line 13 def default_headers @default_headers end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def env @env end |
#env_from_server ⇒ Object (readonly)
Returns the value of attribute env_from_server.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def env_from_server @env_from_server end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def host @host end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def http @http end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def last_response @last_response end |
#rack_env ⇒ Object (readonly)
Returns the value of attribute rack_env.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def rack_env @rack_env end |
#referer ⇒ Object
Returns the value of attribute referer.
13 14 15 |
# File 'lib/rack_toolkit/server.rb', line 13 def referer @referer end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def server @server end |
#virtual_hosts ⇒ Object (readonly)
Returns the value of attribute virtual_hosts.
14 15 16 |
# File 'lib/rack_toolkit/server.rb', line 14 def virtual_hosts @virtual_hosts end |
Instance Method Details
#base_uri ⇒ Object
50 51 52 |
# File 'lib/rack_toolkit/server.rb', line 50 def base_uri @base_uri ||= URI("http://#{host}:#{port}") end |
#current_path ⇒ Object
96 97 98 |
# File 'lib/rack_toolkit/server.rb', line 96 def current_path current_uri.path end |
#current_url ⇒ Object
100 101 102 |
# File 'lib/rack_toolkit/server.rb', line 100 def current_url current_uri.to_s end |
#follow_redirect!(limit: 5, env_override: {}) ⇒ Object
88 89 90 |
# File 'lib/rack_toolkit/server.rb', line 88 def follow_redirect!(limit: 5, env_override: {}) get @last_response['location'], redirect_limit: limit - 1, env_override: {} end |
#get(url_or_path, params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) ⇒ Object
response = get(‘/’); body = response.body; headers = response.to_hash response # => string response.get_fields(‘set-cookie’) # => array # same as response.to_hash
62 63 64 65 66 67 68 |
# File 'lib/rack_toolkit/server.rb', line 62 def get(url_or_path, params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) wrap_response(url_or_path, headers, env_override, params, follow_redirect, redirect_limit) do |uri, h, http| http.get(uri.path.empty? ? '/' : uri.path, h) end end |
#port ⇒ Object
92 93 94 |
# File 'lib/rack_toolkit/server.rb', line 92 def port @port ||= find_free_tcp_port end |
#post(url_or_path, params: nil, query_params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/rack_toolkit/server.rb', line 70 def post(url_or_path, params: nil, query_params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) wrap_response(url_or_path, headers, env_override, params, follow_redirect, redirect_limit) do |uri, h, http| req = Net::HTTP::Post.new uri, h req.form_data = params if params http.request req end end |
#post_data(url_or_path, data, query_params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/rack_toolkit/server.rb', line 80 def post_data(url_or_path, data, query_params: nil, headers: {}, env_override: {}, follow_redirect: true, redirect_limit: 5) wrap_response(url_or_path, headers, env_override, query_params, follow_redirect, redirect_limit) do |uri, h, http| http.post(uri, data, h) end end |
#reset_session! ⇒ Object
54 55 56 57 |
# File 'lib/rack_toolkit/server.rb', line 54 def reset_session! @cookie_jar.clear self.referer = nil end |
#start ⇒ Object
33 34 35 36 37 38 |
# File 'lib/rack_toolkit/server.rb', line 33 def start @server = Puma::Server.new(@dynamic_app) @server.add_tcp_listener @host, port @server_thread = @server.run @http = Net::HTTP.start @host, port end |
#stop ⇒ Object
40 41 42 43 |
# File 'lib/rack_toolkit/server.rb', line 40 def stop @http.finish rescue nil # ignore errors on finish @server.stop true end |