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:

    • A full hash { file: 'val', controller: 'val', action: 'val' }.
    • A hash with { 'file' => 'action' } only (which has a controller class name {File}Controller).
    • A simple string (which will consider file main.rb and controller MainController). Defaults to "#path_basename_#method", (ex: index_get).
    • It can also be a Proc.
  • 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.



42
43
44
45
46
47
# File 'lib/leanweb/route.rb', line 42

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.



19
20
21
# File 'lib/leanweb/route.rb', line 19

def action
  @action
end

#methodObject (readonly)

Returns the value of attribute method.



19
20
21
# File 'lib/leanweb/route.rb', line 19

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/leanweb/route.rb', line 19

def path
  @path
end

#staticObject (readonly)

Returns the value of attribute static.



19
20
21
# File 'lib/leanweb/route.rb', line 19

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)


53
54
55
56
57
# File 'lib/leanweb/route.rb', line 53

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.



97
98
99
100
101
102
103
104
105
# File 'lib/leanweb/route.rb', line 97

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 /.



60
61
62
# File 'lib/leanweb/route.rb', line 60

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

#respond(request) ⇒ Array

Returns a valid rack response.

Parameters:

  • request (Rack::Request)

Returns:

  • (Array)

    a valid rack response.



66
67
68
69
70
# File 'lib/leanweb/route.rb', line 66

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.

Parameters:

  • seed (Array, Hash)

    Seeds to use as replacement on capture groups.

Returns:



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

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.



73
74
75
76
77
# File 'lib/leanweb/route.rb', line 73

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