mojito 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mojito.rb +13 -1
- data/lib/mojito/base.rb +15 -7
- data/lib/mojito/helpers/exception_handling.rb +2 -0
- data/lib/mojito/helpers/shortcuts.rb +27 -21
- data/lib/mojito/matchers/environment.rb +2 -0
- data/lib/mojito/matchers/methods.rb +2 -0
- data/lib/mojito/matchers/path.rb +5 -3
- data/lib/mojito/matchers/session.rb +2 -0
- data/lib/mojito/matchers/url_scheme.rb +3 -1
- data/lib/mojito/matchers/virtual_host.rb +2 -0
- data/lib/mojito/rendering/content.rb +11 -0
- data/lib/mojito/rendering/content_types.rb +2 -0
- data/lib/mojito/rendering/delegation.rb +2 -0
- data/lib/mojito/rendering/file.rb +4 -1
- data/lib/mojito/rendering/status_codes.rb +2 -0
- data/lib/mojito/rendering/templates.rb +3 -0
- data/lib/mojito/request_extensions.rb +4 -0
- data/spec/mojito/base_spec.rb +22 -0
- data/spec/mojito/rendering/delegation_spec.rb +7 -5
- metadata +23 -22
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/mojito.rb
CHANGED
@@ -3,6 +3,9 @@ require 'rack'
|
|
3
3
|
require 'extlib'
|
4
4
|
|
5
5
|
module Mojito
|
6
|
+
|
7
|
+
PLUGINS = {}
|
8
|
+
|
6
9
|
require 'mojito/request_extensions'
|
7
10
|
require 'mojito/helpers'
|
8
11
|
require 'mojito/base'
|
@@ -14,6 +17,7 @@ module Mojito
|
|
14
17
|
M = Matchers
|
15
18
|
H = Helpers
|
16
19
|
|
20
|
+
|
17
21
|
def self.included(type)
|
18
22
|
type.instance_exec do
|
19
23
|
ALL_HELPERS.reverse.each do |mod|
|
@@ -27,7 +31,12 @@ module Mojito
|
|
27
31
|
cl.instance_exec do
|
28
32
|
include Mojito::Base
|
29
33
|
helpers.reverse.each do |helper|
|
30
|
-
|
34
|
+
case helper
|
35
|
+
when Symbol
|
36
|
+
include PLUGINS[helper]
|
37
|
+
when Module
|
38
|
+
include helper
|
39
|
+
end
|
31
40
|
end
|
32
41
|
end
|
33
42
|
cl.routes &block if block
|
@@ -50,6 +59,9 @@ module Mojito
|
|
50
59
|
(ENV['RACK_ENV'] || :development).to_sym
|
51
60
|
end
|
52
61
|
|
62
|
+
def self.development?
|
63
|
+
mode == :development
|
64
|
+
end
|
53
65
|
|
54
66
|
ALL_HELPERS = [Mojito::Matchers, Mojito::Rendering, Mojito::Helpers::ExceptionHandling, Mojito::Helpers::Shortcuts, Mojito::Base]
|
55
67
|
|
data/lib/mojito/base.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
+
require 'mime/types'
|
4
|
+
|
3
5
|
module Mojito
|
4
6
|
|
5
7
|
module Base
|
@@ -9,19 +11,25 @@ module Mojito
|
|
9
11
|
end
|
10
12
|
|
11
13
|
def initialize(env)
|
12
|
-
@
|
14
|
+
@__request = Rack::Request.new env.dup
|
15
|
+
self.env['MOJITO/CONTEXT_PATH'] = self.env['SCRIPT_NAME']
|
13
16
|
end
|
14
17
|
|
15
18
|
def env
|
16
|
-
|
19
|
+
request.env
|
17
20
|
end
|
18
21
|
|
19
22
|
def request
|
20
|
-
@__request
|
23
|
+
@__request
|
21
24
|
end
|
22
25
|
|
23
26
|
def response
|
24
|
-
@__response ||= Rack::Response.new
|
27
|
+
@__response ||= Rack::Response.new.tap do |res|
|
28
|
+
if extension = request.path[/(?<=\.)\w+$/]
|
29
|
+
type = MIME::Types.type_for(extension).first
|
30
|
+
res.headers['Content-Type'] = type.to_s if type
|
31
|
+
end
|
32
|
+
end
|
25
33
|
end
|
26
34
|
|
27
35
|
def halt!(resp = response)
|
@@ -42,7 +50,7 @@ module Mojito
|
|
42
50
|
# Dispatches the current request to the matching routes.
|
43
51
|
def dispatch
|
44
52
|
instance_exec &self.class.routes if self.class.routes
|
45
|
-
|
53
|
+
[404, { 'Content-Type' => 'application/octet-stream' }, []]
|
46
54
|
end
|
47
55
|
|
48
56
|
##
|
@@ -64,7 +72,7 @@ module Mojito
|
|
64
72
|
def __match?(matcher)
|
65
73
|
case matcher
|
66
74
|
when String, Regexp
|
67
|
-
instance_exec &Matchers::PATH(matcher)
|
75
|
+
instance_exec &Mojito::Matchers::PATH(matcher)
|
68
76
|
when Proc
|
69
77
|
instance_exec &matcher
|
70
78
|
else
|
@@ -77,7 +85,7 @@ module Mojito
|
|
77
85
|
|
78
86
|
def call(env)
|
79
87
|
catch :halt do
|
80
|
-
new(env).dispatch
|
88
|
+
self.new(env).dispatch
|
81
89
|
end
|
82
90
|
end
|
83
91
|
|
@@ -1,31 +1,37 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
module Mojito::Helpers
|
3
|
+
module Mojito::Helpers
|
4
4
|
|
5
|
-
|
6
|
-
request.captures
|
7
|
-
end
|
8
|
-
|
9
|
-
def locals
|
10
|
-
request.locals
|
11
|
-
end
|
12
|
-
|
13
|
-
def script_name
|
14
|
-
request.script_name
|
15
|
-
end
|
16
|
-
|
17
|
-
def path_info
|
18
|
-
request.path_info
|
19
|
-
end
|
5
|
+
module Shortcuts
|
20
6
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
7
|
+
def captures
|
8
|
+
request.captures
|
9
|
+
end
|
10
|
+
|
11
|
+
def locals
|
12
|
+
request.locals
|
13
|
+
end
|
26
14
|
|
15
|
+
def script_name
|
16
|
+
request.script_name
|
17
|
+
end
|
27
18
|
|
19
|
+
def path_info
|
20
|
+
request.path_info
|
21
|
+
end
|
22
|
+
|
23
|
+
def session
|
24
|
+
request.session
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
end
|
28
32
|
|
29
33
|
end
|
30
34
|
|
35
|
+
Mojito::PLUGINS[:shortcuts] = Shortcuts
|
36
|
+
|
31
37
|
end
|
data/lib/mojito/matchers/path.rb
CHANGED
@@ -4,9 +4,9 @@ module Mojito::Matchers
|
|
4
4
|
|
5
5
|
module Path
|
6
6
|
|
7
|
-
def PATH(pattern)
|
7
|
+
def PATH(pattern, delimiter = %r{/|\z})
|
8
8
|
consume_path = proc do |pattern|
|
9
|
-
if match = env['PATH_INFO'].match(%r<\A/*#{pattern}(
|
9
|
+
if match = env['PATH_INFO'].match(%r<\A/*#{pattern}(?=#{delimiter})>)
|
10
10
|
env['SCRIPT_NAME'] = match.to_s
|
11
11
|
env['PATH_INFO'] = match.post_match
|
12
12
|
request.locals.update match.names.inject({}) {|hash, name| hash[name.to_sym] = match[name] ; hash }
|
@@ -19,7 +19,7 @@ module Mojito::Matchers
|
|
19
19
|
proc do
|
20
20
|
if p = case pattern
|
21
21
|
when String
|
22
|
-
pattern.gsub(%r{/?:\?\w+}) {|name| "(?:/(?<#{name[2..-1]}>[^/]
|
22
|
+
pattern.gsub(%r{/?:\?\w+}) {|name| "(?:/(?<#{name[2..-1]}>[^/]+?))?" }.gsub(%r{:\w+}) {|name| "(?<#{name[1..-1]}>[^/]+?)" }
|
23
23
|
when Regexp
|
24
24
|
pattern
|
25
25
|
end
|
@@ -32,4 +32,6 @@ module Mojito::Matchers
|
|
32
32
|
|
33
33
|
end
|
34
34
|
|
35
|
+
Mojito::PLUGINS[:path] = Path
|
36
|
+
|
35
37
|
end
|
@@ -8,6 +8,17 @@ module Mojito::Rendering
|
|
8
8
|
response.write content
|
9
9
|
end
|
10
10
|
|
11
|
+
def render_as(type, content, *args)
|
12
|
+
renderer = "to_#{type}".to_sym
|
13
|
+
if content.respond_to? renderer
|
14
|
+
response.write(content.send(renderer, *args[0...content.method(renderer).arity]))
|
15
|
+
else
|
16
|
+
Mojito::Rendering::StatusCodes.instance_method(:not_found!).bind(self).call
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
11
20
|
end
|
12
21
|
|
22
|
+
Mojito::PLUGINS[:content] = Content
|
23
|
+
|
13
24
|
end
|
@@ -6,9 +6,10 @@ module Mojito::Rendering
|
|
6
6
|
require 'pathname'
|
7
7
|
require 'mime/types'
|
8
8
|
require 'time'
|
9
|
+
require 'where'
|
9
10
|
|
10
11
|
def file!(filename)
|
11
|
-
path =
|
12
|
+
path = Where.cdir(1) + filename
|
12
13
|
restrict_path! path if respond_to? :restrict_path!
|
13
14
|
if path.readable? and path.file?
|
14
15
|
body = FileResponse.new path
|
@@ -50,4 +51,6 @@ module Mojito::Rendering
|
|
50
51
|
|
51
52
|
end
|
52
53
|
|
54
|
+
Mojito::PLUGINS[:file] = File
|
55
|
+
|
53
56
|
end
|
@@ -13,6 +13,7 @@ module Mojito::Rendering
|
|
13
13
|
Tilt[args.first].new { args.last }
|
14
14
|
elsif args.size == 1
|
15
15
|
file = Where.cdir(1) + args.first
|
16
|
+
Mojito::R::StatusCodes.instance_method(:not_found!).bind(self).call unless file.exist?
|
16
17
|
if %r{\.(?<extension>\w+)\.\w+$} =~ file.to_s
|
17
18
|
response['Content-Type'] = MIME::Types.type_for(extension)
|
18
19
|
end
|
@@ -23,4 +24,6 @@ module Mojito::Rendering
|
|
23
24
|
|
24
25
|
end
|
25
26
|
|
27
|
+
Mojito::PLUGINS[:templates] = Templates
|
28
|
+
|
26
29
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'simplecov' and SimpleCov.start do
|
3
|
+
add_filter "spec/"
|
4
|
+
end
|
5
|
+
require 'mojito'
|
6
|
+
|
7
|
+
describe Mojito::Base do
|
8
|
+
|
9
|
+
subject do
|
10
|
+
Mojito.base_application :status_codes do
|
11
|
+
on Mojito::Matchers::PATH('test.:extension') do ok! end
|
12
|
+
end.mock_request
|
13
|
+
end
|
14
|
+
|
15
|
+
it { subject.get('/test.html').headers.should include('Content-Type') }
|
16
|
+
it { subject.get('/test.html').headers['Content-Type'].should == 'text/html' }
|
17
|
+
it { subject.get('/test.txt').headers.should include('Content-Type') }
|
18
|
+
it { subject.get('/test.txt').headers['Content-Type'].should == 'text/plain' }
|
19
|
+
it { subject.get('/test.rb').headers.should include('Content-Type') }
|
20
|
+
it { subject.get('/test.rb').headers['Content-Type'].should == 'application/x-ruby' }
|
21
|
+
|
22
|
+
end
|
@@ -7,21 +7,23 @@ require 'mojito'
|
|
7
7
|
describe Mojito::Rendering::Delegation do
|
8
8
|
|
9
9
|
subject do
|
10
|
-
sub_app = Mojito.base_application Mojito::R::Content, Mojito::
|
10
|
+
sub_app = Mojito.base_application Mojito::R::Content, Mojito::M::Path do
|
11
|
+
on PATH('this/is/the/sub-application') do write request.context_path ; halt! end
|
11
12
|
on true do write 'sub-application' ; halt! end
|
12
13
|
end
|
13
14
|
Mojito.base_application Mojito::Rendering::Content, Mojito::Rendering::Delegation do
|
14
|
-
on
|
15
|
+
on 'to/sub/app' do run! sub_app end
|
15
16
|
end.mock_request
|
16
17
|
end
|
17
18
|
|
18
|
-
it { subject.get('/').status.should == 200 }
|
19
|
-
it { subject.get('/').body.should == 'sub-application' }
|
19
|
+
it { subject.get('/to/sub/app').status.should == 200 }
|
20
|
+
it { subject.get('/to/sub/app').body.should == 'sub-application' }
|
21
|
+
it { subject.get('/to/sub/app/this/is/the/sub-application').body.should == '/to/sub/app' }
|
20
22
|
|
21
23
|
context do
|
22
24
|
|
23
25
|
subject do
|
24
|
-
sub_app = Mojito.base_application Mojito::R::Content, Mojito::
|
26
|
+
sub_app = Mojito.base_application Mojito::R::Content, Mojito::H::Shortcuts do
|
25
27
|
on true do write("#{path_info} #{captures.first}") ; halt! end
|
26
28
|
end
|
27
29
|
Mojito.base_application Mojito::M::Path, Mojito::R::Content, Mojito::R::Delegation do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mojito
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement: &
|
16
|
+
requirement: &15854720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.4.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *15854720
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mime-types
|
27
|
-
requirement: &
|
27
|
+
requirement: &15854100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '1.18'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *15854100
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: tilt
|
38
|
-
requirement: &
|
38
|
+
requirement: &15853440 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.3.3
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *15853440
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: extlib
|
49
|
-
requirement: &
|
49
|
+
requirement: &15852680 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 0.9.15
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *15852680
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: where-am-i
|
60
|
-
requirement: &
|
60
|
+
requirement: &15851760 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.0.0
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *15851760
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
|
-
requirement: &
|
71
|
+
requirement: &15850760 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 2.8.0
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *15850760
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rdoc
|
82
|
-
requirement: &
|
82
|
+
requirement: &15850180 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '3.12'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *15850180
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: bundler
|
93
|
-
requirement: &
|
93
|
+
requirement: &15849540 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ~>
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: 1.0.0
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *15849540
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: jeweler
|
104
|
-
requirement: &
|
104
|
+
requirement: &15848960 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ~>
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: 1.8.3
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *15848960
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: rcov
|
115
|
-
requirement: &
|
115
|
+
requirement: &15848300 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,7 +120,7 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *15848300
|
124
124
|
description: A simple yet powerful webframework largely inspired by Rum and Cuba
|
125
125
|
email: dev@trense.info
|
126
126
|
executables: []
|
@@ -152,6 +152,7 @@ files:
|
|
152
152
|
- lib/mojito/rendering/templates.rb
|
153
153
|
- lib/mojito/request_extensions.rb
|
154
154
|
- lib/mojito/utils/status_codes.rb
|
155
|
+
- spec/mojito/base_spec.rb
|
155
156
|
- spec/mojito/helpers_spec.rb
|
156
157
|
- spec/mojito/matchers/methods_spec.rb
|
157
158
|
- spec/mojito/matchers/path_spec.rb
|