kawaii-core 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95acd16e37137a291448035f6d07d4e57fd176a8
4
- data.tar.gz: 59e1181f5923fe52949358ef933b54979fd9e97c
3
+ metadata.gz: d81d0ba32a44a5b7eceec6b3979cd33f58970ec3
4
+ data.tar.gz: 8da2f45e4fc4e5291372f5f5c46edfb1139e849e
5
5
  SHA512:
6
- metadata.gz: 82bc0bf00758bfc60a1659c6cf97e7de0a9252a1538a43ea8060b5742cc7d90dc6a146342c51c4fc1a046fd146904ae10c141f74d2a3d3bfbab090c8b24d0c1b
7
- data.tar.gz: aae617e17cbc4c848d24c02f9d0aadfc6840d3986fcc79934f2811686d40843386d9a5d246aced280736cc29e20f02966ab892c2a02fa2229a4e7e4443970e42
6
+ metadata.gz: 4b583cd1e65bbbc8b8f73ea402fde6e5a706d31778ebfdfd801590af617ee74dd2b3cd8985f1eaa3c42c562610f7f47b181b27a8a45fce7d53c595a9f425f1fb
7
+ data.tar.gz: 368d2864fe554d1923dea8e4cb438694c522eb618316d8feca27554ba3f25578eac98dbf884463bd1e1117cce89017d70697d7a74f108b28e57d431830328294
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Kawaii is a simple web framework based on Rack.
5
5
 
6
- ** This is work in progress. The API is subject to change. **
6
+ **This is work in progress. The API is subject to change.**
7
7
 
8
8
  ## Installation
9
9
 
@@ -23,26 +23,34 @@ Or install it yourself as:
23
23
 
24
24
  ## Running examples
25
25
 
