diff --git a/.gitignore b/.gitignore index 9106b2a..f79d920 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ /pkg/ /spec/reports/ /tmp/ +.yardopts +*.gem diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..6e8bf73 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1.0 diff --git a/lennarb.gemspec b/lennarb.gemspec index 21b43ec..3085c57 100644 --- a/lennarb.gemspec +++ b/lennarb.gemspec @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative 'lib/lennarb/version' +version = File.read(File.expand_path('VERSION', __dir__)).strip Gem::Specification.new do |spec| spec.name = 'lennarb' - spec.version = Lennarb::VERSION + spec.version = version spec.license = 'MIT' spec.authors = ['Aristóteles Coutinho'] spec.email = ['aristotelesbr@gmail.com'] @@ -24,10 +24,11 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] + spec.add_dependency 'colorize', '~> 1.1' spec.add_dependency 'puma', '~> 6.4' spec.add_dependency 'rack', '~> 3.0', '>= 3.0.8' spec.add_dependency 'rake', '~> 13.0', '>= 13.0.6' - spec.add_dependency 'colorize', '~> 1.1' + # Uncomment to register a new dependency of your gem spec.add_development_dependency 'minitest', '~> 5.20' spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6' diff --git a/lib/lenna/base.rb b/lib/lenna/base.rb index 064a5a2..a403c01 100644 --- a/lib/lenna/base.rb +++ b/lib/lenna/base.rb @@ -4,9 +4,9 @@ require 'puma' # Internal dependencies -require_relative 'middleware/default/error_handler' -require_relative 'middleware/default/logging' -require_relative 'router' +require 'lenna/middleware/default/error_handler' +require 'lenna/middleware/default/logging' +require 'lenna/router' module Lenna # The base class is used to start the server. diff --git a/lib/lenna/router.rb b/lib/lenna/router.rb index a1204b4..776147b 100644 --- a/lib/lenna/router.rb +++ b/lib/lenna/router.rb @@ -7,13 +7,14 @@ # External dependencies require 'rack' -require_relative 'middleware/app' -require_relative 'router/builder' -require_relative 'router/cache' -require_relative 'router/namespace_stack' -require_relative 'router/request' -require_relative 'router/response' -require_relative 'router/route_matcher' +# Internal dependencies +require 'lenna/middleware/app' +require 'lenna/router/builder' +require 'lenna/router/cache' +require 'lenna/router/namespace_stack' +require 'lenna/router/request' +require 'lenna/router/response' +require 'lenna/router/route_matcher' module Lenna # The Node struct is used to represent a node in the tree of routes. diff --git a/lib/lenna/router/request.rb b/lib/lenna/router/request.rb index 7a6750c..68c5af3 100644 --- a/lib/lenna/router/request.rb +++ b/lib/lenna/router/request.rb @@ -12,7 +12,7 @@ class Request < ::Rack::Request # # @return [Hash] the request params # - # @public + # @api public def params = super.merge(parse_body_params) # This method rewinds the body diff --git a/lib/lenna/router/response.rb b/lib/lenna/router/response.rb index 1bc8b61..b167c08 100644 --- a/lib/lenna/router/response.rb +++ b/lib/lenna/router/response.rb @@ -12,7 +12,6 @@ class Response public attr_reader :params private attr_accessor :_headers, :_body, :_status - # Initialize the Response def initialize(headers = {}, status = 200, body = []) self._headers = headers self._status = status @@ -21,34 +20,46 @@ def initialize(headers = {}, status = 200, body = []) end # @api public + # # @return [Integer] the response status def status = fetch_status # @api public + # # @param status [Integer] the response status + # # @return [void] def put_status(value) = status!(value) # @api public + # # @return [Array(String)] the body value def body = fetch_body # @api public + # # @param value [Array(String)] the body value + # # @return [void] def put_body(value) = body!(value) + + # This method will get the header value. + # # @api public + # # @param header [String] the header name + # # @return [String] the header value - # @note This method will get the header value. def header(key) = fetch_header(key) # @api public + # # @return [Hash] the response headers def headers = fetch_headers - # @api public + # This method will set the header value. + # # @param header [String] the header name # @param value [String] the header value # @return [void] @@ -56,6 +67,8 @@ def headers = fetch_headers # If the header already exists, then the value will # be appended to the header. # + # @api public + # # @example # put_header('X-Request-Id', '123') # # => '123' @@ -68,6 +81,7 @@ def headers = fetch_headers def put_header(key, value) = header!(key, value) # Add multiple headers. + # # @param headers [Hash] the headers # @return [void] # @note This method will add the headers. @@ -79,23 +93,27 @@ def put_header(key, value) = header!(key, value) # 'Content-Type' => 'application/json', # 'X-Request-Id' => '123' # } - # def put_headers(headers) headers => ::Hash headers.each { |key, value| put_header(key, value) } end - # @api public + # This method will delete the header. # @param header [String] the header name + # # @return [void] - # @note This method will delete the header. + # + # @api public def remove_header(key) = delete_header(key) - # @api public + # This method will get the redirect location. + # # @param value [String] the key of the cookie + # # @return [String] the cookie - # @note This method will get the cookie. + # + # @api public def cookie(value) value => ::String @@ -105,11 +123,14 @@ def cookie(value) .then { |cookie| cookie.split('=').last } end - # @api public + # This method will set the cookie. + # # @param key [String] the key of the cookie # @param value [String] the value of the cookie + # # @return [void] - # @note This method will set the cookie. + # + # @api public def put_cookie(key, value) key => ::String value => ::String @@ -119,8 +140,10 @@ def put_cookie(key, value) header!('Set-Cookie', cookie) end - # @api public + # This method will get all the cookies. + # # @return [Hash] the cookies + # @api public def cookies fetch_header('Set-Cookie') .then { |cookie| cookie.split('; ') } @@ -131,12 +154,14 @@ def cookies end end - # @api public + # This method will set redirect location. The status will be set to 302. + # # @param location [String] the redirect location - # @param status [Integer] the redirect status + # @param status [Integer] the redirect status, default is 302. + # # @return [void] - # @note This method will set the redirect location and - # status and finish the response. + # + # @api public def redirect(location, status: 302) location => ::String @@ -148,21 +173,21 @@ def redirect(location, status: 302) raise ::ArgumentError, 'location must be a string' end - # @api public + # This method will finish the response. + # # @return [void] - # @note This method will finish the response. - def finish = finish! - + # # @api public - # @return [String] the response content type - # @note This method will set - # the response content type. - def content_type = header('Content-Type') + def finish = finish! - # @api public + # This method will set the response content type. + # # @param type [String] the response content type # @param charset [Hash] the response charset + # # @return [void] + # + # @api public def put_content_type(type, charset: nil) type => ::String @@ -174,11 +199,13 @@ def put_content_type(type, charset: nil) raise ::ArgumentError, 'type must be a string' end - # @api public + # This method will set the response data and finish the response. + # # @param data [Hash, Array] the response data + # # @return [void] - # @note This method will set the response data and - # finish the response. + # + # @api public def json(data:, status: 200) data => ::Array | ::Hash @@ -190,8 +217,12 @@ def json(data:, status: 200) end # Set the response content type to text/html. + # # @param str [String] the response body + # # @return [void] + # + # @api public def html(str = nil, status: 200) status!(status) header!('Content-Type', 'text/html') @@ -200,13 +231,13 @@ def html(str = nil, status: 200) finish! end + # This method will render the template. + # # @param template_nam [String] the template name # @param path [String] the template path, default is 'views' # @param locals [Hash] the template locals + # # @return [void | Exception] - # @note This method will render the template. - # The template engine is determined by the - # file extension. # # @example # render('index') @@ -245,9 +276,13 @@ def render(template_name, path: 'views', locals: {}, status: 200) end # Helper methods for the response. - # @api public + # # @return [void] - # @note This method will finish the response with a 404 status. + # + # @api public + # + # @see #render + # @see #finish! def not_found body!(['Not Found']) status!(404) @@ -256,14 +291,18 @@ def not_found private - # @api private + # This method will get the response status. + # # @return [Integer] the response status - # @note This method will get the response status. + # + # @api private def fetch_status = _status - # @api private + # This method will get the response status. + # # @return [Integer] the response status - # @note This method will get the response status. + # + # @api private def status!(value) value => ::Integer @@ -272,14 +311,13 @@ def status!(value) raise ::ArgumentError, 'status must be an integer' end - # @api private # @return [Array(String)] the body value def fetch_body = _body - # @api private + # This method will set the body. + # # @param body [Array(String)] the body to be used # @return [void] - # @note This method will set the body. def body!(value) body => ::String | ::Array @@ -291,23 +329,18 @@ def body!(value) raise ::ArgumentError, 'body must be a string or an array' end - # @api private # @param header [String] the header name + # # @return [String] the header value - # @note This method will get the header value. def fetch_header(header) = _headers[header] - # @api private # @return [Hash] the response headers def fetch_headers = _headers - # @api private # @param key [String] the header name # @param value [String] the value to be used + # # @return [void] - # @note This method will set the header value. - # If the header already exists, then the value will - # be appended to the header. def header!(key, value) key => ::String value => ::String | ::Array @@ -322,14 +355,13 @@ def header!(key, value) raise ::ArgumentError, 'header must be a string or an array' end - # @api private # @param key [String] the header name + # # @return [void] - # @note This method will delete the header. def delete_header(key) = _headers.delete(key) - # @api private # @param value [String] the redirect location + # # @return [void] def location!(value) value => ::String @@ -337,15 +369,14 @@ def location!(value) header!('Location', value) end - # @api private # @param value [String] the content value + # # @return [String] the size of the content - # @note This method will get the size of the content. def content_length!(value) = header!('Content-Length', value) - # @api private + # This method will finish the response. + # # @return [void] - # @note This method will finish the response. def finish! put_content_type('text/html') unless header('Content-Type') content_length!(body.join.size.to_s) unless header('Content-Length') diff --git a/lib/lennarb.rb b/lib/lennarb.rb index 328ecc6..7ce774d 100644 --- a/lib/lennarb.rb +++ b/lib/lennarb.rb @@ -1,4 +1,3 @@ # frozen_string_literal: true -require_relative 'lenna/base' -require_relative 'lennarb/version' +require 'lenna/base' diff --git a/lib/lennarb/version.rb b/lib/lennarb/version.rb deleted file mode 100644 index e9e5bdc..0000000 --- a/lib/lennarb/version.rb +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true - -module Lennarb - VERSION = '0.1.0' - public_constant :VERSION -end