Class: LeanWeb::Route
- Inherits:
-
Object
- Object
- LeanWeb::Route
- Defined in:
- lib/leanweb/route.rb
Overview
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#static ⇒ Object
readonly
Returns the value of attribute static.
Instance Method Summary collapse
-
#available? ⇒ Boolean
Check if route is available.
-
#build(request_path = @path) ⇒ Object
Build this route as an static file and place it relative to PUBLIC_PATH.
-
#initialize(path:, method: 'GET', action: nil, static: nil) ⇒ Route
constructor
A new instance of Route.
-
#path_basename ⇒ Object
Last identifier on a path, returns ‘index` for `/`.
- #respond(request) ⇒ Array
-
#seed_path(seed) ⇒ String
On Regexp paths, return a string valid for making a request to this route.
-
#str_path ⇒ Object
String path, independent if #path is Regexp or String.
Constructor Details
#initialize(path:, method: 'GET', action: nil, static: nil) ⇒ Route
A new instance of Route.
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
#action ⇒ Object
Returns the value of attribute action.
21 22 23 |
# File 'lib/leanweb/route.rb', line 21 def action @action end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
21 22 23 |
# File 'lib/leanweb/route.rb', line 21 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
21 22 23 |
# File 'lib/leanweb/route.rb', line 21 def path @path end |
#static ⇒ Object (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.
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
{LeanWeb::PUBLIC_PATH}.
102 103 104 105 106 107 108 109 110 |
# File 'lib/leanweb/route.rb', line 102 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_basename ⇒ Object
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
71 72 73 74 75 |
# File 'lib/leanweb/route.rb', line 71 def respond(request) return respond_proc(request) if @action.instance_of?(Proc) respond_method(request) end |
#seed_path(seed) ⇒ String
On Regexp paths, return a string valid for making a request to this route.
88 89 90 91 92 93 94 95 96 |
# File 'lib/leanweb/route.rb', line 88 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_path ⇒ Object
String path, independent if #path is Regexp or String.
78 79 80 81 82 |
# File 'lib/leanweb/route.rb', line 78 def str_path @path.source.gsub(/[\^$]/, '') rescue NoMethodError @path end |