26
- The `/examples` directory contains various basic usage examples.
26
+ Clone the Kawaii project to run the examples. The [/examples directory](https://github.com/bilus/kawaii/tree/master/examples) contains various basic usage examples.
27
+
28
+ ```
29
+ $ git clone https://github.com/bilus/kawaii.git
30
+ $ cd kawaii
31
+ ```
27
32
 
28
33
  Run the examples using rackup or directly:
29
34
 
30
35
  ```
31
36
  $ cd examples
32
- $ rackup -r kawaii modular.ru
37
+ $ rackup -I ../lib modular.ru
33
38
  ```
34
39
 
35
40
  Many examples can also be run directly without rackup, e.g.:
36
41
 
37
- ```
42
+ ```ruby
38
43
  $ cd examples
39
- $ ruby -r kawaii hello_world.rb
44
+ $ ruby -I ../lib hello_world.rb
45
+ ```
40
46
 
41
47
  ## Getting started
42
48
 
49
+ Note: In addition to this Readme, there's also an online [API reference](http://bilus.github.io/kawaii/Kawaii.html).
50
+
43
51
  Kawaii's basic usage is very similar how you'd use Sinatra. You can define route handlers at the file scope. Here's an example:
44
52
 
45
- ```
53
+ ```ruby
46
54
  require 'kawaii'
47
55
 
48
56
  get '/' do
@@ -62,7 +70,7 @@ Then navigate to `http://localhost:8088` to see the greeting.
62
70
 
63
71
  To run the app you created in the "Getting started" section above using rackup, create the following `hello.ru` file:
64
72
 
65
- ```
73
+ ```ruby
66
74
  require 'kawaii'
67
75
  require_relative 'hello'
68
76
 
@@ -79,7 +87,7 @@ There are several methods you can use to build your routes, handle passed parame
79
87
 
80
88
  The basic way to add a route handler is to invoke a method corresponding to the given HTTP verb, e.g.:
81
89
 
82
- ```
90
+ ```ruby
83
91
  post '/users' do
84
92
  # Some response
85
93
  end
@@ -110,7 +118,7 @@ For example `get '/users/?'` matches both `/users/` and `/users` while `get '/us
110
118
 
111
119
  Route patterns may contain named parameters, prefixed with a colon. Parameters are accessible through the `params` hash in handler:
112
120
 
113
- ```
121
+ ```ruby
114
122
  get '/users/:id' do
115
123
  params[:id]
116
124
  end
@@ -122,7 +130,7 @@ end
122
130
 
123
131
  Route patterns may contain regular expressions. Example:
124
132
 
125
- ```
133
+ ```ruby
126
134
  get %r{/users/.*} do
127
135
  'Hello, world'
128
136
  end
@@ -132,7 +140,7 @@ end
132
140
 
133
141
  Routes may be nested using the `context` method. Example:
134
142
 
135
- ```
143
+ ```ruby
136
144
  context '/api' do
137
145
  get '/users' do
138
146
  'Hello'
@@ -146,13 +154,13 @@ Will above handler will be accessible through `/api/users`.
146
154
 
147
155
  If string patterns and regular expression are not flexible enough, you can create a custom matcher.
148
156
 
149
- A matcher instance responds to `match` method and returns either a `Match` instance or nil if there's no match. See documentation for {Matcher#match} for more details.
157
+ A matcher instance responds to `match` method and returns either a `Match` instance or nil if there's no match. See documentation for {Kawaii::Matcher#match} for more details.
150
158
 
151
159
  ### Request object
152
160
 
153
161
  Handlers can access the `Rack::Request` instance corresponding to the current request:
154
162
 
155
- ```
163
+ ```ruby
156
164
  get '/' do
157
165
  request.host
158
166
  end
@@ -162,7 +170,7 @@ end
162
170
 
163
171
  View templates must currently be stored in `views/` directory of the project using Kawaii. They can be rendered using the `render` method:
164
172
 
165
- ```
173
+ ```ruby
166
174
  get '/' do
167
175
  render('index.html.erb')
168
176
  end
@@ -170,7 +178,7 @@ end
170
178
 
171
179
  You can set instance variables and use them in the templates.
172
180
 
173
- ```
181
+ ```ruby
174
182
  get '/' do
175
183
  @title = 'Hello, world'
176
184
  render('index.html.erb')
@@ -179,7 +187,7 @@ end
179
187
 
180
188
  Let's say `views/index.html.erb` looks like this:
181
189
 
182
- ```
190
+ ```html
183
191
  <h1><%= @title %></h1>
184
192
  ```
185
193
 
@@ -311,10 +319,40 @@ describe MyApp
311
319
  end
312
320
  ```
313
321
 
322
+ ## Custom 404 handler
323
+
324
+ By default Kawaii will respond with 404 'Not found' if no matching routes can be found.
325
+
326
+ You can define your own 404 handler:
327
+
328
+ ```ruby
329
+ not_found do
330
+ [404, {Rack::CONTENT_TYPE => 'text/plain'}, ['No matching routes found']]
331
+ end
332
+ ```
333
+
334
+ Notice that it needs to respond with a valid Rack response Array.
335
+
336
+ ## Custom exception handler
337
+
338
+ You can define your own 500 handler for unhandled exception:
339
+
340
+ ```ruby
341
+ get '/' do
342
+ fail 'Ooops!'
343
+ end
344
+ on_error do |e|
345
+ [500, {Rack::CONTENT_TYPE => 'text/plain'}, [e.to_s]]
346
+ end
347
+ ```
348
+
349
+ Just like with `not_found`, you need to return a well-formed Rack response Array.
350
+
314
351
  ## Resources
315
352
 
316
- See `/examples`
317
- ### Reference
353
+ 1. [API reference](http://bilus.github.io/kawaii/Kawaii.html).
354
+ 2. See [examples](https://github.com/bilus/kawaii/tree/master/examples) of basic usage of Kawaii.
355
+ 3. Small [example project](https://github.com/bilus/kawaii-sample) using the gem.
318
356
 
319
357
  ## Contributing
320
358
 
@@ -328,43 +366,74 @@ The gem is available as open source under the terms of the [MIT License](http://
328
366
 
329
367
  ## TODO
330
368
 
331
- + Hello world app.
332
- + Specs for the app.
333
- + GET routes inside a class deriving from Base.
334
- + Support for running apps without config.ru (ruby -I ./lib examples/hello_world.rb
335
- + Top-level routes.
336
- + Example for top-level routes.
337
- + Nested routes.
338
- + Modular apps (multiple modules via config.ru).
339
- + Matchers.
340
- + Wildcard regex routes, e.g. '/foo/bar/?'.
341
- + Parameter-based routes. Unsupported in 'context'.
342
- + Request object.
343
- + Merge Rack Request params.
344
- + String responses.
345
- + Other HTTP verbs.
346
- + Refactor & create individual files.
347
- + Views (via `render` method in handlers) using Tilt.
348
- + Rack route test helpers work.
349
- + API reference.
350
- + Check: References to methods defined in contexts and at class scope.
351
- + Controllers - 'hello_world#index'
352
- + 'route' to controllers (via class name or symbol references).
353
- + Controllers - render.
354
-
355
- - Readme - description and tutorial.
356
- - Rubocop-compliant.
357
- - Push gem.
358
-
359
- - Example project using the gem and controllers (with views).
360
-
361
- - Rack/custom global middleware.
362
- - Route-specific middleware.
363
- - Custom error handling (intercept exceptions, 404 what else?).
364
- - Code review
369
+ X Hello world app.
370
+
371
+ X Specs for the app.
372
+
373
+ X GET routes inside a class deriving from Base.
374
+
375
+ X Support for running apps without config.ru (ruby -I ./lib examples/hello_world.rb
376
+
377
+ X Top-level routes.
378
+
379
+ X Example for top-level routes.
380
+
381
+ X Nested routes.
382
+
383
+ X Modular apps (multiple modules via config.ru).
384
+
385
+ X Matchers.
386
+
387
+ X Wildcard regex routes, e.g. '/foo/bar/?'.
388
+
389
+ X Parameter-based routes. Unsupported in 'context'.
390
+
391
+ X Request object.
392
+
393
+ X Merge Rack Request params.
394
+
395
+ X String responses.
396
+
397
+ X Other HTTP verbs.
398
+
399
+ X Refactor & create individual files.
400
+
401
+ X Views (via `render` method in handlers) using Tilt.
402
+
403
+ X Rack route test helpers work.
404
+
405
+ X API reference.
406
+
407
+ X Check: References to methods defined in contexts and at class scope.
408
+
409
+ X Controllers - 'hello_world#index'
410
+
411
+ X 'route' to controllers (via class name or symbol references).
412
+
413
+ X Controllers - render.
414
+
415
+ X Push gem.
416
+
417
+ X Readme - description and tutorial.
418
+
419
+ X Example project using the gem and controllers (with views).
420
+
421
+ X Custom error handling (intercept exceptions, 404 what else?).
422
+
423
+ X Rubocop-compliant.
424
+
425
+ O Update and push.
426
+
427
+ O Code review
428
+
429
+ O Rack/custom global middleware.
430
+
431
+ O Route-specific middleware.
365
432
 
366
433
  ## Known issues
367
434
 
435
+ **There are many missing features and glitches are inevitable. The library hasn't been used in production yet. Please report them to `gyamtso at gmail dot com`.**
436
+
368
437
  ### Rubocop
369
438
 
370
439
  `lib/kawaii/routing_methods.rb:46:1: C: Extra blank line detected.`
@@ -9,10 +9,10 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ['Marcin Bilski']
10
10
  spec.email = ['gyamtso@gmail.com']
11
11
 
12
- spec.summary = 'Kawaii is a simple web framework based on Rack'
13
- spec.description = 'Kawaii is a basic but extensible web framework based on Rack'
14
-
15
- spec.homepage = "https://github.com/bilus/kawaii"
12
+ spec.summary = 'A simple web framework based on Rack'
13
+ spec.description = 'Kawaii is a simple web framework based on Rack'
14
+
15
+ spec.homepage = 'https://github.com/bilus/kawaii'
16
16
  spec.license = 'MIT'
17
17
 
18
18
  spec.files = `git ls-files -z`.split("\x0").reject { |f|
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency 'guard-rspec', '~>4.6'
32
32
  spec.add_development_dependency 'rack-test', '~>0.6'
33
33
  spec.add_development_dependency 'yard', '~> 0.8'
34
+ spec.add_development_dependency 'rubocop', '~> 0.35'
34
35
  end
@@ -20,29 +20,53 @@ module Kawaii
20
20
 
21
21
  # Instances of classes derived from [Kawaii::Base] are Rack applications.
22
22
  def call(env)
23
- matching = self.class.match(env) || not_found
23
+ matching = self.class.match(env)
24
24
  matching.call(env)
25
+ rescue => e
26
+ self.class.handle_error(e)
25
27
  end
26
28
 
27
29
  class << self
28
30
  include ServerMethods
29
31
  include RoutingMethods
30
32
 
33
+ # Define 404 handler. Has to return a valid Rack response.
34
+ def not_found(&block)
35
+ @not_found_handler = block
36
+ end
37
+
38
+ # Define an unhandled exception handler. Has to return a valid Rack
39
+ # response.
40
+ def on_error(&block)
41
+ @error_handler = block
42
+ end
43
+
44
+ def match(env)
45
+ super(env) || not_found_handler
46
+ end
47
+
31
48
  # Make it runnable via `run MyApp`.
32
49
  def call(env)
33
50
  @app ||= new
34
51
  @app.call(env)
35
52
  end
36
- end
37
53
 
38
- protected
54
+ def handle_error(e)
55
+ handler = @error_handler || ->(ex) { fail ex }
56
+ handler.call(e)
57
+ end
39
58
 
40
- def not_found
41
- @downstream_app || ->(_env) { text(404, 'Not found') }
42
- end
59
+ protected
43
60
 
44
- def text(status, s)
45
- [status, { Rack::CONTENT_TYPE => 'text/plain' }, [s]]
61
+ def not_found_handler
62
+ @downstream_app ||
63
+ @not_found_handler ||
64
+ ->(_env) { text(404, 'Not found') }
65
+ end
66
+
67
+ def text(status, s)
68
+ [status, { Rack::CONTENT_TYPE => 'text/plain' }, [s]]
69
+ end
46
70
  end
47
71
  end
48
72
  end
@@ -17,7 +17,7 @@ module Kawaii
17
17
  # get '/', 'hello_world#index'
18
18
  class Controller
19
19
  include RenderMethods
20
-
20
+
21
21
  # Parameter [Hash] accessible in actions
22
22
  attr_reader :params
23
23
  # Rack::Request accessible in actions
@@ -43,7 +43,7 @@ module Kawaii
43
43
  end
44
44
 
45
45
  # Tries to match the actual path.
46
- # @param path [String] the actual path from Rack env
46
+ # @param _path [String] the actual path from Rack env
47
47
  # @return {Match} if the beginning of path does match or nil if there is
48
48
  # no match.
49
49
  def match(_path)
@@ -100,7 +100,7 @@ module Kawaii
100
100
  # get /\/users.*/ do ... end
101
101
  class RegexpMatcher
102
102
  # Creates a {RegexpMatcher}
103
- # @param path [Regexp] path specification regex
103
+ # @param rx [Regexp] path specification regex
104
104
  # @todo Support parameters based on named capture groups.
105
105
  def initialize(rx)
106
106
  @rx = rx
@@ -1,6 +1,6 @@
1
1
  module Kawaii
2
2
  # Allows handlers to use methods defined in outer contexts or at class scope.
3
- # Set {#parent_scope} in constructor.
3
+ # Set MethodChain.parent_scope in constructor.
4
4
  module MethodChain
5
5
  attr_writer :parent_scope
6
6
 
@@ -2,7 +2,8 @@ module Kawaii
2
2
  # Template rendering based on Tilt.
3
3
  module RenderMethods
4
4
  # Renders a template.
5
- # @param tmpl [String] file name or path to template, relative to /views in project dir
5
+ # @param tmpl [String] file name or path to template, relative to /views in
6
+ # project dir
6
7
  # @example Rendering html erb file
7
8
  # render('index.html.erb')
8
9
  # @todo Layouts.
@@ -50,7 +50,7 @@ module Kawaii
50
50
  # corresponds to HelloWorld).
51
51
  # @example REST resource routes
52
52
  # route '/users/', 'hello_world'
53
- #
53
+ #
54
54
  # # Will insert routes corresponding to:
55
55
  # # GET /users/? -> Controller#index
56
56
  # # GET /users/:id/? -> Controller#show
@@ -1,4 +1,4 @@
1
1
  # Gem version.
2
2
  module Kawaii
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kawaii-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Bilski
@@ -122,7 +122,21 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0.8'
125
- description: Kawaii is a basic but extensible web framework based on Rack
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.35'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.35'
139
+ description: Kawaii is a simple web framework based on Rack
126
140
  email:
127
141
  - gyamtso@gmail.com
128
142
  executables: []
@@ -185,9 +199,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
199
  version: '0'
186
200
  requirements: []
187
201
  rubyforge_project:
188
- rubygems_version: 2.2.2
202
+ rubygems_version: 2.4.8
189
203
  signing_key:
190
204
  specification_version: 4
191
- summary: Kawaii is a simple web framework based on Rack
205
+ summary: A simple web framework based on Rack
192
206
  test_files: []
193
207
  has_rdoc: