Class: LeanWeb::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/leanweb/route.rb

Overview

A single route which routes with the #respond method. It can also #build an static file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, method: 'GET', action: nil, static: nil) ⇒ Route

A new instance of Route.

Parameters:

  • path (String, Regexp)

    Path matcher, can be an String or Regexp with positional or named capture groups, @action will receive these as positional or named arguments. Always begins with /.

  • method (String, nil) (defaults to: 'GET')

    Must be an HTTP verb such as GET or POST.

  • action (Proc, Hash, String, nil) (defaults to: nil)

    References a Method/Proc to be triggered. It can be:

    • Nothing, defaults to :”#path_basename_#method” (such as :index_get) on :MainController.
    • A full hash { controller: 'val', action: 'val' }.
    • A hash with { Controller: :action } only.
    • A simple string for an action on :MainController.
    • It can also be a Proc. Will be executed in a :MainController instance context.
  • static (Boolean|Array) (defaults to: nil)

    Defines a route as static. Defaults true for GET method, false otherwise. Set to false to say it can only work dynamically. You can also supply an array of arrays or hashes to generate static files based on that positional or keyword params.



44
45
46
47
48
49
# File 'lib/leanweb/route.rb', line 44

def initialize(path:, method: 'GET', action: nil, static: nil)
  @path = path
  @method = method
  self.action = action
  @static = static.nil? ? @method == 'GET' : static
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



21
22
23
# File 'lib/leanweb/route.rb', line 21

def action
  @action
end

#methodObject (readonly)

Returns the value of attribute method.



21
22
23
# File 'lib/leanweb/route.rb', line 21

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/leanweb/route.rb', line 21

def path
  @path
end

#staticObject (readonly)

Returns the value of attribute static.



21
22
23
# File 'lib/leanweb/route.rb', line 21

def static
  @static
end

Instance Method Details

#available?Boolean

Check if route is available.

If on production environment (not development), should serve only dynamic (not static) routes.

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/leanweb/route.rb', line 55

def available?
  return @static == false if ENV['RACK_ENV'] != 'development'

  true
end

#build(request_path = @path) ⇒ Object

Build this route as an static file and place it relative to PUBLIC_PATH.

Parameters:

  • request_path (String) (defaults to: @path)

    Request path for dynamic (regex) routes.



107
108
109
110
111
112
113
114
115
# File 'lib/leanweb/route.rb', line 107

def build(request_path = @path)
  response = respond(
    Rack::Request.new(Rack::MockRequest.env_for(request_path))
  )
  out_path = output_path(request_path, response[1]['Content-Type'] || nil)
  FileUtils.mkdir_p(File.dirname(out_path))

  File.write(out_path, response[2][0])
end

#path_basenameObject

Last identifier on a path, returns index for /.



62
63
64
# File 'lib/leanweb/route.rb', line 62

def path_basename
  str_path[-1] == '/' ? 'index' : File.basename(str_path)
end

#respond(request) ⇒ Array

Respond with a proc, controller method, or in case of true static routes a rendering of VIEW_PATH/#path with any file extension.

Parameters:

  • request (Rack::Request)

Returns:

  • (Array)

    a valid rack response.



71
72
73
74
75
76
77
78
79
80
# File 'lib/leanweb/route.rb', line 71

def respond(request)
  return respond_proc(request) if @action.instance_of?(Proc)

  respond_method(request)
rescue NoMethodError
  raise unless @static == true && (view_path = guess_view_path)

  controller = default_controller_class.new(self)
  controller.default_static_action(view_path)
end

#seed_path(seed) ⇒ String

On Regexp paths, return a string valid for making a request to this route.

Parameters:

  • seed (Array, Hash)

    Seeds to use as replacement on capture groups.

Returns:

  • (String)

    sown path.



93
94
95
96
97
98
99
100
101
# File 'lib/leanweb/route.rb', line 93

def seed_path(seed)
  sown_path = str_path
  if seed.instance_of?(Hash)
    seed.each{ |key, val| sown_path.sub!(/\(\?<#{key}>[^)]+\)/, val) }
  else
    seed.each{ |val| sown_path.sub!(/\([^)]+\)/, val) }
  end
  sown_path
end

#str_pathObject

String path, independent if #path is Regexp or String.



83
84
85
86
87
# File 'lib/leanweb/route.rb', line 83

def str_path
  @path.source.gsub(/[\^$]/, '')
rescue NoMethodError
  @path
end