wouter 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wouter.rb +67 -40
  3. metadata +27 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb608868bb7ca95a7e244c723598b71ce229d5a3bf0547e98b8845d0d3c5ee09
4
- data.tar.gz: b20aa9ec15c8cf94d0700dc0c5e618161dacdce1b9f2dd0aa7dae50e2baaacd3
3
+ metadata.gz: 4731ee9735261f806ded54813c9655fcae91349f6c3b9a5061f0664ac2618b72
4
+ data.tar.gz: 0bb29c4009bcc7846e07dfad0dae5c8c0ba4af3ceaa5bf1607ca4f89590e3e59
5
5
  SHA512:
6
- metadata.gz: ba6b29440962dd0b887d7d933b8a6445da9e3b9f4b8be6195a1c915cb235d39e1b5a49f1de8a5ba4f487586067919d81126b4ad38c6f4266bfc06939980f3cee
7
- data.tar.gz: 0caa411508ceb2d28b7bf69e52162026b1234b974a9d6cfd8c0a94d77ede7ac89e187870d30ee3714d70fab22085f4c11096a5d4fb8305dd819e658e0bd22be7
6
+ metadata.gz: 81a9adf9928ce8af300ede523ed9fad8ac9c657ccee665fae108b4f13d54c3fa77a11c61cefdd3d4bceae992d4f68e8cf2e65e6f103dafd0b2287ca2e310fb1a
7
+ data.tar.gz: ad519803a201b62cbb9e9b1d93365329a74d0517859dd60fff1ca8b10088d86788baad5d9d5903aa04df8dbdd423bcf26f88433e3efd1f9e334ece9037ee59c0
data/lib/wouter.rb CHANGED
@@ -1,12 +1,11 @@
1
- require "bundler"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler'
2
4
  Bundler.setup(:default)
3
- require "rack"
5
+ require 'rack'
4
6
 
7
+ # Wouter main class to define routes from
5
8
  class Wouter
6
- # ========== Rack wrappers
7
- class Request < Rack::Request; end
8
- class Response < Rack::Response; end
9
-
10
9
  # ========== Endpoint class to make life easier
11
10
  class Endpoint
12
11
  attr_accessor :req, :res
@@ -21,7 +20,7 @@ class Wouter
21
20
  def self.call(env)
22
21
  endpoint = new(Rack::Request.new(env), Rack::Response.new)
23
22
  final_resp = endpoint.respond
24
- # Return a `Rack::Response` or write to the body of the response if the return value of `response` is not `Rack::Response`
23
+ # Return a `Rack::Response` or write to the body of the response
25
24
  if final_resp.is_a?(Rack::Response)
26
25
  final_resp
27
26
  else
@@ -68,11 +67,12 @@ class Wouter
68
67
  class <<self
69
68
  %i[get post put delete].each do |m|
70
69
  define_method(m) do |path, route_class|
71
- routes.push({
70
+ route = {
72
71
  method: m.to_s.upcase.to_sym,
73
72
  path: path,
74
73
  route_class: route_class
75
- })
74
+ }
75
+ routes.push(route)
76
76
  end
77
77
  end
78
78
 
@@ -88,6 +88,7 @@ class Wouter
88
88
  app.to_app
89
89
  end
90
90
 
91
+ # Wrapper class for creating single `.call` interface for `Rack::Builder`
91
92
  class Wrapper
92
93
  attr_reader :routes
93
94
 
@@ -98,58 +99,84 @@ class Wouter
98
99
  def call(env)
99
100
  request = Rack::Request.new(env)
100
101
 
101
- route = routes.find do |route|
102
- if route[:method] == request.request_method.to_sym
103
- if parameterized_path?(route[:path])
104
- split_path = route[:path].split("/")
105
- path_regex = build_path_regex(split_path)
106
- path_regex.match?(request.path)
107
- else
108
- route[:path] == request.path
109
- end
110
- end
102
+ route = routes.find do |r|
103
+ matching_route?(r, request)
111
104
  end
112
105
 
113
- if route
114
- if parameterized_path?(route[:path])
115
- split_path = route[:path].split("/")
116
- path_parameter_data = build_path_regex(split_path).match(request.path)
117
- path_parameter_names = find_parameter_names_in_path(split_path)
118
- path_parameter_names.each_with_index do |parameter_name, i|
119
- request.update_param(parameter_name, path_parameter_data[i+1])
120
- end
121
- end
106
+ return not_found unless route
122
107
 
123
- resp = route[:route_class].call(request.env)
124
- resp.finish
125
- else
126
- not_found
108
+ path_parameters(route, request.path) do |name, value|
109
+ request.update_param(name, value)
127
110
  end
111
+
112
+ resp = route[:route_class].call(request.env)
113
+ resp.finish
128
114
  end
129
115
 
130
116
  private
131
117
 
132
- # Does the path have a parameter in it? ex: "/user/:id"
118
+ # Does the given route match the request
119
+ def matching_route?(route, request)
120
+ return false unless route[:method] == request.request_method.to_sym
121
+
122
+ if parameterized_path?(route[:path])
123
+ path_regex = build_path_regex(route[:path].split('/'))
124
+ path_regex.match?(request.path)
125
+ else
126
+ route[:path] == request.path
127
+ end
128
+ end
129
+
130
+ # Yield the parameters parsed from the path
131
+ # Example definition: "/hello/:name"
132
+ # Example request: "/hello/robert"
133
+ # Example yield: name, robert
134
+ def path_parameters(route, path)
135
+ return unless parameterized_path?(route[:path])
136
+
137
+ split_path = route[:path].split('/')
138
+ path_parameter_data = build_path_regex(split_path).match(path)
139
+ path_parameter_names = find_parameter_names_in_path(split_path)
140
+ path_parameter_names.each_with_index do |parameter_name, i|
141
+ yield parameter_name, path_parameter_data[i + 1]
142
+ end
143
+ end
144
+
145
+ # Does the path have a parameter in it? ex: '/user/:id'
133
146
  def parameterized_path?(path)
