tynn 1.0.0.rc1 → 1.0.0.rc2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4778b186382c25ab99b1687323ec3562c045a0ee
4
- data.tar.gz: 3b8c8d0c9a80cf5cce5cfb235e27be3b34093197
3
+ metadata.gz: 5991d5053c18550ff9e4640a0fb6ce8877e2e8d9
4
+ data.tar.gz: 546da7cc32f33b70f95abb79557fb05a1045bc01
5
5
  SHA512:
6
- metadata.gz: ef2895b73ebb6b9180e747a28499de003fb430a6e6eaee6027f7d7ae5c2569c9161347aed31aacb25552ce010dff48a01ebdbdf39af026f61702316c38ac3036
7
- data.tar.gz: 47e3ccf946f417ba6c444d4197d511eef8df1c987e83468214a5949d3ffa6c46a89e891da13d032e077bdd6b1781ec64313a60a3a399e5eb259ced0513fe6869
6
+ metadata.gz: 73682ff6496225b2fee11bdf76bf262ded91308c5db2b7c4bd4442a5b152247b5a8ea5a23d57b7f96acc96f67e180eca4165518a38f5bb7b2feb9f84481e0b1a
7
+ data.tar.gz: 0a4b872f14749623d88aaac9e6ade93a787a67ac1ee5eb93350028845ba5d55f217923dd8f5d62aed61b2bfd4b40c29d238763659dec36cb4778570a20495d3f
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  tynn [![Gem Version](https://badge.fury.io/rb/tynn.svg)](https://rubygems.org/gems/tynn)
2
2
  ====
3
3
 
4
- Simple library to create [Rack][rack] applications.
4
+ A thin library for web development.
5
5
 
6
6
  Description
7
7
  -----------
@@ -28,7 +28,7 @@ class Tynn
28
28
  # ```
29
29
  #
30
30
  def self.define(&block)
31
- @syro = Syro.new(self, &block)
31
+ build_app(Syro.new(self, &block))
32
32
  end
33
33
 
34
34
  # Adds given Rack `middleware` to the stack.
@@ -46,16 +46,14 @@ class Tynn
46
46
  end
47
47
 
48
48
  def self.call(env) # :nodoc:
49
- return to_app.call(env)
49
+ return @app.call(env)
50
50
  end
51
51
 
52
- def self.to_app # :nodoc:
53
- fail("Missing application handler. Try #{ self }.define") unless @syro
54
-
52
+ def self.build_app(syro) # :nodoc:
55
53
  if __middleware.empty?
56
- return @syro
54
+ @app = syro
57
55
  else
58
- return __middleware.reverse.inject(@syro) { |a, m| m.call(a) }
56
+ @app = __middleware.reverse.inject(syro) { |a, m| m.call(a) }
59
57
  end
60
58
  end
61
59
 
@@ -64,7 +62,7 @@ class Tynn
64
62
  end
65
63
 
66
64
  def self.reset! # :nodoc:
67
- @syro = nil
65
+ @app = nil
68
66
  @middleware = []
69
67
  end
70
68
 
@@ -73,7 +71,7 @@ class Tynn
73
71
  # ```
74
72
  # module AppName
75
73
  # def self.setup(app, name)
76
- # app.settings[:app_name] = name
74
+ # app.set(:app_name, name)
77
75
  # end
78
76
  #
79
77
  # def app_name
@@ -81,10 +79,6 @@ class Tynn
81
79
  # end
82
80
  #
83
81
  # module ClassMethods
84
- # def app_name=(new_name)
85
- # settings[:app_name] = new_name
86
- # end
87
- #
88
82
  # def app_name
89
83
  # return settings[:app_name]
90
84
  # end
@@ -95,7 +89,7 @@ class Tynn
95
89
  #
96
90
  # Tynn.app_name # => "MyApplication"
97
91
  #
98
- # Tynn.app_name = "MyGreatestApp"
92
+ # Tynn.set(:app_name, "MyGreatestApp")
99
93
  # Tynn.app_name # => "MyGreatestApp"
100
94
  #
101
95
  # Tynn.define do
@@ -107,7 +101,7 @@ class Tynn
107
101
  #
108
102
  # Check the [helpers][examples] that come with tynn for more examples.
109
103
  #
110
- # [examples]: https://github.com/harmoni/tynn/tree/master/lib/tynn
104
+ # [examples]: https://github.com/frodsan/tynn/tree/master/lib/tynn
111
105
  #
112
106
  def self.helpers(helper, *args, &block)
113
107
  self.include(helper)
@@ -121,6 +115,10 @@ class Tynn
121
115
  end
122
116
  end
123
117
 
118
+ def self.set(key, value)
119
+ settings[key] = value
120
+ end
121
+
124
122
  def request_class # :nodoc:
125
123
  return Tynn::Request
126
124
  end
@@ -128,4 +126,40 @@ class Tynn
128
126
  def response_class # :nodoc:
129
127
  return Tynn::Response
130
128
  end
129
+
130
+ ##
131
+ # :method: halt(response)
132
+ #
133
+ # Immediately stops the request and returns `response` as per
134
+ # Rack's specification.
135
+ #
136
+ # ```
137
+ # halt([200, { "Content-Type" => "text/html" }, ["hello"]])
138
+ # halt([res.status, res.headers, res.body])
139
+ # halt(res.finish)
140
+ # ```
141
+
142
+ ##
143
+ # :method: req
144
+ #
145
+ # Returns the incoming request object. This object is an instance
146
+ # of Tynn::Request.
147
+ #
148
+ # ```
149
+ # req.post? # => true
150
+ # req.params # => { "username" => "bob", "password" => "secret" }
151
+ # req[:username] # => "bob"
152
+ # ```
153
+
154
+ ##
155
+ # :method: res
156
+ #
157
+ # Returns the current response object. This object is an instance
158
+ # of Tynn::Response.
159
+ #
160
+ # ```
161
+ # res.status = 200
162
+ # res["Content-Type"] = "text/html"
163
+ # res.write("<h1>Welcome back!</h1>")
164
+ # ```
131
165
  end
@@ -1,17 +1,19 @@
1
- module Tynn::AllMethods
2
- def head
3
- if root? && req.head?
4
- yield
1
+ class Tynn
2
+ module AllMethods
3
+ def head
4
+ if root? && req.head?
5
+ yield
5
6
 
6
- halt(res.finish)
7
+ halt(res.finish)
8
+ end
7
9
  end
8
- end
9
10
 
10
- def options
11
- if root? && req.options?
12
- yield
11
+ def options
12
+ if root? && req.options?
13
+ yield
13
14
 
14
- halt(res.finish)
15
+ halt(res.finish)
16
+ end
15
17
  end
16
18
  end
17
19
  end
@@ -1,44 +1,46 @@
1
- # Adds helper methods to get and check the current environment.
2
- #
3
- # ```
4
- # require "tynn"
5
- # require "tynn/environment"
6
- #
7
- # Tynn.helpers(Tynn::Environment)
8
- #
9
- # Tynn.environment # => :development
10
- #
11
- # Tynn.development? # => true
12
- # Tynn.production? # => false
13
- # Tynn.test? # => false
14
- # ```
15
- #
16
- # By default, the environment is based on `ENV["RACK_ENV"]`.
17
- #
18
- # ```
19
- # Tynn.helpers(Tynn::Environment, env: ENV["RACK_ENV"])
20
- # ```
21
- #
22
- module Tynn::Environment
23
- def self.setup(app, env: ENV["RACK_ENV"]) # :nodoc:
24
- app.settings[:environment] = (env || :development).to_sym
25
- end
26
-
27
- module ClassMethods # :nodoc:
28
- def environment
29
- return settings[:environment]
1
+ class Tynn
2
+ # Adds helper methods to get and check the current environment.
3
+ #
4
+ # ```
5
+ # require "tynn"
6
+ # require "tynn/environment"
7
+ #
8
+ # Tynn.helpers(Tynn::Environment)
9
+ #
10
+ # Tynn.environment # => :development
11
+ #
12
+ # Tynn.development? # => true
13
+ # Tynn.production? # => false
14
+ # Tynn.test? # => false
15
+ # ```
16
+ #
17
+ # By default, the environment is based on `ENV["RACK_ENV"]`.
18
+ #
19
+ # ```
20
+ # Tynn.helpers(Tynn::Environment, env: ENV["RACK_ENV"])
21
+ # ```
22
+ #
23
+ module Environment
24
+ def self.setup(app, env: ENV["RACK_ENV"]) # :nodoc:
25
+ app.set(:environment, (env || :development).to_sym)
30
26
  end
31
27
 
32
- def development?
33
- return environment == :development
34
- end
28
+ module ClassMethods # :nodoc:
29
+ def environment
30
+ return settings[:environment]
31
+ end
35
32
 
36
- def test?
37
- return environment == :test
38
- end
33
+ def development?
34
+ return environment == :development
35
+ end
36
+
37
+ def test?
38
+ return environment == :test
39
+ end
39
40
 
40
- def production?
41
- return environment == :production
41
+ def production?
42
+ return environment == :production
43
+ end
42
44
  end
43
45
  end
44
46
  end
@@ -1,15 +1,17 @@
1
1
  require "erubis"
2
2
  require_relative "render"
3
3
 
4
- module Tynn::Erubis
5
- def self.setup(app, options = {}) # :nodoc:
6
- options = options.dup
4
+ class Tynn
5
+ module Erubis
6
+ def self.setup(app, options = {}) # :nodoc:
7
+ options = options.dup
7
8
 
8
- options[:options] ||= {}
9
- options[:options] = {
10
- escape_html: true
11
- }.merge!(options[:options])
9
+ options[:options] ||= {}
10
+ options[:options] = {
11
+ escape_html: true
12
+ }.merge!(options[:options])
12
13
 
13
- app.helpers(Tynn::Render, options)
14
+ app.helpers(Tynn::Render, options)
15
+ end
14
16
  end
15
17
  end
@@ -1,11 +1,42 @@
1
1
  require "json"
2
2
 
3
- module Tynn::JSON
4
- JSON_CONTENT_TYPE = "application/json".freeze # :nodoc:
3
+ class Tynn
4
+ # Adds helper methods for json generation.
5
+ #
6
+ # ```
7
+ # require "tynn"
8
+ # require "tynn/json"
9
+ #
10
+ # Tynn.helpers(Tynn::JSON)
11
+ # ```
12
+ #
13
+ module JSON
14
+ JSON_CONTENT_TYPE = "application/json".freeze # :nodoc:
5
15
 
6
- def json(data)
7
- res.headers[Rack::CONTENT_TYPE] = JSON_CONTENT_TYPE
16
+ # Calls `to_json` on `data` and writes the generated \JSON object into
17
+ # the response body. Also, It automatically sets the `Content-Type`
18
+ # header to `application/json`.
19
+ #
20
+ # ```
21
+ # Tynn.define do
22
+ # on("hash") do
23
+ # json(foo: "bar")
24
+ # end
25
+ #
26
+ # on("array") do
27
+ # json([1, 2, 3])
28
+ # end
29
+ #
30
+ # on("to_json") do
31
+ # json(Model.first)
32
+ # end
33
+ # end
34
+ # ```
35
+ #
36
+ def json(data)
37
+ res.headers[Rack::CONTENT_TYPE] = JSON_CONTENT_TYPE
8
38
 
9
- res.write(data.to_json)
39
+ res.write(data.to_json)
40
+ end
10
41
  end
11
42
  end
@@ -1,58 +1,60 @@
1
- # Adds extra matchers to Tynn.
2
- #
3
- # ```
4
- # require "tynn"
5
- # require "tynn/matchers"
6
- #
7
- # Tynn.helpers(Tynn::Matchers)
8
- # ```
9
- #
10
- module Tynn::Matchers
11
- # A catch-all matcher.
1
+ class Tynn
2
+ # Adds extra matchers to Tynn.
12
3
  #
13
4
  # ```
14
- # Tynn.define do
15
- # authenticated? do
16
- # # ...
17
- # end
5
+ # require "tynn"
6
+ # require "tynn/matchers"
18
7
  #
19
- # default do # on true
20
- # # ...
21
- # end
22
- # end
8
+ # Tynn.helpers(Tynn::Matchers)
23
9
  # ```
24
10
  #
25
- # :call-seq: default(&block)
26
- #
27
- def default
28
- yield
11
+ module Matchers
12
+ # A catch-all matcher.
13
+ #
14
+ # ```
15
+ # Tynn.define do
16
+ # authenticated? do
17
+ # # ...
18
+ # end
19
+ #
20
+ # default do # on true
21
+ # # ...
22
+ # end
23
+ # end
24
+ # ```
25
+ #
26
+ # :call-seq: default(&block)
27
+ #
28
+ def default
29
+ yield
29
30
 
30
- halt(res.finish)
31
- end
31
+ halt(res.finish)
32
+ end
32
33
 
33
- # Match if the given `key` is present in `req.params`.
34
- #
35
- # ```
36
- # Tynn.define do
37
- # param(:user) do |params|
38
- # user = User.create(params)
39
- #
40
- # # ...
41
- # end
42
- #
43
- # default do
44
- # res.write("missing param")
45
- # end
46
- # end
47
- # ```
48
- #
49
- # :call-seq: param(key, &block)
50
- #
51
- def param(key)
52
- if (v = req[key]) && !v.empty?
53
- yield(v)
34
+ # Match if the given `key` is present in `req.params`.
35
+ #
36
+ # ```
37
+ # Tynn.define do
38
+ # param(:user) do |params|
39
+ # user = User.create(params)
40
+ #
41
+ # # ...
42
+ # end
43
+ #
44
+ # default do
45
+ # res.write("missing user param")
46
+ # end
47
+ # end
48
+ # ```
49
+ #
50
+ # :call-seq: param(key, &block)
51
+ #
52
+ def param(key)
53
+ if (v = req[key]) && !v.empty?
54
+ yield(v)
54
55
 
55
- halt(res.finish)
56
+ halt(res.finish)
57
+ end
56
58
  end
57
59
  end
58
60
  end
@@ -1,16 +1,18 @@
1
- module Tynn::NotFound
2
- def call(*) # :nodoc:
3
- result = super
1
+ class Tynn
2
+ module NotFound
3
+ def call(*) # :nodoc:
4
+ result = super
4
5
 
5
- if result[0] == 404 && result[2].empty?
6
- not_found
6
+ if result[0] == 404 && result[2].empty?
7
+ not_found
7
8
 
8
- return res.finish
9
- else
10
- return result
9
+ return res.finish
10
+ else
11
+ return result
12
+ end
11
13
  end
12
- end
13
14
 
14
- def not_found # :nodoc:
15
+ def not_found # :nodoc:
16
+ end
15
17
  end
16
18
  end