tansu 0.1.0 → 0.2.0

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: e6a494e03db5288f809efafa34da0b9e6ce81bad
4
- data.tar.gz: be13adff36064062a618995ad8ccc19d020f4aee
3
+ metadata.gz: fd67f1e2c6100222b3b22315e166451be9ae789e
4
+ data.tar.gz: 5805a8800e971efc0b277e71e15d10bd766fc1bf
5
5
  SHA512:
6
- metadata.gz: 84c2f35cca74ccdcb48cba1c47ed2648cf1c77945b75219e291dd481bbe58560f7a11d086cfc2afecf733226fe8b23cc1d5b153711e282fbf7ab3ac46bf60915
7
- data.tar.gz: fec37752fc4a277e8a106ed6cf5289a17be7639f1006b636101c494e8fea23e65400a462625b7cd22686474db9fa147d1af3e78f646da9acb252911f0306413d
6
+ metadata.gz: a0f706c3bfabf11d502018c54e7ff795aa527b2239d825aa4b485ea2d6907cf9f8013bf05728b7f7c36cda099aa15b586b77f414a5065eb2e5bd8cb7e55711bc
7
+ data.tar.gz: 5db1cdb46be41625f410c1ad851e046fe2ef915c75cbd8f9bedc05afe55edd4d54d25ef3b16aa42bdac516ddb901131e0f689271c4315dac8a053f94c5018cce
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Tansu
2
2
 
