wouter 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wouter.rb +29 -64
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a103da6e9f11d0e96881ccaf75062ef5a6a5322e78e3010300465645d5a9bd62
4
- data.tar.gz: e10e000a61b9a304156429f1c22244ab6af4b456bb17d1960a901eb9c1579a4d
3
+ metadata.gz: 83e0fd10fd5d4cfd564ab573dc0602bc7752f8fc9219e7b4ef988ce8879e0902
4
+ data.tar.gz: 6e7613daede1879b938dbf481827b27ad15b52905a72c5742c1b16b8935f5ce0
5
5
  SHA512:
6
- metadata.gz: 3ae45cbb20ba4659e9ef46d44fdb9deafef6fb252ab17acdad3eef68a4a3f446113c2641e672e165d3b9e97cf32d8399932141f4db223a0bbcbfb8f0bcd9c6db
7
- data.tar.gz: 95239729b7a893187eb60d126bbd75aab7901718c21e28b47053c491bea3b0eb613539618f2245b69b55cdb4a18a0f3155781179216d173c19f6cd47d2c680f4
6
+ metadata.gz: d73792353400cd5de74b1da3b57eace57bba19e7cb517401a3d09416a8be22e5c3b6a36a6ab8090af85334282c8bf2d8aece9299fc79fc7135ee37f8235f4560
7
+ data.tar.gz: ad2264f42d38ad8f02465a1e235125e3815306d868b53a37a7209880177b7a1844d85423f16879cbe5d50fc1b2f02f28a3287ea698bbe4ae7e05e55017ada17d
data/lib/wouter.rb CHANGED
@@ -5,37 +5,11 @@ require "rack"
5
5
  class Wouter
6
6
 
7
7
  # Incoming HTTP Request Wrapper
8
- class Request
9
- attr_reader :params
10
-
11
- def initialize(params:)
12
- @params = params
13
- end
8
+ class Request < Rack::Request
14
9
  end
15
10
 
16
11
  # Outgoing HTTP Response Wrapper
17
- class Response
18
- attr_accessor :headers
19
-
20
- def initialize
21
- @headers = {'Content-Type' => 'text/html'}
22
- @body = ""
23
- @status = 200
24
- end
25
-
26
- def body(b)
27
- @body = b
28
- self
29
- end
30
-
31
- def status(s)
32
- @status = s
33
- self
34
- end
35
-
36
- def build_response
37
- [@status, @headers, [@body]]
38
- end
12
+ class Response < Rack::Response
39
13
  end
40
14
 
41
15
  # HTTP Endpoint Helper class
@@ -44,8 +18,9 @@ class Wouter
44
18
  attr_reader :params
45
19
 
46
20
  def initialize
47
- @request = request
48
- @response = response
21
+ @request = nil
22
+ @response = nil
23
+ @params = nil
49
24
  end
50
25
 
51
26
  def call(req, res)
@@ -53,30 +28,33 @@ class Wouter
53
28
  @params = req.params
54
29
  @response = res
55
30
  r = respond
56
- @response.body r
31
+ @response.write r
32
+ @response
57
33
  end
58
34
 
59
35
  def json(body)
60
- @response.headers['Content-Type'] = 'application/json'
36
+ @response.set_header('Content-Type', 'application/json')
61
37
  body
62
38
  end
63
39
 
64
40
  def status(code)
65
- @response.status code
41
+ @response.status = code
66
42
  ""
67
43
  end
68
44
  end
69
45
 
70
- ## Route Definitions
46
+ ## Internal Data
71
47
 
72
- @@routes = []
48
+ def self.routes
49
+ @routes ||= []
50
+ end
73
51
 
74
52
  ## DSL
75
53
 
76
54
  class <<self
77
55
  %i[get post put delete].each do |m|
78
56
  define_method(m) do |path, route_class|
79
- @@routes.push({
57
+ routes.push({
80
58
  method: m.to_s.upcase.to_sym,
81
59
  path: path,
82
60
  route_class: route_class
@@ -88,13 +66,12 @@ class Wouter
88
66
  ## Rack API
89
67
 
90
68
  def self.call(env)
91
- path = env["PATH_INFO"]
92
- method = env["REQUEST_METHOD"].to_sym
69
+ request = Request.new(env)
93
70
 
94
71
  route_params = {}
95
72
 
96
- route = @@routes.find do |route|
97
- if route[:method] == method
73
+ route = routes.find do |route|
74
+ if route[:method] == request.request_method.to_sym
98
75
  if route[:path].include?(":")
99
76
  split_path = route[:path].split("/")
100
77
  # Find all the named parameters in the route, drop the ":" so we have the names: ":id" => "id"
@@ -102,8 +79,8 @@ class Wouter
102
79
  # Turn the route into a regex: "/hello/:name" => "\/hello\/(\w*)"
103
80
  path_regex_string = split_path.map { |s| s[0] == ":" ? "(\\w*)" : s }.join("\/")
104
81
  r = Regexp.new(path_regex_string)
105
- if r.match?(path)
106
- match_data = r.match(path)
82
+ if r.match?(request.path)
83
+ match_data = r.match(request.path)
107
84
  # Match the match data with the named params, ex { "id" => 123 }
108
85
  route_param_names.each_with_index do |n, i|
109
86
  route_params[n] = match_data[i+1]
@@ -113,21 +90,22 @@ class Wouter
113
90
  false
114
91
  end
115
92
  else
116
- route[:path] == path
93
+ route[:path] == request.path
117
94
  end
118
95
  end
119
96
  end
120
97
 
121
98
  if route
122
- params = generate_params(env["rack.input"].gets).merge(route_params)
99
+ route_params.each do |k, v|
100
+ request.update_param(k, v)
101
+ end
123
102
 
124
- request = Request.new(params: params)
125
103
  response = Response.new
126
104
 
127
- route_response = route[:route_class].new.call(request, response)
128
- response route_response
105
+ rack_response = route[:route_class].new.call(request, response)
106
+ rack_response.finish
129
107
  else
130
- response not_found
108
+ not_found.finish
131
109
  end
132
110
  end
133
111
 
@@ -135,22 +113,9 @@ class Wouter
135
113
 
136
114
  def self.not_found
137
115
  resp = Response.new
138
- resp.status 404
139
- resp.body "Not Found"
116
+ resp.status = 404
117
+ resp.write "Not Found"
118
+ resp
140
119
  end
141
120
 
142
- # Query string comes in as "key=value&key=value" and we turn it into { "key" => "value", "key" => "value" }
143
- def self.generate_params(params)
144
- return {} if params.nil? || params.empty?
145
- params_hash = {}
146
- params.split("&").each do |param_pair|
147
- key, value = param_pair.split("=")
148
- params_hash[key] = value
149
- end
150
- params_hash
151
- end
152
-
153
- def self.response(route_response)
154
- route_response.build_response
155
- end
156
121
  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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Peterson