134
- path.include?(":")
147
+ path.include?(':')
135
148
  end
136
149
 
137
- # Find all the named parameters in the route, drop the ":" so we have the names: ":id" => "id"
150
+ # Find all the named parameters in the route
151
+ # Then drop the ':' so we have the names: ':id' => 'id'
138
152
  def find_parameter_names_in_path(split_path)
139
- split_path.find_all { |s| s.size > 1 ? s[0] == ":" : false }.map { |s| s[1..-1] }
153
+ params = split_path.find_all do |s|
154
+ if s.size > 1
155
+ s[0] == ':'
156
+ else
157
+ false
158
+ end
159
+ end
160
+ params.map { |s| s[1..-1] }
140
161
  end
141
162
 
142
- # Turn the route into a regex: "/hello/:name" => "\/hello\/(\w*)"
163
+ # Turn the route into a regex: '/hello/:name' => '\/hello\/(\w*)'
143
164
  def build_path_regex(split_path)
144
- regex_string = split_path.map { |s| s[0] == ":" ? "(\\w*)" : s }.join("\/")
145
- Regexp.new(regex_string)
165
+ regex_string = split_path.map do |s|
166
+ if s[0] == ':'
167
+ '(\\w*)'
168
+ else
169
+ s
170
+ end
171
+ end
172
+ Regexp.new(regex_string.join('\/'))
146
173
  end
147
174
 
148
175
  # Return HTTP 404
149
176
  def not_found
150
177
  resp = Rack::Response.new
151
178
  resp.status = 404
152
- resp.write "Not Found"
179
+ resp.write 'Not Found'
153
180
  resp
154
181
  end
155
182
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wouter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Peterson
@@ -31,107 +31,106 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: 2.0.6
33
33
  - !ruby/object:Gem::Dependency
34
- name: rake
34
+ name: m
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '12.3'
39
+ version: '1.5'
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: 12.3.1
42
+ version: 1.5.1
43
43
  type: :development
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - "~>"
48
48
  - !ruby/object:Gem::Version
49
- version: '12.3'
49
+ version: '1.5'
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: 12.3.1
52
+ version: 1.5.1
53
53
  - !ruby/object:Gem::Dependency
54
- name: rack-test
54
+ name: minitest
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
57
  - - "~>"
58
58
  - !ruby/object:Gem::Version
59
- version: '1.1'
59
+ version: '5.11'
60
60
  - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: 1.1.0
62
+ version: 5.11.3
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '1.1'
69
+ version: '5.11'
70
70
  - - ">="
71
71
  - !ruby/object:Gem::Version
72
- version: 1.1.0
72
+ version: 5.11.3
73
73
  - !ruby/object:Gem::Dependency
74
- name: minitest
74
+ name: minitest-rg
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - "~>"
78
78
  - !ruby/object:Gem::Version
79
- version: '5.11'
79
+ version: '5.2'
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 5.11.3
82
+ version: 5.2.0
83
83
  type: :development
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '5.11'
89
+ version: '5.2'
90
90
  - - ">="
91
91
  - !ruby/object:Gem::Version
92
- version: 5.11.3
92
+ version: 5.2.0
93
93
  - !ruby/object:Gem::Dependency
94
- name: minitest-rg
94
+ name: rack-test
95
95
  requirement: !ruby/object:Gem::Requirement
96
96
  requirements:
97
97
  - - "~>"
98
98
  - !ruby/object:Gem::Version
99
- version: '5.2'
99
+ version: '1.1'
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
- version: 5.2.0
102
+ version: 1.1.0
103
103
  type: :development
104
104
  prerelease: false
105
105
  version_requirements: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - "~>"
108
108
  - !ruby/object:Gem::Version
109
- version: '5.2'
109
+ version: '1.1'
110
110
  - - ">="
111
111
  - !ruby/object:Gem::Version
112
- version: 5.2.0
112
+ version: 1.1.0
113
113
  - !ruby/object:Gem::Dependency
114
- name: m
114
+ name: rake
115
115
  requirement: !ruby/object:Gem::Requirement
116
116
  requirements:
117
117
  - - "~>"
118
118
  - !ruby/object:Gem::Version
119
- version: '1.5'
119
+ version: '12.3'
120
120
  - - ">="
121
121
  - !ruby/object:Gem::Version
122
- version: 1.5.1
122
+ version: 12.3.1
123
123
  type: :development
124
124
  prerelease: false
125
125
  version_requirements: !ruby/object:Gem::Requirement
126
126
  requirements:
127
127
  - - "~>"
128
128
  - !ruby/object:Gem::Version
129
- version: '1.5'
129
+ version: '12.3'
130
130
  - - ">="
131
131
  - !ruby/object:Gem::Version
132
- version: 1.5.1
133
- description: Wouter is a modular web router built on Rack. I like explicitness. I
134
- like modularity. Wouter aims to make both of those aspects simple.
132
+ version: 12.3.1
133
+ description: Wouter is a modular web router built on Rack.
135
134
  email: robertpeterson@gmail.com
136
135
  executables: []
137
136
  extensions: []