3
+ [![Build Status](https://travis-ci.org/daisuko/tansu.svg?branch=master)](https://travis-ci.org/daisuko/tansu)
4
+
3
5
  Tansu is a web-application framework.
4
6
 
5
7
  ## Installation
@@ -1,15 +1,13 @@
1
1
  module Tansu
2
2
  class Application
3
- class << self
4
- attr_accessor :before, :after
5
- end
3
+ extend Tansu::Configuration
6
4
 
7
5
  def call(env)
8
- Application.before.() if Application.before
6
+ Application[:before].() if Application[:before]
9
7
 
10
8
  response = action(env)
11
9
 
12
- Application.after.() if Application.after
10
+ Application[:after].() if Application[:after]
13
11
 
14
12
  response
15
13
  end
@@ -18,26 +16,20 @@ module Tansu
18
16
 
19
17
  def action(env)
20
18
  request = Rack::Request.new(env)
21
- environment = Environment.new(request, Rack::Response.new)
19
+ environment = Environment.new(request, Response.new)
22
20
 
23
- path = parse(request.path_info)
21
+ path = environment.path.empty? ? [:*] : environment.path
24
22
  router = path.inject(Tansu.root) { |a, e| a[e] }[request.request_method]
25
23
 
26
24
  if Minicontext::Router::Just === router
27
25
  response = router.value.(environment).response
28
26
 
29
- Rack::Response === response ? response : internal_server_error
27
+ Response === response ? response.finish : internal_server_error
30
28
  else
31
29
  not_found
32
30
  end
33
31
  end
34
32
 
35
- def parse(path)
36
- ret = (path[1..-1] || '').split('/').map(&:to_sym)
37
-
38
- ret.empty? ? [:*] : ret
39
- end
40
-
41
33
  def internal_server_error
42
34
  ['500', { 'Context-Type' => 'text/html' }, ['internal server error.']]
43
35
  end
@@ -0,0 +1,27 @@
1
+ module Tansu
2
+ module Configuration
3
+ def self.extended(klass)
4
+ klass.instance_variable_set(:"@vars", {})
5
+ end
6
+
7
+ def set(name, val)
8
+ self[name]= val
9
+ end
10
+
11
+ def get(name)
12
+ self[name]
13
+ end
14
+
15
+ def []=(name, val)
16
+ @vars[name] = val
17
+ end
18
+
19
+ def [](name)
20
+ @vars[name]
21
+ end
22
+
23
+ def configure(&block)
24
+ instance_eval &block
25
+ end
26
+ end
27
+ end
data/lib/tansu/context.rb CHANGED
@@ -1,9 +1,14 @@
1
1
  module Tansu
2
2
  class Context < Minicontext::Context
3
- METHODS = %w(GET POST PATCH PUT DELETE)
3
+ METHODS = %w(GET HEAD POST PATCH PUT DELETE)
4
4
  KEYWORD = Minicontext::Context::KEYWORD + METHODS
5
5
 
6
- METHODS.each do |method|
6
+ def get(name, &block)
7
+ context(name) { task 'GET', &block }
8
+ head(name, &block)
9
+ end
10
+
11
+ (METHODS - ['GET']).each do |method|
7
12
  define_method(method.downcase.to_sym) do |name, &block|
8
13
  context(name) { task method, &block }
9
14
  end
@@ -1,17 +1,30 @@
1
1
  module Tansu
2
2
  class Environment < Minicontext::Environment
3
+ include Tansu::Render
4
+
5
+ attr_reader :path
6
+
7
+ IS_RESPOND_TO_EACH = -> (v) { v.respond_to?(:each) }
8
+
9
+ def initialize(request, response = nil)
10
+ @path = parse(request.path_info)
11
+
12
+ super request, response
13
+ end
14
+
3
15
  def bind(val)
4
- res = if Minicontext::Halt === val || Rack::Response === val
16
+ res = case val
17
+ when Response, Minicontext::Halt
5
18
  val
6
- elsif val.respond_to?(:each)
19
+ when IS_RESPOND_TO_EACH
7
20
  response.body = val
8
21
 
9
22
  response
10
- elsif Fixnum === val
23
+ when Fixnum
11
24
  response.status = val
12
25
 
13
26
  response
14
- elsif String === val
27
+ when String
15
28
  response.body = [val]
16
29
 
17
30
  response
@@ -22,8 +35,24 @@ module Tansu
22
35
  super res
23
36
  end
24
37
 
38
+ def headers(hash = nil)
39
+ response.headers.merge!(hash) if hash
40
+
41
+ response.headers
42
+ end
43
+
44
+ def session
45
+ request.session
46
+ end
47
+
25
48
  def redirect(uri, status = 302)
26
49
  response.redirect(uri, status)
27
50
  end
51
+
52
+ private
53
+
54
+ def parse(path)
55
+ (path[1..-1] || '').split('/').map(&:to_sym)
56
+ end
28
57
  end
29
58
  end
@@ -0,0 +1,52 @@
1
+ module Tansu
2
+ module Render
3
+ class NoRendererError < StandardError; end
4
+ class LayoutMissingError < StandardError; end
5
+
6
+ %i(erb haml).each do |ext|
7
+ define_method(ext) do |name, locals = {}, options = {}, &block|
8
+ render(ext, name, locals, options, &block)
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def render(ext, name, locals = {}, options = {}, &block)
15
+ options = options.merge(renderer_options(ext)) { |k, a, b| a }
16
+ options = options.merge(application_options) { |k, a, b| a }
17
+ template = renderer(ext, name, options)
18
+ ret = template.render(self, locals, &block)
19
+
20
+ if options[:layout]
21
+ layout = options.delete(:layout)
22
+ layout = :layout if layout == true
23
+
24
+ render(ext, layout, locals, options) { ret }
25
+ else
26
+ ret
27
+ end
28
+ end
29
+
30
+ def renderer(ext, name, options)
31
+ basename = "#{name}.#{ext}"
32
+ filename = File.join options[:views], basename
33
+
34
+ raise NoRendererError, ext unless template = Tilt[ext]
35
+ raise LayoutMissingError, filename unless File.exist?(filename)
36
+
37
+ templates_cache.fetch(filename) { template.new(filename, options) }
38
+ end
39
+
40
+ def renderer_options(ext)
41
+ Application[ext] || {}
42
+ end
43
+
44
+ def application_options
45
+ { views: Application[:templates] }
46
+ end
47
+
48
+ def templates_cache
49
+ Application[:templates_cache] ||= Tilt::Cache.new
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,47 @@
1
+ module Tansu
2
+ class Response < Rack::Response
3
+ def initialize(body = [], status = 200, header = {})
4
+ super body, status, header
5
+
6
+ headers['Content-Type'] ||= 'text/html'
7
+ end
8
+
9
+ def finish
10
+ drop_content_info if drop_content_info?
11
+
12
+ ret = drop_body? ? drop_body : body
13
+
14
+ headers['Content-Length'] = content_length.to_s if need_content_length?
15
+
16
+ [status.to_i, headers, ret]
17
+ end
18
+
19
+ private
20
+
21
+ def drop_content_info
22
+ headers.delete 'Content-Type'
23
+ headers.delete 'Content-Length'
24
+ end
25
+
26
+ def drop_body
27
+ close
28
+ []
29
+ end
30
+
31
+ def content_length
32
+ body.inject(0) { |a, e| a + Rack::Utils.bytesize(e) }
33
+ end
34
+
35
+ def need_content_length?
36
+ headers['Content-Type'] && !headers['Content-Length'] && Array === body
37
+ end
38
+
39
+ def drop_content_info?
40
+ status.to_i / 100 == 1 or drop_body?
41
+ end
42
+
43
+ def drop_body?
44
+ [204, 205, 304].include?(status.to_i)
45
+ end
46
+ end
47
+ end
data/lib/tansu/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tansu
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/tansu.rb CHANGED
@@ -1,6 +1,14 @@
1
+ require "rack"
2
+ require "minicontext"
3
+ require "tilt"
4
+ require "rack/protection"
5
+
1
6
  require "tansu/version"
2
7
  require "tansu/context"
8
+ require "tansu/response"
9
+ require "tansu/render"
3
10
  require "tansu/environment"
11
+ require "tansu/configuration"
4
12
  require "tansu/application"
5
13
 
6
14
  module Tansu
data/tansu.gemspec CHANGED
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency "rack"
23
23
  spec.add_dependency "minicontext"
24
+ spec.add_dependency "tilt"
25
+ spec.add_dependency "rack-protection"
24
26
 
25
27
  spec.add_development_dependency "bundler", "~> 1.10"
26
28
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tansu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - daisuko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-07 00:00:00.000000000 Z
11
+ date: 2015-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tilt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-protection
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: bundler
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -114,8 +142,11 @@ files:
114
142
  - bin/setup
115
143
  - lib/tansu.rb
116
144
  - lib/tansu/application.rb
145
+ - lib/tansu/configuration.rb
117
146
  - lib/tansu/context.rb
118
147
  - lib/tansu/environment.rb
148
+ - lib/tansu/render.rb
149
+ - lib/tansu/response.rb
119
150
  - lib/tansu/version.rb
120
151
  - tansu.gemspec
121
152
  homepage: https://github.com/daisuko/tansu