From 000e53e742a88f2ef8a4bb3f5cb7094d1fa95d80 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Mon, 16 Oct 2023 15:29:39 +0400 Subject: [PATCH] Fixed import function, fixed documentation warnings issued by YARD --- landline.gemspec | 2 +- lib/landline/dsl/constructors_probe.rb | 4 ++-- lib/landline/dsl/methods_path.rb | 7 ++++--- lib/landline/dsl/methods_template.rb | 11 ++++++++++- lib/landline/node.rb | 2 +- lib/landline/pattern_matching/glob.rb | 2 +- lib/landline/pattern_matching/rematch.rb | 2 +- lib/landline/probe/handler.rb | 2 +- lib/landline/probe/serve_handler.rb | 1 - lib/landline/template.rb | 13 ++++++++----- lib/landline/template/erb.rb | 2 +- lib/landline/template/erubi.rb | 2 +- lib/landline/util/mime.rb | 2 +- lib/landline/util/multipart.rb | 11 +++++++---- 14 files changed, 39 insertions(+), 24 deletions(-) diff --git a/landline.gemspec b/landline.gemspec index 2af2743..752b621 100644 --- a/landline.gemspec +++ b/landline.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |spec| spec.name = "landline" - spec.version = "0.9.3" + spec.version = "0.10.0" spec.summary = "Elegant HTTP DSL" spec.description = <<~DESC Landline is a no-hard-dependencies HTTP routing DSL that was made entirely for fun. diff --git a/lib/landline/dsl/constructors_probe.rb b/lib/landline/dsl/constructors_probe.rb index 5d46b2b..023aacb 100644 --- a/lib/landline/dsl/constructors_probe.rb +++ b/lib/landline/dsl/constructors_probe.rb @@ -5,7 +5,7 @@ module Landline # Probe (and subclasses) DSL construct module ProbeConstructors # Create a new erb template - # @see {Landline::Template#new} + # @see Landline::Template#new def erb(input, vars = {}) Landline::Templates::ERB.new(input, vars, @@ -14,7 +14,7 @@ module Landline end # Create a new erb template using Erubi engine - # @see {Landline::Template#new} + # @see Landline::Template#new # @param freeze [Boolean] whether to use frozen string literal # @param capture [Boolean] whether to enable output capturing def erubi(input, vars = {}, freeze: true, capture: false) diff --git a/lib/landline/dsl/methods_path.rb b/lib/landline/dsl/methods_path.rb index 04c9449..8412455 100644 --- a/lib/landline/dsl/methods_path.rb +++ b/lib/landline/dsl/methods_path.rb @@ -12,9 +12,10 @@ module Landline # Create a status code handler on path. # Recursively applies to all paths unless overridden. - # @param [Integer, nil] Specify a status code to handle - def handle(code = "default", &block) - @origin.properties["handle.#{code}"] = block + # @param code [Integer, nil] Specify a status code to handle + # @param block [#call] Block to run on given code + def handle(code = nil, &block) + @origin.properties["handle.#{code || 'default'}"] = block end # Insert a pass-through pipeline into request processing diff --git a/lib/landline/dsl/methods_template.rb b/lib/landline/dsl/methods_template.rb index c3b1bff..b9f8e3a 100644 --- a/lib/landline/dsl/methods_template.rb +++ b/lib/landline/dsl/methods_template.rb @@ -8,8 +8,17 @@ module Landline # Common methods for template contexts module TemplateMethods # Import a template part + # @param filepath [String, File] path to the file (or the file itself) + # @return [String] compiled template def import(filepath) - @parent_template.import(file(filepath)).run + template = if filepath.is_a? File + filepath + elsif filepath.start_with? "/" + File.open(filepath) + else + file(filepath) + end + @parent_template.import(template).run end end end diff --git a/lib/landline/node.rb b/lib/landline/node.rb index 55fe2bf..0e870dc 100644 --- a/lib/landline/node.rb +++ b/lib/landline/node.rb @@ -35,7 +35,7 @@ module Landline end # Try to navigate the path. Run method callback in response. - # @param [Landline::Request] + # @param request [Landline::Request] # @return [Boolean] def go(request) # rejected at pattern diff --git a/lib/landline/pattern_matching/glob.rb b/lib/landline/pattern_matching/glob.rb index b511b11..050aff8 100644 --- a/lib/landline/pattern_matching/glob.rb +++ b/lib/landline/pattern_matching/glob.rb @@ -52,7 +52,7 @@ module Landline # - underscores # - dashes class Glob - # @param input [String] Glob pattern + # @param pattern [String] Glob pattern def initialize(pattern) pattern = Landline::PatternMatching.canonicalize(pattern) pieces = pattern.split(TOKENS) diff --git a/lib/landline/pattern_matching/rematch.rb b/lib/landline/pattern_matching/rematch.rb index b1200f7..defaaf3 100644 --- a/lib/landline/pattern_matching/rematch.rb +++ b/lib/landline/pattern_matching/rematch.rb @@ -39,7 +39,7 @@ module Landline end # Test if input is convertible and if it should be converted. - # @param input + # @param string [String] input to test conversion on # @return [Boolean] Input can be safely converted to Glob def self.can_convert?(string) string.is_a? Regexp diff --git a/lib/landline/probe/handler.rb b/lib/landline/probe/handler.rb index b51b945..a69f05d 100644 --- a/lib/landline/probe/handler.rb +++ b/lib/landline/probe/handler.rb @@ -7,7 +7,7 @@ module Landline # Probe that executes a callback on request class Handler < Landline::Probe # @param path [Object] - # @param parent [Landline::Node] + # @param args [Hash] hashed parameters to passthrough to Probe # @param exec [#call] def initialize(path, **args, &exec) super(path, **args) diff --git a/lib/landline/probe/serve_handler.rb b/lib/landline/probe/serve_handler.rb index 4c04107..a82e16f 100644 --- a/lib/landline/probe/serve_handler.rb +++ b/lib/landline/probe/serve_handler.rb @@ -9,7 +9,6 @@ module Landline class Serve < Landline::Probe # @param path [Object] # @param parent [Landline::Node] - # @param exec [#call] def initialize(path, parent:) super(path, parent: parent, filepath: true) end diff --git a/lib/landline/template.rb b/lib/landline/template.rb index 90a0b2b..42beed9 100644 --- a/lib/landline/template.rb +++ b/lib/landline/template.rb @@ -86,11 +86,14 @@ module Landline # ... (stub) end - # Import a template from within current template - def import(filepath) - filepath = filepath.is_a? File ? filepath : File.open(filepath) - # @sg-ignore - newtemp = self.class.new(filepath, {}, parent: @parent) + # Import a template from within current template. + # @param file [File] template file to import + # @return [Landline::Template] loaded tempalte + def import(file) + newtemp = self.class.new(file, + {}, + parent: @parent, + filename: file.path) newtemp.binding = @binding newtemp end diff --git a/lib/landline/template/erb.rb b/lib/landline/template/erb.rb index 2c11211..757bdc9 100644 --- a/lib/landline/template/erb.rb +++ b/lib/landline/template/erb.rb @@ -7,7 +7,7 @@ module Landline module Templates # ERB Template language adapter class ERB < Landline::Template - # @see {Landline::Template#new} + # (see Landline::Template#new) def initialize(input, vars = nil, parent:, filename:) super varname = "_part_#{SecureRandom.hex(10)}".to_sym diff --git a/lib/landline/template/erubi.rb b/lib/landline/template/erubi.rb index 4e0596d..c023b5e 100644 --- a/lib/landline/template/erubi.rb +++ b/lib/landline/template/erubi.rb @@ -7,7 +7,7 @@ module Landline module Templates # Erubi (ERB) template language adapter class Erubi < Landline::Template - # @see {Landline::Template#new} + # (see Landline::Template#new) def initialize(input, vars = nil, **ext) diff --git a/lib/landline/util/mime.rb b/lib/landline/util/mime.rb index 47b4a75..3e43667 100644 --- a/lib/landline/util/mime.rb +++ b/lib/landline/util/mime.rb @@ -1267,7 +1267,7 @@ module Landline }.freeze # Get MIME type by file extension - # @param ext [String] filename + # @param file [String] filename # @return [String] MIME type, defaults to "application/octet-stream" def self.get_mime_type(file) MIME[file.match(/\.(\w+)$/)&.[](1)] or "application/octet-stream" diff --git a/lib/landline/util/multipart.rb b/lib/landline/util/multipart.rb index 8c173a2..4ed8617 100644 --- a/lib/landline/util/multipart.rb +++ b/lib/landline/util/multipart.rb @@ -9,6 +9,8 @@ require_relative 'html' module Landline module Util + FormPart = Struct.new(:data, :name, :filename, + :filetype, :tempfile, :headers) # Valid element of form data with headers # @!attribute headers [Hash] headers recevied from form data # @!attribute name [String] name of the form part @@ -16,8 +18,7 @@ module Landline # @!attribute filename [String,nil] Original name of the sent file # @!attribute filetype [String,nil] MIME-type of the file # @!attribute tempfile [File,nil] Temporary file for storing sent file data. - FormPart = Struct.new(:data, :name, :filename, - :filetype, :tempfile, :headers) do + class FormPart # Is this form part a file or plain data? # @return [Boolean] def file? @@ -135,7 +136,8 @@ module Landline end # Setup file metadata - # @part part [FormPart] + # @param part [Landline::Util::FormPart] + # @return [void] def setup_file_meta(part) part.name = part.headers.dig("content-disposition", 1, "name") part.filename = part.headers.dig("content-disposition", 1, "filename") @@ -144,7 +146,8 @@ module Landline end # Setup plain metadata - # @part part [FormPart] + # @param part [Landline::Util::FormPart] + # @return [void] def setup_data_meta(part) part.name = part.headers.dig("content-disposition", 1, "name") end