class Tsurezure
main class for tsurezure.
Attributes
endpoints[R]
Public Class Methods
new(port)
click to toggle source
prepares the server to run on a specified port.
# File lib/tsurezure.rb, line 182 def initialize(port) raise ErrorCodes.nan_error 'port' unless port.is_a? Numeric raise ErrorCodes.range_error 0, 65_535 unless (0..65_535).include? port @server = TCPServer.new port @port = port @endpoints = {} @middleware = {} end
Public Instance Methods
add_middleware(path, callback, options)
click to toggle source
# File lib/tsurezure.rb, line 194 def add_middleware(path, callback, options) unless path.is_a? String raise ArgumentError, 'first argument to middleware\ must be string or function.' end middleware_object = { options: options, callback: callback, path_regex: path } @middleware[path] << middleware_object if @middleware[path] @middleware[path] = [middleware_object] unless @middleware[path] end
kill()
click to toggle source
# File lib/tsurezure.rb, line 241 def kill abort end
listen(callback = nil)
click to toggle source
run when the server is prepared to accept requests.
# File lib/tsurezure.rb, line 225 def listen(callback = nil) if $TRZR_PROCESS_MODE == 'development' puts "[trzr_dev] running on port #{@port}!" end # call the callback if there's one provided callback.call server_opts if callback.is_a? Proc # create a new thread for handle each incoming request loop do Thread.start(@server.accept) do |client| RequestHandler.new(client).process client, @endpoints, @middleware end end end
register(http_method, path, callback, options = nil)
click to toggle source
# File lib/tsurezure.rb, line 209 def register(http_method, path, callback, options = nil) http_method = http_method.upcase insurance = ensure_registration http_method, path, callback, options raise ArgumentError, insurance if insurance.class == String # register a new endpoint but do not register dupes @endpoints[http_method] = {} unless @endpoints.key? http_method new_endpoint = { path: path, responder: callback, options: options } add_new_endpoint new_endpoint, http_method end
Private Instance Methods
server_opts()
click to toggle source
# File lib/tsurezure.rb, line 247 def server_opts { port: @port, endpoints: @endpoints, middleware: @middleware } end
registration of endpoints and
↑ topPrivate Instance Methods
add_new_endpoint(endpoint, method)
click to toggle source
# File lib/tsurezure.rb, line 300 def add_new_endpoint(endpoint, method) @endpoints[method].each do |_, value| if value[:path] == endpoint[:path] raise ArgumentError, 'cannot register duplicate path.' end end # add endpoint to list of registered endpoints @endpoints[method][endpoint[:path]] = endpoint end
ensure_registration(*args)
click to toggle source
# File lib/tsurezure.rb, line 260 def ensure_registration(*args) verification = verify_registration(*args) return verification unless verification verification # to register end
validate_registration_params(method, path, responder)
click to toggle source
# File lib/tsurezure.rb, line 268 def validate_registration_params(method, path, responder) unless TResponse::Utils.new.valid_methods.include? method return "#{method} is not a valid http method." end return 'invalid path type. must be a string.' unless path.class == String if path.empty? || path.chr != '/' return 'invalid path. must begin with "/".' end return 'invalid responder type. must a proc.' unless responder.class == Proc true end
verify_registration(http_method, path, responder, options)
click to toggle source
# File lib/tsurezure.rb, line 284 def verify_registration(http_method, path, responder, options) valid = validate_registration_params http_method, path, responder return valid unless valid == true return true if options.nil? || options.empty? return 'invalid options type.' unless options.class == Hash valid_opts = %w[content_type method location] opts_valid = OUtil.check_against_array(options.keys, valid_opts, 'register') return 'invalid options provided to register.' unless opts_valid true # to ensure_registration end