Class: RackToolkit::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_toolkit/server.rb,
lib/rack_toolkit/response_enhancer.rb

Defined Under Namespace

Modules: ResponseEnhancer

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#appObject

Returns the value of attribute app.



13
14
15
# File 'lib/rack_toolkit/server.rb', line 13

def app
  @app
end

#bind_hostObject (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

Returns the value of attribute cookie_jar.



14
15
16
# File 'lib/rack_toolkit/server.rb', line 14

def cookie_jar
  @cookie_jar
end

#current_uriObject (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_domainObject

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_envObject

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_headersObject

Returns the value of attribute default_headers.



13
14
15
# File 'lib/rack_toolkit/server.rb', line 13

def default_headers
  @default_headers
end

#envObject (readonly)

Returns the value of attribute env.



14
15
16
# File 'lib/rack_toolkit/server.rb', line 14

def env
  @env
end

#env_from_serverObject (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

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/rack_toolkit/server.rb', line 14

def host
  @host
end

#httpObject (readonly)

Returns the value of attribute http.



14
15
16
# File 'lib/rack_toolkit/server.rb', line 14

def http
  @http
end

#last_responseObject (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_envObject (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

#refererObject

Returns the value of attribute referer.



13
14
15
# File 'lib/rack_toolkit/server.rb', line 13

def referer
  @referer
end

#serverObject (readonly)

Returns the value of attribute server.



14
15
16
# File 'lib/rack_toolkit/server.rb', line 14

def server
  @server
end

#virtual_hostsObject (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_uriObject



50
51
52
# File 'lib/rack_toolkit/server.rb', line 50

def base_uri
  @base_uri ||= URI("http://#{host}:#{port}")
end

#current_pathObject



96
97
98
# File 'lib/rack_toolkit/server.rb', line 96

def current_path
  current_uri.path
end

#current_urlObject



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

#portObject



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

#startObject



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

#stopObject



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